By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

How to get the correct Window Handle for a application opened using createprocess api

P: 17
Dear All,

We have a requirement to open any document with a specific application and display it within a OCX control (Example - word, excel, adobe reader, MSPaint, Photoshop etc). We are using CreateProcess Api to open the application. This APi returns process id and process handle. Once the application is opened, i need the exact window handle to show within the container (OCX control). I use "EnumWindows" to enumerate all top level windows and check
whether there is no parent and check for the process id. If the process id matches, i check the application class name to get the exact handle (many handles are returned for the same process id). I beleive the application class name will be unique for each window handle. Is there a better way to get the exact window handle for the application opened using createprocess?

================================================== ========Code:

ReDim malngWindowHandles(0) As Long
lngRetVal = EnumWindows(AddressOf EnumWindowsProc, lngParam)

For lngHandle = 1 To UBound(malngWindowHandles)
lngRetVal = GetWindowLong(malngWindowHandles(lngHandle), GWL_HWNDPARENT)
If lngRetVal = 0 Then
If lngProcessId = GetProcIDFromWHandle(malngWindowHandles(lngHandle) ) Then
strClassName = Space$(256)
lngClassLength = GetClassName(malngWindowHandles(lngHandle), strClassName, 256)
strClassName = UCase$(Trim$(Left$(strClassName, lngClassLength) & ""))
strClassName = """" & strClassName & """"
If InStr(strAppClassName & ",", strClassName & ",") > 0 Then
GetApplicationHandle = malngWindowHandles(lngHandle): Exit For
End If
End If
End If

================================================== ============================================

Thanks & Regards,
May 4 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.