Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ShellExecute
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Title:
Miscellaneous
Thread ID:
01016790
Message ID:
01016829
Views:
11
I just found this out myself... Sergey is absolutely correct and I apologize for my earlier post as it was completely misleading. I should have looked on news2news to make sure I guess, I was going on the MS documentation available on the MSDN. MS documentation assures us that the HINSTANCE that is returned is a real HINSTANCE, however this is not the case as can be seen in the following conversion of the VB code...
#DEFINE GW_HWNDNEXT  2

DECLARE LONG GetParent In "user32" LONG HWND
DECLARE LONG GetWindow In "user32" LONG HWND , LONG wCmd
DECLARE LONG FindWindow In "user32" AS FindWindowA STRING lpClassName , STRING lpWindowName
DECLARE LONG GetWindowText In "user32" AS GetWindowTextA LONG HWND, STRING @lplcing, LONG cch
DECLARE LONG GetWindowThreadProcessId In "user32" LONG HWND , LONG @lpdwprocessid
DECLARE INTEGER ShellExecute IN "shell32" INTEGER HWND, STRING lpOperation, STRING lpFile, STRING lpParameters, STRING lpDirectory, INTEGER nShowCmd

LOCAL hInst  && Instance handle from Shell function.
LOCAL hWndApp   && Window handle from GetWinHandle.
LOCAL BUFFER  && Holds caption of Window.
LOCAL numChars  && Count of bytes returned.

*!* Shell to an application
hInst = ShellExecute(0, "open", "calc.exe", "", "", 3)

IF hInst > 32
	*!* Begin search for handle
	hWndApp = GetWinHandle(hInst)

	IF hWndApp <> 0
	*!* Init buffer
		BUFFER = SPACE$(128)

	*!* Get caption of window
		numChars = GetWindowTextA(hWndApp, @BUFFER, LEN(BUFFER))

	*!* Display window's caption
		MESSAGEBOX("You shelled to the Application: " + LEFT(BUFFER, numChars))
	ELSE
		MESSAGEBOX("Couldn't find the application")	
	ENDIF
ELSE
	MESSAGEBOX("There was a problem calling ShellExecute")
ENDIF

FUNCTION ProcIDFromWnd(HWND)
	LOCAL idProc

*!* Get PID for this HWnd
	idProc = GetWindowThreadProcessId(HWND, 0)

*!* Return PID
	RETURN (idProc)
ENDFUNC

FUNCTION GetWinHandle(hInstance)
	LOCAL tempHwnd

*!* Grab the first window handle that Windows finds:
	tempHwnd = FindWindowA(NULL, NULL)

*!* DO until you find a match or there are no more window handles:
	DO WHILE NOT tempHwnd = 0
*!* Check if no parent for this window
		IF GetParent(tempHwnd) = 0
*!* Check for PID match
			IF hInstance = ProcIDFromWnd(tempHwnd)
*!* Exit search ENDDO
				EXIT
			ENDIF
		ENDIF

*!* Get the next window handle
		tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
	ENDDO
*!* Return found handle
	RETURN (tempHwnd)
ENDFUNC
#DEFINE GW_HWNDNEXT  2

DECLARE LONG GetParent In "user32" LONG HWND
DECLARE LONG GetWindow In "user32" LONG HWND , LONG wCmd
DECLARE LONG FindWindow In "user32" AS FindWindowA STRING lpClassName , STRING lpWindowName
DECLARE LONG GetWindowText In "user32" AS GetWindowTextA LONG HWND, STRING @lplcing, LONG cch
DECLARE LONG GetWindowThreadProcessId In "user32" LONG HWND , LONG @lpdwprocessid
DECLARE INTEGER ShellExecute IN "shell32" INTEGER HWND, STRING lpOperation, STRING lpFile, STRING lpParameters, STRING lpDirectory, INTEGER nShowCmd

LOCAL hInst  && Instance handle from Shell function.
LOCAL hWndApp   && Window handle from GetWinHandle.
LOCAL BUFFER  && Holds caption of Window.
LOCAL numChars  && Count of bytes returned.

*!* Shell to an application
hInst = ShellExecute(0, "open", "calc.exe", "", "", 3)

IF hInst > 32
	*!* Begin search for handle
	hWndApp = GetWinHandle(hInst)

	IF hWndApp <> 0
	*!* Init buffer
		BUFFER = SPACE$(128)

	*!* Get caption of window
		numChars = GetWindowTextA(hWndApp, @BUFFER, LEN(BUFFER))

	*!* Display window's caption
		MESSAGEBOX("You shelled to the Application: " + LEFT(BUFFER, numChars))
	ELSE
		MESSAGEBOX("Couldn't find the application")	
	ENDIF
ELSE
	MESSAGEBOX("There was a problem calling ShellExecute")
ENDIF

FUNCTION ProcIDFromWnd(HWND)
	LOCAL idProc

*!* Get PID for this HWnd
	idProc = GetWindowThreadProcessId(HWND, 0)

*!* Return PID
	RETURN (idProc)
ENDFUNC

FUNCTION GetWinHandle(hInstance)
	LOCAL tempHwnd

*!* Grab the first window handle that Windows finds:
	tempHwnd = FindWindowA(NULL, NULL)

*!* DO until you find a match or there are no more window handles:
	DO WHILE NOT tempHwnd = 0
*!* Check if no parent for this window
		IF GetParent(tempHwnd) = 0
*!* Check for PID match
			IF hInstance = ProcIDFromWnd(tempHwnd)
*!* Exit search ENDDO
				EXIT
			ENDIF
		ENDIF

*!* Get the next window handle
		tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
	ENDDO
*!* Return found handle
	RETURN (tempHwnd)
ENDFUNC
>I converted the code to VFP and the return from GetWindowProcessID doesn't return the same thing that ShellExecute returns.
>
>>The code at the below link, while in VB is pretty straight forward...
>>
>>http://support.microsoft.com/kb/242308/
>>
>>
>>>Is there a way to get the windows handle from the ShellExecute command? ShellExecute returns the instance handle and I can't seem to get to the Windows Handle. I've been using the FindWindowEx function to look for the title but that doesn't guarantee me the correct window.
Previous
Reply
Map
View

Click here to load this message in the networking platform