Send Message(string) c# exe to vc++ exe(unmanaged) using WindowsSendMessage

M

Mayur

I tried followinf but working fine fo int but how to do it for string using
user custome message

in c#
[DllImport("User32.dll")]

public static extern int FindWindow(string strClassName,string
strWindowName);

[DllImport("User32.dll")]

public static extern Int32 SendMessage(

int hWnd, // handle to destination window

int Msg, // message

int wParam, // first message parameter

[MarshalAs(UnmanagedType.LPStr)] string lParam);



public const int WM_APP = 0x8000;

public const int WM_DELETEALL =WM_APP + 0x100;

int wnd=FindWindow(null,"TestMfcSendMsgExe");

Form1.SendMessage(wnd,WM_DELETEALL,0,55);





and MFC Exe is like this

#define WM_DELETEALL WM_APP + 0x100

BEGIN_MESSAGE_MAP(CTestMfcSendMsgExeDlg, CDialog)

ON_MESSAGE (WM_DELETEALL, OnDeleteAll)


END_MESSAGE_MAP()

LRESULT CTestMfcSendMsgExeDlg::OnDeleteAll(WPARAM wParam, LPARAM lParam)

{

MessageBox("Hello","Mayur",MB_OK);

}

I am getting the message box after using SendMessage() in c# exe but i want
to send text(string ) information.

I need help..

Regards,

Mayur.
 
W

Willy Denoyette [MVP]

Mayur said:
I tried followinf but working fine fo int but how to do it for string using
user custome message

in c#
[DllImport("User32.dll")]

public static extern int FindWindow(string strClassName,string
strWindowName);

[DllImport("User32.dll")]

public static extern Int32 SendMessage(

int hWnd, // handle to destination window

int Msg, // message

int wParam, // first message parameter

[MarshalAs(UnmanagedType.LPStr)] string lParam);



public const int WM_APP = 0x8000;

public const int WM_DELETEALL =WM_APP + 0x100;

int wnd=FindWindow(null,"TestMfcSendMsgExe");

Form1.SendMessage(wnd,WM_DELETEALL,0,55);





and MFC Exe is like this

#define WM_DELETEALL WM_APP + 0x100

BEGIN_MESSAGE_MAP(CTestMfcSendMsgExeDlg, CDialog)

ON_MESSAGE (WM_DELETEALL, OnDeleteAll)


END_MESSAGE_MAP()

LRESULT CTestMfcSendMsgExeDlg::OnDeleteAll(WPARAM wParam, LPARAM lParam)

{

MessageBox("Hello","Mayur",MB_OK);

}

I am getting the message box after using SendMessage() in c# exe but i want
to send text(string ) information.

I need help..

Regards,

Mayur.

First you need to correct your API signatures like this:

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string strClassName,string strWindowName);

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

When you need to send a string, you'll have to marshal the string to a native buffer and
pass it's pointer as lParam, something like this:

IntPtr hWnd = ...FindWindow(..);
string s = "Somestring";
IntPtr sNative = Marshal.StringToHGlobalAuto(s);
IntPtr result = SendMessage(hWnd,..,.., sNative);
Marshal.FreeHGlobal(sNative);
....

Willy.
 

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

Top