简体   繁体   中英

Need help with RegisterWindowMessage and SendMessage from c++ to c#

I have this code in C#:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern uint RegisterWindowMessage(string lpProcName);

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
   IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

and I need to convert this from C++ to C#:

UINT UWM_UART_CTRL_TRS = ::RegisterWindowMessage(_T("BT_UARTCTRL_TRANSFER"));
::SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, 0, 0);

and this:

UINT UWM_UART_CTRL_TRS = ::RegisterWindowMessage(_T("BT_UARTCTRL_TRANSFER"));
::SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, 1, 0);

Thanks in advance

Should be pretty straight forward. You'll need to change the PInvoke definitions to public inside the class that contains them:

public class Win32Api
{
   [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
   public static extern uint RegisterWindowMessage(string lpString);

   [DllImport("user32.dll", SetLastError = true)]
   public static extern IntPtr SendMessage(
      IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
}

Then for the calls, they would look like this:

uint UWM_UART_CTRL_TRS = Win32Api.RegisterWindowMessage("BT_UARTCTRL_TRANSFER");
Win32Api.SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, (IntPtr)0, (IntPtr)0);

and this:

uint UWM_UART_CTRL_TRS = Win32Api.RegisterWindowMessage("BT_UARTCTRL_TRANSFER");
Win32Api.SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, (IntPtr)1, (IntPtr)0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM