Link to home
Start Free TrialLog in
Avatar of meteorelec
meteorelecFlag for Ireland

asked on

Passing a parameter to VB and then using sendmessage

What I'm doing here is firing an exe from a command line with a parameter at the end.  This is then passed to the exe and used to look up the account.  I can pass the parameter by using sendkeys but I want to use sendmessage to do it.  I have attached snippets of the code that I am trying to use without success.  I'm not suer if I should be using WM_SETTEXT or the CStr(AccountName).  Any advice would be very welcome.  Thanks.
Public AccountName As String = ""
 
Call SendMessageByString(TPickList, WM_SETTEXT, 0&, CStr(AccountName))

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If WM_SETTEXT doesn't work then, you could try sending the string as individual characters using WM_KEYDOWN and WM_CHAR:
Imports System.Runtime.InteropServices
Public Class Form3
 
    Private Const WM_CHAR As Integer = &H102
 
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
        (ByVal parent As IntPtr, ByVal child As IntPtr, _
        ByVal className As String, ByVal caption As String) As IntPtr
 
    Public Declare Auto Function PostMessage Lib "user32.dll" ( _
        ByVal handle As IntPtr, ByVal wMsg As Integer, _
        ByVal wParam As Integer, ByVal lParam As Integer) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim notepad As Process
 
        Dim ps() As Process = Process.GetProcessesByName("NotePad")
        If ps.Length = 0 Then
            notepad = Process.Start("NotePad")
            notepad.WaitForInputIdle()
        Else
            notepad = ps(0)
        End If
 
        Dim editHandle As IntPtr = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", "")
        Dim msg As String = "WM_CHAR works..."
        For Each c As Char In msg
            PostMessage(editHandle, WM_CHAR, Microsoft.VisualBasic.AscW(c), IntPtr.Zero)
        Next
    End Sub
 
End Class

Open in new window

Avatar of meteorelec

ASKER

Hi Idle Mind,

I've tried what you have said and this is where I'm at.  I can use sendmessage to click the button that I require on the application window, I can now use sendmessage to send a piece of text to the box that needs to be populated.  However when I put these onto the one button they do not work so from the form that I have developed I have to button's one to generate the window that needs to be populated and one then to pass the text.  I have attached the code below.  Thanks for your help.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        SendMessageByString(TBitBtnEx, BM_CLICK, 0&, 0) 'Not sure if CStr is a function in VB7?'
 
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Call SendMessageByString(TPickList, WM_SETTEXT, IntPtr.Zero, "WM_SETTEXT works...")
 
    End Sub

Open in new window

Have you tried adding a delay between the two to allow the window to pop up?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         SendMessageByString(TBitBtnEx, BM_CLICK, 0&, 0)
         System.Threading.Thread.Sleep(1000)
         SendMessageByString(TPickList, WM_SETTEXT, IntPtr.Zero, "WM_SETTEXT works...")
    End Sub
Yeah I tried that without success, I've just tried it with your code and it was unsuccessful as well.  

It seems to be once completes the SendMessageByString(TBitBtnEx, BM_CLICK, 0&, 0) and generates the new window that it is looking for something else.  

The reason that I say this is because I cannot go back to Form 1 to click the other button but if I split the commands onto 2 seperate buttons then I can click the 1st button to generate the Account Lookup window and click the 2nd button to pass the text with no problems.
I wonder if PostMessage() instead of SendMessage() would make a difference...  =\
Do you know how to define it?
It should be declared exactly like SendMessage....just change "SendMessage" to "PostMessage".  =)

Hi Idle Mind, I did a bit of playing about and got it working through the use of the Postmessage followed by a delay and then the SendMessageByString.  Hopefully the rest of it will be a bit less tricky lol.  Thanks for your help.
You're welcome!

I was helping you in two of your other open questions...is this related to those as well?  ...or do I need to revisit those threads?
Yeah this is related to those as well but don't worry about them as I think I'll be able to fumble my way from here.  If I can't then I'll post a new thread.  Once again thanks a million for the help.
Avatar of l0o3
l0o3

Excuse me, Is this code in C#?
That's VB.Net code...though you could easily use a free online converter to get it into C#.