P
pedrito
I've got an app that takes a URL as an argument. If a second instance of my
application is started with a URL argument, instead of starting up, it sends
the URL to the first instance.
WM_COPYDATA seemed the best way to handle this, but my string appears to be
getting corrupted in transit.
The code for sending the string is as follows:
COPYDATA cd = new COPYDATA();
cd.dwData = 0;
cd.cbData = (uint) (_startupUrl.Length * 2);
cd.lpData = Marshal.StringToHGlobalUni(_startupUrl);
IntPtr lpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cd));
Marshal.StructureToPtr(cd, lpPtr, true);
SendMessage(process.MainWindowHandle, 0x004A, 0, lpPtr);
COPYDATA cd2 = (COPYDATA) Marshal.PtrToStructure(lpPtr, typeof(COPYDATA));
string strCheck = Marshal.PtrToStringUni(cd2.lpData);
Debug.WriteLine("Sent URL: " + strCheck);
Marshal.FreeHGlobal(lpPtr);
Now, the Debug.WriteLine shows that the string is correct at the time of
sending.
On the receiving end, my code is pretty much like the 2 lines before the
Debug.WriteLine. When I get the WM_COPYDATA message, the code is:
COPYDATA cd = (COPYDATA) Marshal.PtrToStructure(m.LParam, typeof(COPYDATA));
string url = Marshal.PtrToStringUni(cd.lpData);
At this point, however url generally has about 5 unicode characters appended
to it (I don't know what they are, they just show up as those empty box
characters).
Anyone have any idea what's going wrong here?
Thanks
application is started with a URL argument, instead of starting up, it sends
the URL to the first instance.
WM_COPYDATA seemed the best way to handle this, but my string appears to be
getting corrupted in transit.
The code for sending the string is as follows:
COPYDATA cd = new COPYDATA();
cd.dwData = 0;
cd.cbData = (uint) (_startupUrl.Length * 2);
cd.lpData = Marshal.StringToHGlobalUni(_startupUrl);
IntPtr lpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cd));
Marshal.StructureToPtr(cd, lpPtr, true);
SendMessage(process.MainWindowHandle, 0x004A, 0, lpPtr);
COPYDATA cd2 = (COPYDATA) Marshal.PtrToStructure(lpPtr, typeof(COPYDATA));
string strCheck = Marshal.PtrToStringUni(cd2.lpData);
Debug.WriteLine("Sent URL: " + strCheck);
Marshal.FreeHGlobal(lpPtr);
Now, the Debug.WriteLine shows that the string is correct at the time of
sending.
On the receiving end, my code is pretty much like the 2 lines before the
Debug.WriteLine. When I get the WM_COPYDATA message, the code is:
COPYDATA cd = (COPYDATA) Marshal.PtrToStructure(m.LParam, typeof(COPYDATA));
string url = Marshal.PtrToStringUni(cd.lpData);
At this point, however url generally has about 5 unicode characters appended
to it (I don't know what they are, they just show up as those empty box
characters).
Anyone have any idea what's going wrong here?
Thanks