Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4610080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:04:50+00:00 2026-05-22T01:04:50+00:00

I need to automate a third party program, and the only course of action

  • 0

I need to automate a third party program, and the only course of action is to simulate a click on some buttons.

I do this by finding the HWND handle of the button with EnumChildWindows. When I’ve found the “window” (the button), I try to send BM_CLICK to it with SendMessageW. This works, my tests show that the button indeed think it was clicked.

The problem arise in my error handling. There is no feedback given by the BM_CLICK message, so I don’t really know if it has been clicked. I thought I should be diligent and check for any error codes though with Marshal.GetLastWin32Error.

This returns ERROR_PROC_NOT_FOUND, which is not really what I would expect from a successful message handling.

I import SendMessageW as follows:

[DllImport("User32.dll", 
           CharSet = CharSet.Unicode, 
           CallingConvention = CallingConvention.Winapi, 
           SetLastError = true)]
public static extern IntPtr SendMessageW(
     HandleRef hWnd, 
     UInt32 Msg, 
     UIntPtr wParam, 
     IntPtr lParam);

The code doing the call is:

User32.SendMessageW(
    buttonHandle,
    (uint)ButtonControlMessages.BM_CLICK, // value of BM_CLICK = 0x00F5.
    UIntPtr.Zero,
    IntPtr.Zero);

int error = Marshal.GetLastWin32Error();
if (error != ErrorCodes.ERROR_SUCCESS) // love the name of this error code.
    throw new Win32Exception(error);

My tests are just using a simple Windows Forms with a button control attached. Thus, I can procure the handle through button.Handle. It gets clicked; could it be that this error is completely unrelated?

It sure would be nice to get rid of it though, I’d like some way to be sure that the call to SendMessageW at least didn’t fail.

I’m on Windows 7 x86-32 with .NET 4.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:04:51+00:00Added an answer on May 22, 2026 at 1:04 am

    The calling convention should be Stdcall, but since that is the default you can just drop it.

    I think your SendMessage P/Invoke looks a bit odd but that’s probably not the cause of the issue. In any case I would do it like this:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    

    I think what’s happening here is that SendMessage() is working but isn’t assigning the last error. The only thing that the documentation for SendMessage() mentions about errors is:

    When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied).

    The return value for SendMessage() is dependent on the message sent. In the case of BM_CLICK there is apparently no message sent. In other words you simply do not get any feedback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to automate a testing for an third party application. I've only the
I need to automate some tasks on a website that does not have an
This is the new input file format. I need to automate the process of
an application I have written uses several third party jars. Sometimes only a small
I need to automate downloading of a file from this site http://stats.smith.com/reports/Default.aspx using C#.
I need to automate some tasks in Adobe InDesign CS3 from a .NET 4.0
http://support.microsoft.com/kb/213930 shows How to Create a Bell Curve Chart. I need to automate this
I'm working on a third-party program that aggregates data from a bunch of different,
I need to automate a process involving a website that is using a login
I need to automate an IM client to update a status every few hours,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.