To check if a document is opened in jWord by Application 1 before closing cWord in Application 2, you can use the following approach:
In Application 1, keep track of the document state using a variable or flag that indicates whether a document is currently open or not. Set the flag to true when a document is opened in jWord and false when the document is closed.
In Application 2, before calling cWord:Quit(), you can use inter-process communication to check the state of the document in jWord. Here's a general outline of the steps involved:
a. Use a suitable inter-process communication mechanism supported by your programming language or platform, such as named pipes, sockets, or shared memory, to establish communication between Application 1 and Application 2.
b. In Application 1, expose a method or API that returns the current state of the document in jWord (i.e., whether it is open or closed).
c. In Application 2, call the method or API provided by Application 1 to query the document state in jWord. Based on the response received, you can determine whether it's safe to call cWord:Quit().
d. If the document state returned from Application 1 indicates that a document is open in jWord, skip calling cWord:Quit() to avoid closing the shared document.
Note that the exact implementation details may vary depending on the programming languages and frameworks you're using for both applications. Make sure to consult the documentation and resources specific to your development environment for guidance on inter-process communication and exposing APIs.
Public Const WM_COPYDATA As Integer = &H4A
Private Sub CheckJWordDocumentState()
' Find the handle of Application 1
Dim hWnd As IntPtr = FindWindow(Nothing, "Application 1 Window Title") ' Replace "Application 1 Window Title" with the actual title of Application 1
If hWnd <> IntPtr.Zero Then
' Send a message to Application 1 to check the document state
Dim documentState As Boolean = GetJWordDocumentState(hWnd)
If Not documentState Then
' Call cWord:Quit() because no document is open in jWord
cWord.Quit()
Else
' Handle the case when a document is open in jWord
' You can display an error message or prompt the user to close the document manually
End If
End If
End Sub
Private Function GetJWordDocumentState(ByVal hWnd As IntPtr) As Boolean
Dim cds As New CopyDataStruct()
cds.dwData = IntPtr.Zero
cds.lpData = String.Empty
cds.cbData = 0
Dim result As IntPtr = SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, cds)
If cds.cbData = 1 AndAlso cds.lpData <> String.Empty Then
Return cds.lpData = "1"
End If
Return False ' Default state if an error occurred
End Function
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowW" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As CopyDataStruct) As IntPtr
In your code, call the CheckJWordDocumentState() method before closing Application 2 or before invoking cWord:Quit().
Note: Replace "Application 1 Window Title" in Application 2 code with the actual title of the Application 1 window. Ensure that both applications are running with their respective windows open before attempting to check the document state.
#include <iostream>
#include <vector>
#include <windows.h>
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
std::vector<HWND>& windows = *reinterpret_cast<std::vector<HWND>*>(lParam);
char className[256];
if (GetClassName(hwnd, className, sizeof(className)) != 0) {
if (strcmp(className, "YourTargetClassName") == 0) {
windows.push_back(hwnd);
}
}
return TRUE;
}
int main() {
std::vector<HWND> windows;
if (EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&windows))) {
for (HWND hwnd : windows) {
char windowTitle[256];
GetWindowText(hwnd, windowTitle, sizeof(windowTitle));
std::cout << "Window Title: " << windowTitle << std::endl;
}
}
return 0;
}
STATIC PROCEDURE WordQuit
cWord:Quit()
RETURN
#include "FiveWin.ch"
function Main()
local nFWHApps := 0
EnumWindows( { | hWnd | If( GetClassName( hWnd ) == "TWINDOW", nFWHApps++, ) } )
MsgInfo( nFWHApps )
return nil
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot] and 25 guests