Skip to content

[RESOLVED] PostMessage/SendMessage in C#/.NET

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #191441
    nooob
    Member

    hi all

    Can anyone help me with how to set text to a textfield/textbox using e.g. the SendMessage api in C# or .NET?

    The signature of the SendMessage in C# is this:

    [DllImport(“user32.dll”, CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    where the wParam and lParam are both of type IntPtr (and not of type StringBuilder)…so i dont know where to put the string that I want to set to a textbox!!!

    Is there another way?!

    Plz…help

    ty alot

    nooob

    #191448

    post your question in c#.net forum, you’ll have more chance to get response but bwt i’m around here so I will give you the solution. I’m not good for explanation but check this code bellow

    [DllImport(“user32.dll”, EntryPoint = “SendMessage”)]
    public static extern int SendMessageByString(
    #if STRING
    int hwnd, uint wMsg, int wParam, string lParam);//lParam :
    #else
    int hwnd, uint wMsg, int wParam, StringBuilder lParam);//lParam :

    #endif

    that’s code it work for stringbuilder and string

    #191447

    or if you dont want to complicate your life, just doing like this

    [DllImport(“user32.dll”, EntryPoint = “SendMessage”)]
    public static extern int SendMessageByString(
    int hwnd, uint wMsg, int wParam, string lParam);//lParam :

    the 4th parameter is string type and not stringbuilder but I like the first one

    #191446
    nooob
    Member

    ty alot nanomachine for ur time…

    below is my code:

    private void btnBounce_Click(object sender, EventArgs e)
    {
    IntPtr hRoom = FindRoom("My Window Class");
    PostMessage(hRoom, WM_COMMAND, (IntPtr)32947, IntPtr.Zero); //if I run the program and click the button the prog stops here...halts
    
    IntPtr hBounceWin = FindRoom("#32770");
    IntPtr hEdit = FindWindowEx(hBounceWin, IntPtr.Zero, "Edit", null);
    SendMessageByString(hEdit, WM_SETTEXT, IntPtr .Zero, "Bounce This Person");
    IntPtr hBounceBut = FindWindowEx(hBounceWin, IntPtr.Zero, "Button", "Bounce");
    SendMessageLong(hBounceBut, WM_KEYDOWN, VK_SPACE, IntPtr.Zero);
    SendMessageLong(hBounceBut, WM_KEYUP, VK_SPACE, IntPtr.Zero);
    }

    if I run the prog and click on the associated button then the prog stops after executing the PostMessage api…but if i debug it…then everything is fine?!?!

    Am I missing something like DoEvents as in vb6?

    any idea?

    thx nano

    nooob

    #191445

    your error maybe this line

    IntPtr hBounceWin = FindRoom(“#32770”);

    cuz the winclass #32770 is so many, i’m not sure you can find the right window handle with using that class in parameter

    between IntPtr hBounceWin = FindRoom(“#32770”); and your postmessage
    maybe do a sleep 100 milisecond to let the time window to bounce user loaded

    #191444
    nooob
    Member

    ok nanomachine dude…thanks alot for your replies

    i will try

    nooob

    #191443

    what version of .net do u use, I’m using vs2005.

    if you use same version of mine and if you dont figure it out yet, I will give a try.

    to find apart of window caption use enumwindows is a good idea. here is example.

    EnumWindows(EnumWindowsProc,IntPtr.Zero);

    and the function EnumWindowsProc is like this

    private static bool EnumWindowsProc(IntPtr hWnd, IntPtr param)
    {
    string tmp = GetWindowText(hWnd);
    if (tmp.IndexOf(“Ready to bounce”)>-1)
    {
    hWndTobounce =hWnd;
    return false;
    }
    return true;
    }

    define api enumwindows like this

    public delegate bool CallBack(IntPtr handle, IntPtr param);
    [DllImport(“user32.dll”)]
    public static extern bool EnumWindows(CallBack cb, IntPtr param);

    #191442
    nooob
    Member

    😀 got it…dude

    nanomachine ty dude…ur previous post gave me the answer…i.e. to sleep for a few ms.

    sleeping there is imporant because i load a window there…
    now it works…thnx alot

    i use the vc#2005 express edition

    i neew help with another thing as well (getting the width of a view list)..but i will make it another thread

    nooob[/b]

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.