FindWindow is not working



Dear coleagues,

I am trying to call FindWindow function but it seems not to be working. It
returns a loooooong value even if the Window "SoftMaxPro GxP" is not
started. I can't even find a window which is open. What am I doing wrong?
Plese help....
Kind regards, Alex

Imports System

Imports System.Data

Imports System.Windows.Forms

Module Module1


Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As String) As Long

Private Declare Function RegisterWindowMessage Lib "user32" Alias
"RegisterWindowMessageA" (ByVal msgName As String) As Long

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Const WM_SETTEXT = 12

'No. of seconds that GetMsgFromPro() will wait before timing out

Dim mcsngMsgTimeout As Single = 600

Sub Main()

Dim sndMsg, inpStr As String

sndMsg = "_"

inpStr = "OK"


Console.WriteLine("Type command")

sndMsg = Console.ReadLine()



Console.WriteLine("Finish. Press a key to continue or space Q to finish.")

inpStr = Console.ReadLine()

Loop Until inpStr <> "Q"

End Sub

Public Function SendMsgToProT(ByVal msgStr As String) As Long

'Function to send windows command to SOftMaxPro application

'This is the primary method of communication between the two processes, on
the same PC

Console.WriteLine("..command sending in progress.Please wait.")

Dim lVal, hWnd, softmaxMsg As Long

hWnd = 0

softmaxMsg = 0

hWnd = FindWindow("SoftMaxPROMainWnd", "SoftMax Pro GxP")

'hWnd = FindWindow(vbNullString, "Notepad")

If hWnd = 0 Then

Console.WriteLine("OK: SoftMax Pro GxP connected.")



softmaxMsg = RegisterWindowMessage("SoftMaxProMsg")

If softmaxMsg = 0 Then

Console.WriteLine("ERROR: SoftMax")




lVal = SendMessage(hWnd, WM_SETTEXT, softmaxMsg, msgStr)

Console.WriteLine("Command " + msgStr + " is sent to SoftMax Pro GxP.")

End If

End If

End Function

Public Function GetMsgFromPro(ByVal strMessage As String) As Long

'First send a request message to the iunstrument and then get

'back a text message from the instrument via the clipboard.

'Wait for a message for up to (mcsngMsgTimeout) seconds before giving up.

Dim strClip As String

Dim sngStart As Integer = New Integer

Dim sngEnd As Integer = New Integer

sngStart = 0

sngEnd = 30


Console.WriteLine(" waiting in progress.Please wait.")

While sngStart < sngEnd

strClip = Clipboard.GetText()


sngStart = sngStart + 1

End While

End Function

End Module


Dear all,

It works now with new declaration. Integer instead of Long is the solution.

Kind regards, Alex

Herfried K. Wagner [MVP]

Alex said:
It works now with new declaration. Integer instead of Long is the

Note that handles must be declared as 'IntPtr' in order to make the code
work on 64-bit systems.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads