Click to See Complete Forum and Search --> : Send a string between processes


BlackAPI
March 22nd, 2006, 03:45 PM
Hi everybody
Please help in this situation, I want to send a string from one process to another, of coz the handles are known. Write and read a file is simple method but it may cause conflict and works slowly.
Thanks in advance !

golanshahar
March 22nd, 2006, 04:06 PM
There are several ways look here: Interprocess Communications (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipc/base/interprocess_communications.asp).

You can also use ::SendMessage() with WM_COPYDATA.

Cheers

sreehari
March 23rd, 2006, 01:28 AM
Some more...
Interprocess communication tutorial (http://www.codeproject.com/threads/ipc_tute.asp)
Interprocess Communication using Shared Memory (http://www.codeproject.com/threads/sharedmemipc.asp)
Using Mailslots for Interprocess Communication (http://www.codeproject.com/cpp/mailslots.asp)

MSDN Article
INFO: Interprocess Communication on Windows Platforms (http://support.microsoft.com/default.aspx?scid=kb;en-us;95900#XSLTH3121121122120121120120)

BlackAPI
March 23rd, 2006, 03:53 AM
Itried the SendMessage with this code
>>>>>>>>>>>>>>>>>>>>>>
//Sendding Message In Sender
typedef struct tagMYREC
{
char s1[50];

} MYREC;
MYREC MyRec;
COPYDATASTRUCT MyCDS;

...................
............
if (MyRec.s1[0]!=' ')
{
HWND hwnd = FindWindow(NULL, "My Program)");

if (hwnd != NULL)
{
MyCDS.cbData=NULL;
MyCDS.dwData = sizeof(MyRec);
MyCDS.lpData = &MyRec;
SendMessage(hwnd,WM_COPYDATA,NULL,(LPARAM) (LPVOID)&MyCDS);
}
}
>>>>>>>>>>>>>>>
Receiver is writen in C#
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//In Receiver
[StructLayout(LayoutKind.Sequential)]
private class MYDATA
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string word;

}

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COPYDATA)
{

MYDATA data = new MYDATA();
data = (MYDATA)Marshal.PtrToStructure(m_objData,typeof(MYDATA));
MessageBox.Show("I receive Message");

}
else
{
base.WndProc (ref m);
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>

SendMessage works but the the retured string always ="2" How to correct it?

Thanks in advance!

golanshahar
March 23rd, 2006, 05:01 AM
Read the MSDN:

cbData Specifies the size, in bytes, of the data pointed to by the lpData member.


thats mean instead of the code:

MyCDS.cbData=NULL;
MyCDS.dwData = sizeof(MyRec);
MyCDS.lpData = &MyRec;


Should be:

MyCDS.cbData=sizeof(MyRec);
MyCDS.dwData = NULL;
MyCDS.lpData = &MyRec;


Try again ;)

Cheers

BlackAPI
March 23rd, 2006, 07:44 AM
Read the MSDN:


thats mean instead of the code:

MyCDS.cbData=NULL;
MyCDS.dwData = sizeof(MyRec);
MyCDS.lpData = &MyRec;


Should be:

MyCDS.cbData=sizeof(MyRec);
MyCDS.dwData = NULL;
MyCDS.lpData = &MyRec;


Try again ;)

Cheers
YEs it is my donkey error but after correction the string ="" , I think problem with Marshal.ToStructure () function ,if you have experiences with C# please help!

onlyfish
March 24th, 2006, 07:50 AM
I have a idea, but it may be troublesome.
I think you can do it with SOCKET, if you send string between PROCESS regularly.

golanshahar
March 24th, 2006, 09:02 AM
YEs it is my donkey error but after correction the string ="" , I think problem with Marshal.ToStructure () function ,if you have experiences with C# please help!

I am not familiar with that function.
Just a suggestion write a simple MFC or VC app (not C#) and see that you able to get the data in WM_COPYDATA. If it works, please redirect the question to the C# forum.

Cheers