473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing String to other Applications with SendMessage,...

Hi,

i want to emulate a (synchronous) BroadCastSystemMessage
with EnumWindows and SendMessage. I dont want to use
the BroadcastSystemMessage because it needs the SE_TCB_NAME
Privilege (you dont have this in normal). So i decided to use the
EnumWindows with SendMessage. What i try here is to pass a
WM_USER+1111 with LParam and WParam, pointing to strings.
But when i try to get the strings at the other end with
Marshal.PtrToStringAnsi,
the String itself is empty. I dont know what the mistake here is, so
a little code:

IntPtr ptrIPAddress = IntPtr.Zero;
IntPtr ptrGUID = IntPtr.Zero;

ptrIPAddress =
Marshal.AllocHGlobal(Encoding.ASCII.GetByteCount(t his.textBoxGGIPAIPAddress.Text));
ptrGUID =
Marshal.AllocHGlobal(Encoding.ASCII.GetByteCount(t his.guidApplicationGUID.ToString()));

ptrIPAddress = Marshal.StringToHGlobalAnsi(this.textBoxGGIPAIPAdd ress.Text);
ptrGUID = Marshal.StringToHGlobalAnsi(this.guidApplicationGU ID.ToString());

//(IntPtr,uint, IntPtr,IntPtr)
SendMessage(hWnd, (uint)WindowsMessages.WM_USER + 1111, ptrGUID ,
ptrIPAddress );

Marshal.FreeHGlobal(ptrIPAddress);
Marshal.FreeHGlobal(ptrGUID);

At the other end i try this, but nothing is in there:

protected override void WndProc(ref Message m)
{
if (m.Msg == (uint) WindowsAPIClass.WindowsMessages.WM_USER + 1111)
{
String s = Marshal.PtrToStringAnsi(m.LParam);
MessageBox.Show(s);
}
base.WndProc(ref m);
}
How to do this? I cant use remoting or stuff like that, because
the message should be available for any application running on
the System,...it seems that the global memory isnt really global,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 3 '07 #1
14 12585
"Kerem Gümrükcü" <ka*******@hotmail.comwrote in message
news:uA**************@TK2MSFTNGP06.phx.gbl...
Hi,

i want to emulate a (synchronous) BroadCastSystemMessage
with EnumWindows and SendMessage. I dont want to use
the BroadcastSystemMessage because it needs the SE_TCB_NAME
Privilege (you dont have this in normal). So i decided to use the
EnumWindows with SendMessage. What i try here is to pass a
WM_USER+1111 with LParam and WParam, pointing to strings.
But when i try to get the strings at the other end with
Marshal.PtrToStringAnsi,
the String itself is empty. I dont know what the mistake here is, so
a little code:
You can't pass a pointer cross process. Each application has it own
completely seperate memory space. What is a valid pointer for your app is
not a valid pointer for another. You should look at the WM_COPY_DATA message
as it will copy your string data cross-process and is quite easy to use.

Michael
Dec 3 '07 #2
Hi Michael,

yes i know this but I still tried to find a way. I know
the WM_COPYDATA but it would be great if
this would be possible with some sort of WM_USER.
But since processes are exclusive i think i have to
go the WM_COPYDATA way,...

Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 3 '07 #3
Hello,
i want to emulate a (synchronous) BroadCastSystemMessage
with EnumWindows and SendMessage.
Seems like you're trying to enumate SendMessage(HWND_BROADCAST, …) instead.
Why not use it?

(H) Serge
Dec 4 '07 #4
Hi Serge,
>Seems like you're trying to enumate SendMessage(HWND_BROADCAST, .) instead.
Why not use it?
i need a synchronous way in sending the message. I also could use
PostMessage but it does place the message into the messageQ and
returns in the same moment. Send Message does not (if you use it
with every window handle obtained from EnumWindows and not
with HWND_BROADCAST,)...

Thanks anyway,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 4 '07 #5
>ptrIPAddress =
>Marshal.AllocHGlobal(Encoding.ASCII.GetByteCount( this.textBoxGGIPAIPAddress.Text));
ptrGUID =
Marshal.AllocHGlobal(Encoding.ASCII.GetByteCount( this.guidApplicationGUID.ToString()));

ptrIPAddress = Marshal.StringToHGlobalAnsi(this.textBoxGGIPAIPAdd ress.Text);
ptrGUID = Marshal.StringToHGlobalAnsi(this.guidApplicationGU ID.ToString());
By overwriting the pointers you're leaking the memory you first
allocated with AllocHGlobal. Those calls seem completely unnecessary.

>How to do this? I cant use remoting or stuff like that, because
the message should be available for any application running on
the System,...it seems that the global memory isnt really global,...
If you want real global memory you should look at memory mapped files.
But it may be easier to use WM_COPYDATA as Michael suggested.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 4 '07 #6
Hi Mattias,
>By overwriting the pointers you're leaking the memory you first
allocated with AllocHGlobal. Those calls seem completely unnecessary.
Yes, this was the call from a earlier try, i removed it....
>If you want real global memory you should look at memory mapped files.
But it may be easier to use WM_COPYDATA as Michael suggested.
Memory mapped files are in my case and also the approach of global
available data not that secure since want to make the thing more secure.
I use encrypted data that must be transfered,...

Thanks for the reply,...
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 4 '07 #7
>Memory mapped files are in my case and also the approach of global
>available data not that secure since want to make the thing more secure.
I use encrypted data that must be transfered,...
Sorry I'm not following. Memory mapped files seems lie a much more
secure solution in that case, since you can specify a security
descriptor for the file mapping and have complete control over who can
read and write from it.

Compare that to broadcasting data with window messages where any
message recipient gets to read the data.

Whether or not the data is encrypted is irrelevant to your
communication mechanism.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 4 '07 #8
Hi Mattias,

i solved it another way, but my problem is passing
the data between two windows does not work. I get
garbage at the other end when passing the data with
the WM_COPYDATA Message.

Here is Code:

//structure to pass
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct APP_IP_GUID_INFORMATION
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string ApplicationGUID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string IPAddress;
}
[StructLayout(LayoutKind.Sequential)]
public struct CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr pData;
}
IntPtr ptrAIGI = IntPtr.Zero;
IntPtr ptrCDS = IntPtr.Zero;
IntPtr hWnd = IntPtr.Zero;

GGIPAWin32APIClass.APP_IP_GUID_INFORMATION aigi;
aigi.ApplicationGUID = this.guidApplicationGUID.ToString();
aigi.IPAddress = this.textBoxGGIPAIPAddress.Text;

ptrAIGI =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GGIPAWi n32APIClass.APP_IP_GUID_INFORMATION)));
Marshal.StructureToPtr(aigi, ptrAIGI, false);

GGIPAWin32APIClass.CopyDataStruct cds;
cds.dwData = this.Handle.ToInt32();
cds.cbData =
Marshal.SizeOf(typeof(GGIPAWin32APIClass.APP_IP_GU ID_INFORMATION));
cds.pData = ptrAIGI;

ptrCDS =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GGIPAWi n32APIClass.CopyDataStruct)));
Marshal.StructureToPtr(cds, ptrCDS, false);

/*...........*/

hWnd = new IntPtr(Convert.ToInt32(ClienthWnd.Key.ToString())) ;
HandleRef hWndRef = new HandleRef(this,hWnd);
GGIPAWin32APIClass.SendMessage(hWndRef,
(uint)GGIPAWin32APIClass.WindowsMessages.WM_COPYDA TA, this.Handle, ptrCDS)
/*...........*/

Marshal.FreeHGlobal(ptrAIGI);
Marshal.FreeHGlobal(ptrCDS);
The dwData and cbData at the other end are ok, but the pointer to the data
is garbage,...why.
Any idea? All pinvoke-functions and data is ASCII or ANSI,...
Thanks in advance,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Dec 5 '07 #9
"Kerem Gümrükcü" <ka*******@hotmail.comwrote in message
news:Or*************@TK2MSFTNGP04.phx.gbl...
The dwData and cbData at the other end are ok, but the pointer to the data
is garbage,...why.
Any idea? All pinvoke-functions and data is ASCII or ANSI,...
The pointer is garbage or what it points to is garbage? Are you sure you're
creating the data correctly? Try sending a byte array with a few arbitrary
values in it first. Also try using the rtlMoveMemory api to copy your data
to a byte array into your app as a test (ie not through SendMessage).

Michael
Dec 5 '07 #10
Hi Michael,

when i send the Data with WM_COPYDATA, the receiving
application has the right cbData and dwData values send from
my application, but pData is nothing else than random application.
I dont know whether the mistake lies on the sender or the receiver
side, but i think the mistake is on the sender side. I debugged my
reciver applications CopyDataStruct with VC++ (receiver is a
native app) and also a mannaged app. Bott apps pData is useless.
Dont as me why! Except the cbData and dwData nothing males sense.

Any ideas. Do i marshal the data the right way? Are my pinvoke
signatures ok?

Data should be TCHAR lpIPAddress[128] and
TCHAR lpApplicationGUID[128] at the other end...
But its something like "@/!29d)383Rgen%2%4"ß?0".
Pointing to "i dont know" data,...
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 5 '07 #11
"Kerem Gümrükcü" <ka*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi Michael,

when i send the Data with WM_COPYDATA, the receiving
application has the right cbData and dwData values send from
my application, but pData is nothing else than random application.
I dont know whether the mistake lies on the sender or the receiver
side, but i think the mistake is on the sender side. I debugged my
reciver applications CopyDataStruct with VC++ (receiver is a
native app) and also a mannaged app. Bott apps pData is useless.
Dont as me why! Except the cbData and dwData nothing males sense.
How do you know it's your pointer that is rubbish? The pointer will be
changed from one app to the other. It's possible that the data is incorrect
*before* it is sent and the SendMessage is working correctly. Check this
first using the 2 suggestion in my previous post.

Michael
Dec 5 '07 #12
Hi Michael,

i am desperately looking for a Memory Window
in my VStudio 2005 Standard Edition. Did i miss
something or do i dont have one there? I dont want
to debug it the native way or use some external
debugger to have a in-depth look into the pointers
memory address. I have a symbol file and i can use
some external debugger like Smidgeonsofts PEDebugger
or some other *.pdb able debugger.
But this is not the point here. When i do a MessageBox
on the Structs Members "IPAddress" and "ApplicationGUID"
i get valid Strings displayed. Maybe something is corrupted after
Marshal.StructToPtr,...i cant see the memory pointers address,
thats the point...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 5 '07 #13
"Kerem Gümrükcü" <ka*******@hotmail.comwrote in message
news:O4*************@TK2MSFTNGP06.phx.gbl...
Hi Michael,

i am desperately looking for a Memory Window
in my VStudio 2005 Standard Edition. Did i miss
something or do i dont have one there?
Try Ctrl + Alt + M and then push 1. Otherwise it should be on a menu
somewhere.
I dont want
to debug it the native way or use some external
debugger to have a in-depth look into the pointers
memory address. I have a symbol file and i can use
some external debugger like Smidgeonsofts PEDebugger
or some other *.pdb able debugger.
My suggestion was to just copy the data to a byte array and have a look at
what's in the byte array. If that has what you expect then you are half way
there. Although looking at the memory window is probably easier.

The other idea is to create a small amount of arbitrary data (again, use a
byte array) and see if that gets copied across ok.

Michael
Dec 5 '07 #14
Hi Michael,

i was missing the Memory window and the command
you suggested didnt work. So i did a raw debugging with
ollydbg and a raw disassembly of the memory and looked
at the memory pointing from the CopyDataStruct.
Everything was fine, but i made a wrong calculation at the
other end receiving the WM_COPYDATA. The size and
the offset to the data was wrong, so it was clear the i
pointed at random process data. Problem solved,..everybody
is happy!
And now i found this, but too late...(sigh)

http://msdn2.microsoft.com/en-us/lib...3e(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...51(VS.80).aspx

Will be usefull the next time!
Visual Studio: Every Day a new experience,...
Thank you very much,...

Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 5 '07 #15

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

Similar topics

5
by: Adam Clauss | last post by:
I am attempting to set the text on a richedit control in another application using EM_SETTEXTEX:...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
2
by: wesmanjunk | last post by:
Does anyone know how to generate mouse click in another application? using C# and Windows XP.. The Idea it to click ok buttons on a form where you know the position of all the buttons, or you...
1
by: Daniel Halan | last post by:
Hello, I want to send a SendMessage(hwnd,...) to an another application containing eighter a "String" or a PIDL... I know that the pointer lives in its own process so the other app will have...
14
by: Erik | last post by:
Hi, i'm trying to do this : #include <stdlib.h> #include <stdio.h> #define FILE "/tmp/myfile" #define USERS_LIST "/tmp/userslist" int main() { //open file
2
by: Gary | last post by:
1. I am using SendMessage (HWND_BROADCAST, WM_FONTCHANGE, 0, (LPARAM)pFileName); in C++ side 2. In WndProc, I wanted to get the fileName from m.lparam, which is a IntPtr. How can I convert it to a...
2
by: Mayur | last post by:
I tried followinf but working fine fo int but how to do it for string using user custome message in c# public static extern int FindWindow(string strClassName,string strWindowName);
2
by: william.w.oneill | last post by:
I have an application that takes a few command line parameters. As recommended by others in this group, I'm using a named mutex to ensure that only one instance of the application is running. My...
11
by: MikeY | last post by:
Perhaps someone can point me in the right direction in finding code, or finding reading information on how to pass information (ie. ArrayList, string, etc) between two running window applications,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.