473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshalling strings: SendMessage & WndProc

Hi,

I am trying to write a 2 .net programs that can communicate via
windows messages. I have achieved this but only just passing ints.
When i try and pass a string, i get junk out. I guess this something
to do with the processes the pointers relate too. I have tried hunting
around for answer but i am unsure where to look, google has drawn a
blank. Any help to solve this problem or to point me in the direction
of some articles.

Would be great.

Thanks in advance.

James Burrow

Program 1
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SendMessage(IntPtr hwnd, int wMsg, int
wParam,[MarshalAs(UnmanagedType.LPStr)] ref string lParam);

[DllImport("user32.dll",EntryPoint="FindWindow",Cha rSet=CharSet.Auto)]
public static extern int FindWindow(string _WindowClassName, string
_WindowName);

private void button2_Click(object sender, System.EventArgs e)
{
int HWND = FindWindow(null,"Recieve Message");
Console.WriteLine(HWND);

if(HWND>0)
{
string sSendData = "JAMESTEST";
SendMessage(new IntPtr(HWND), WM_CURRENTSONG, sSendData.Length, ref
sSendData);
}
}
Program 2
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_CURRENTSONG)
{
int iLength = m.WParam.ToInt32();
string sTheString = Marshal.PtrToStringAuto(m.LParam);
Console.WriteLine("Length:{0} String:{1}",iLength,sTheString);
}

base.WndProc (ref m);
}
Nov 15 '05 #1
2 11516
Use WM_COPYDATA message to pass data between your applications
"James Burrow" <ja**********@hotmail.com> wrote in message
news:fa**************************@posting.google.c om...
Hi,

I am trying to write a 2 .net programs that can communicate via
windows messages. I have achieved this but only just passing ints.
When i try and pass a string, i get junk out. I guess this something
to do with the processes the pointers relate too. I have tried hunting
around for answer but i am unsure where to look, google has drawn a
blank. Any help to solve this problem or to point me in the direction
of some articles.

Would be great.

Thanks in advance.

James Burrow

Program 1
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SendMessage(IntPtr hwnd, int wMsg, int
wParam,[MarshalAs(UnmanagedType.LPStr)] ref string lParam);

[DllImport("user32.dll",EntryPoint="FindWindow",Cha rSet=CharSet.Auto)]
public static extern int FindWindow(string _WindowClassName, string
_WindowName);

private void button2_Click(object sender, System.EventArgs e)
{
int HWND = FindWindow(null,"Recieve Message");
Console.WriteLine(HWND);

if(HWND>0)
{
string sSendData = "JAMESTEST";
SendMessage(new IntPtr(HWND), WM_CURRENTSONG, sSendData.Length, ref
sSendData);
}
}
Program 2
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_CURRENTSONG)
{
int iLength = m.WParam.ToInt32();
string sTheString = Marshal.PtrToStringAuto(m.LParam);
Console.WriteLine("Length:{0} String:{1}",iLength,sTheString);
}

base.WndProc (ref m);
}

Nov 15 '05 #2
James,

You are right in your assumption. When you call SendMessage and pass a
value which is a pointer to another process, the address means something
completely different to the other process, as it is a different memory space
altogether.

So, you can use WM_COPYDATA, as another poster suggested, or you can use
some other techniques, such as remoting, to pass data between processes.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James Burrow" <ja**********@hotmail.com> wrote in message
news:fa**************************@posting.google.c om...
Hi,

I am trying to write a 2 .net programs that can communicate via
windows messages. I have achieved this but only just passing ints.
When i try and pass a string, i get junk out. I guess this something
to do with the processes the pointers relate too. I have tried hunting
around for answer but i am unsure where to look, google has drawn a
blank. Any help to solve this problem or to point me in the direction
of some articles.

Would be great.

Thanks in advance.

James Burrow

Program 1
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SendMessage(IntPtr hwnd, int wMsg, int
wParam,[MarshalAs(UnmanagedType.LPStr)] ref string lParam);

[DllImport("user32.dll",EntryPoint="FindWindow",Cha rSet=CharSet.Auto)]
public static extern int FindWindow(string _WindowClassName, string
_WindowName);

private void button2_Click(object sender, System.EventArgs e)
{
int HWND = FindWindow(null,"Recieve Message");
Console.WriteLine(HWND);

if(HWND>0)
{
string sSendData = "JAMESTEST";
SendMessage(new IntPtr(HWND), WM_CURRENTSONG, sSendData.Length, ref
sSendData);
}
}
Program 2
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_CURRENTSONG)
{
int iLength = m.WParam.ToInt32();
string sTheString = Marshal.PtrToStringAuto(m.LParam);
Console.WriteLine("Length:{0} String:{1}",iLength,sTheString);
}

base.WndProc (ref m);
}

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3046
by: Ed Sutton | last post by:
I need to do a custom sort on a TreeView. I have various object types associated with the TreeNode Tag property. I want to sort objects of the same type at the top of the list, other objects at...
0
2044
by: Bobby D | last post by:
I have two applications, one is sending a message, one is receiving. The sending app's code is as follows: ------------------------------------------------------------------------ private void...
7
606
by: GianPiero Andreis | last post by:
Hello All, let me pose a simple question about combobox and the CB_INSERTSTRING message. Suppose for instance that I already have a combobox handle, how can I declare and use the SendMessage...
3
5048
by: Max M. Power | last post by:
When I use the SendMessage API I can sucessfully send and receive a user defined message. When I use the PostMessage API I can NOT sucessfully send and receive the same user defined message. ...
4
15573
by: Sean | last post by:
I am trying to send a WM_COPYDATA message to another application in C#, ..NET 2.0. The other application receives the message, but only seems to see the first character of the string, does...
1
6243
by: Thomas Kehl | last post by:
Hello. I use the fallowing Functions to send Message from one Application to another. This is working correct on a 32bit System. But on a 64Bit System, the Target-Application will no received...
11
2674
by: Daniel Bass | last post by:
Greetings! I'm trying to call this method in a c# app... SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE DeviceHandle, unsigned char *pData, long max_length); So far I've got...
2
2131
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
2520
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.