Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to provide an hWnd handle of my application when I have no window?

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I am trying to use the GetWindow API , which takes 2 parameters, a window handel, and a option, I got the code for seeing all the Tasks that are runing from vb-world.net, modified it so I could use it in my ActiveX dll, but the problem is I dont have a form1.hwnd, therefore I cant figure out how to provide a Hwnd, and it wont take a NULL, 0 or anything else. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
I tried App.hInstance, but I still get back zero as a result<br>here is the function as it have it set to return a string seperated by VBNullchar<br><br><FONT FACE=monospace><br>Public Function TaskRunning(ByRef Result As Variant) As Boolean<br><br>Dim CurrWnd As Long<br>Dim Length As Long<br>Dim TaskName As String<br>Dim Parent As Long<br>Dim first As Boolean<br>first = True<br><br>CurrWnd = GetWindow(App.hInstance, GW_HWNDFIRST)<br><br>While CurrWnd &lt;&gt; 0<br><br>Parent = GetParent(CurrWnd)<br>Length = GetWindowTextLength(CurrWnd)<br>TaskName = Space$(Length + 1)<br>Length = GetWindowText(CurrWnd, TaskName, Length + 1)<br>TaskName = Left$(TaskName, Len(TaskName) - 1)<br><br>If Length &gt; 0 Then<br>&nbsp;&nbsp;&nbsp;If first = True Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result = TaskName<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first = False<br>&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result = vbNullChar & TaskName<br>&nbsp;&nbsp;&nbsp;End If<br>End If<br>CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)<br>DoEvents<br><br>Wend<br>TaskRunning = True<br>End Function<br></font><br><br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
when I said gets back zero as a result, I mean GetWindow returns zero, app.hInstance, is a non-zero <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
I am using an invisible form in the ActiveX dll in the meantime, hopefully it'll work ok on a NT until I find a different method. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Karl,<br>&nbsp;&nbsp;&nbsp;I'm confused.&nbsp;&nbsp;If you don't have a window, why would you expect to be able to get a window handle???<br> <p>nick bulka<br><a href=mailto: > </a><br><a href= > </a><br>
 
the example uses Getwindow to show all the tasks, and GetWindow requires a Window Hwnd, I dont know how else to get all the tasks running. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Karl,<br><br>Interesting problem - as far as I know even processes without a UI have an hWnd - but I've never had to know it myself.<br><br>I found this, rather old, code that <i>might</i> be of use to you. It finds an hWnd based on a hInstance -- but for old versions of windows which is a bit annoying.<br><FONT FACE=monospace><b><br>&nbsp;&nbsp;&nbsp;' Constants used<br>&nbsp;&nbsp;&nbsp;Global Const GW_HWNDNEXT = 2<br>&nbsp;&nbsp;&nbsp;Global Const GWW_HINSTANCE = (-6)<br><br>&nbsp;&nbsp;&nbsp;' Enter each of the following declarations as one, single line:<br>&nbsp;&nbsp;&nbsp;Declare Function GetParent Lib &quot;User&quot; (ByVal hWnd As Integer) As Integer<br>&nbsp;&nbsp;&nbsp;Declare Function GetWindow Lib &quot;User&quot; (ByVal hWnd As Integer,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal wCmd As Integer) As Integer<br>&nbsp;&nbsp;&nbsp;Declare Function GetWindowWord Lib &quot;User&quot; (ByVal hWnd As Integer,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal nIndex As Integer) As Integer<br>&nbsp;&nbsp;&nbsp;Declare Function FindWindow Lib &quot;User&quot; (ByVal lpClassName As Any,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal lpWindowName As Any) As Integer<br>&nbsp;&nbsp;&nbsp;Declare Function GetWindowText Lib &quot;User&quot; (ByVal hWnd As Integer,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal lpString As String, ByVal aint As Integer) As Integer<br><br>&nbsp;&nbsp;&nbsp;Function GetWinHandle (hInstance%) As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Function receives an instance handle as a parameter and returns<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' the windows handle of the window with a matching instance handle.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim tempHwnd%<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Grab the first window handle that Window's finds:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempHwnd% = FindWindow(0&, 0&)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Loop until there are no more window handles:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Do Until tempHwnd% = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Check if you have the applications Parent window:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If GetParent(tempHwnd%) = 0 Then<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Check the instance handle for the app:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If hInstance% = GetWindowWord(tempHwnd%, GWW_HINSTANCE) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Found a match:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetWinHandle = tempHwnd%<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempHwnd% = GetWindow(tempHwnd%, GW_HWNDNEXT)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Loop<br>&nbsp;&nbsp;&nbsp;End Function<br><br>&nbsp;&nbsp;&nbsp;Sub Command1_Click ()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim hInst As Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Instance handle from Shell function.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim hWndApp As Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Window handle from GetWinHandle.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim buffer As String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Holds caption of Window.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim numChars As Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Count of bytes returned.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Shell to an application and get its window handle:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hInst = Shell(&quot;calc.exe&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hWndApp = GetWinHandle(hInst)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Verify that you have the correct handle by displaying<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' its window caption in a message box:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer = Space$(128)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numChars = GetWindowText(hWndApp, buffer, Len(buffer))<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;You shelled to the Application: &quot; & Left$(buffer, numChars)<br>&nbsp;&nbsp;&nbsp;End Sub<br><br></font></b><br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Karl, did you check the VB FAQ &quot;List all applications / processes currently running&quot;? There is some code there that doesn't use GetWindow or expect an hWnd. Maybe you can use it. <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
actually, the one in the FAQ is the exact example I seen (from VB-World.net) the second solution just shows the exe and path of any running process <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
An ActiveX .EXE may return a window handle but an ActiveX component probably won't. Assuming, from your previous posts, that you you are trying to prevent a &quot;multiple instance&quot; scenario, you might try a different approach:<br><br>Leave a marker in the registry, the environment or the file system. Something that says &quot;Kilroy is already here, End Program.&quot;<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
no I just want something where I Can report alot of information about the system, NT, IIS version, ODBC listings, all their DSN they've registered, so on. this is just part of the thing I'm&nbsp;&nbsp;trying to get, trying to write a Dianostic ASP page(with some help from ActiveX dll on serverside) this will make life much easier on the customer when they have a prob we can check their dianostic page, I've already made an ASP that allows viewing of all their exe/ocx/dll versions, if any of our componets happened to be out of date, or wrong. (already wrote the instalation software as well, Didnt write the web application, just the stuff to help it manage) <br><br><font color=red>think that was too much info? naw</font> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top