Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Running an external exe file from VFP
Message
From
22/10/2004 04:04:24
Jon Neale
Bond International Software
Wootton Bassett, United Kingdom
 
 
To
22/10/2004 03:36:46
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00953639
Message ID:
00953649
Views:
18
Get a thread to the window using the follwoing, you need to pass in the exe's Window name.
FUNCTION x_payrun
LPARAMETERS p_title

LOCAL llThread

IF EMPTY(p_title)
	RETURN -1
ENDIF 

llThread = .F.
IF TYPE("p_thread") = "N"
	llThread = .T.
ENDIF

#DEFINE GW_HWNDLAST  1 
#DEFINE GW_HWNDNEXT  2 
#DEFINE GW_CHILD     5 
DO decl 

LOCAL hDesktop, hFirstChild, hLastChild, rc, cWinCap, cWinClass,; 
    nVisible, nLeft, nTop, nRight, nBottom 

hDesktop = GetDesktopWindow() 
hFirstChild = GetWindow(hDesktop, GW_CHILD) 
hLastChild = GetWindow(hFirstChild, GW_HWNDLAST) 

hCurrent = hFirstChild 
DO WHILE .T. 

    cWinCap = GetWinText(hCurrent) 
	
	IF (UPPER(ALLTRIM(p_title))$UPPER(ALLTRIM(cWinCap))) and IsWindowVisible(hCurrent) <> 0

		* got the correct window 
		llReturn = hCurrent
		
		IF llThread
			IF hCurrent = p_thread 
				* window is still open keep solus waiting before shutting down
				RETURN hCurrent
			ELSE
				RETURN -1
			ENDIF
		ELSE
			* if we find it activate it before closing it 
			Declare Integer ShowWindow in Win32API ;
				Integer hwnd, Integer nCmdShow
			* Restore it to its last size and position
			= ShowWindow(hCurrent,9)
			EXIT 
		ENDIF
	ENDIF 
	
    IF hCurrent = hLastChild 
        llReturn = -1
        EXIT 
    ENDIF 
    hCurrent = GetWindow(hCurrent, GW_HWNDNEXT) 
ENDDO 

RETURN llReturn
This bit uses the thread to send a message to the exe to close
LPARAMETERS lctitle

LOCAL lnRet

IF EMPTY(lcTitle) 
	RETURN 
ENDIF 

lnhWnd = x_payrun(lctitle)
	
IF lnhWnd > 0 
	#DEFINE WM_CLOSE   0x0010
	DECLARE INTEGER SendMessage IN Win32API;
	  INTEGER hwnd, INTEGER uMsg, ;
	  INTEGER wParam, INTEGER lParam
	DECLARE INTEGER GetLastError IN kernel32
	DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject

	* lnhWnd is the Window handle to close
	* Close and allow prompt to save changes
	lnRet=SendMessage(lnhWnd, WM_CLOSE, 0, 0)
		
	DO WHILE x_payrun(lctitle,lnhWnd) > 0 
	   * loop solus
           IF INKEY() = 27
		* escape pressed - loop over this session
		EXIT
           ENDIF
	ENDDO
		
	* Unable to communicate
	IF lnRet <> 0
	    =MESSAGEBOX("Error occurred. Error code: "+STR(getlasterror()),16,"Solus Application Error" )
	    RETURN
	ELSE
	     =CloseHandle(lnhWnd)
	ENDIF
ENDIF
Hope this makes sense (and works).

Jon
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform