473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing String to other Applications with SendMessage,...

Hi,

i want to emulate a (synchronous) BroadCastSystem Message
with EnumWindows and SendMessage. I dont want to use
the BroadcastSystem Message 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.PtrToSt ringAnsi,
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.AllocHG lobal(Encoding. ASCII.GetByteCo unt(this.textBo xGGIPAIPAddress .Text));
ptrGUID =
Marshal.AllocHG lobal(Encoding. ASCII.GetByteCo unt(this.guidAp plicationGUID.T oString()));

ptrIPAddress = Marshal.StringT oHGlobalAnsi(th is.textBoxGGIPA IPAddress.Text) ;
ptrGUID = Marshal.StringT oHGlobalAnsi(th is.guidApplicat ionGUID.ToStrin g());

//(IntPtr,uint, IntPtr,IntPtr)
SendMessage(hWn d, (uint)WindowsMe ssages.WM_USER + 1111, ptrGUID ,
ptrIPAddress );

Marshal.FreeHGl obal(ptrIPAddre ss);
Marshal.FreeHGl obal(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 .WindowsMessage s.WM_USER + 1111)
{
String s = Marshal.PtrToSt ringAnsi(m.LPar am);
MessageBox.Show (s);
}
base.WndProc(re f 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 12617
"Kerem Gümrükcü" <ka*******@hotm ail.comwrote in message
news:uA******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

i want to emulate a (synchronous) BroadCastSystem Message
with EnumWindows and SendMessage. I dont want to use
the BroadcastSystem Message 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.PtrToSt ringAnsi,
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) BroadCastSystem Message
with EnumWindows and SendMessage.
Seems like you're trying to enumate SendMessage(HWN D_BROADCAST, …) instead.
Why not use it?

(H) Serge
Dec 4 '07 #4
Hi Serge,
>Seems like you're trying to enumate SendMessage(HWN D_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.AllocH Global(Encoding .ASCII.GetByteC ount(this.textB oxGGIPAIPAddres s.Text));
ptrGUID =
Marshal.AllocH Global(Encoding .ASCII.GetByteC ount(this.guidA pplicationGUID. ToString()));

ptrIPAddress = Marshal.StringT oHGlobalAnsi(th is.textBoxGGIPA IPAddress.Text) ;
ptrGUID = Marshal.StringT oHGlobalAnsi(th is.guidApplicat ionGUID.ToStrin g());
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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct APP_IP_GUID_INF ORMATION
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
public string ApplicationGUID ;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
public string IPAddress;
}
[StructLayout(La youtKind.Sequen tial)]
public struct CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr pData;
}
IntPtr ptrAIGI = IntPtr.Zero;
IntPtr ptrCDS = IntPtr.Zero;
IntPtr hWnd = IntPtr.Zero;

GGIPAWin32APICl ass.APP_IP_GUID _INFORMATION aigi;
aigi.Applicatio nGUID = this.guidApplic ationGUID.ToStr ing();
aigi.IPAddress = this.textBoxGGI PAIPAddress.Tex t;

ptrAIGI =
Marshal.AllocHG lobal(Marshal.S izeOf(typeof(GG IPAWin32APIClas s.APP_IP_GUID_I NFORMATION)));
Marshal.Structu reToPtr(aigi, ptrAIGI, false);

GGIPAWin32APICl ass.CopyDataStr uct cds;
cds.dwData = this.Handle.ToI nt32();
cds.cbData =
Marshal.SizeOf( typeof(GGIPAWin 32APIClass.APP_ IP_GUID_INFORMA TION));
cds.pData = ptrAIGI;

ptrCDS =
Marshal.AllocHG lobal(Marshal.S izeOf(typeof(GG IPAWin32APIClas s.CopyDataStruc t)));
Marshal.Structu reToPtr(cds, ptrCDS, false);

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

hWnd = new IntPtr(Convert. ToInt32(Clienth Wnd.Key.ToStrin g()));
HandleRef hWndRef = new HandleRef(this, hWnd);
GGIPAWin32APICl ass.SendMessage (hWndRef,
(uint)GGIPAWin3 2APIClass.Windo wsMessages.WM_C OPYDATA, this.Handle, ptrCDS)
/*...........*/

Marshal.FreeHGl obal(ptrAIGI);
Marshal.FreeHGl obal(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*******@hotm ail.comwrote in message
news:Or******** *****@TK2MSFTNG P04.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

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

Similar topics

5
5848
by: Adam Clauss | last post by:
I am attempting to set the text on a richedit control in another application using EM_SETTEXTEX: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_settextex.asp I have the following: public static extern int SendMessage(IntPtr...
9
2291
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 receiving it, would that change the answer to the above?
2
12778
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 can train the mouse on the correct locations. oh the other application may or may not be dotnet Thanks
1
1675
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 problems reading it. How was it done to create the String / PIDL in a common memoryspace? What I want to do is: string szFolder = @"C:\temp\";
14
1822
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
2513
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 string in C# Thanks.
2
12519
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
4234
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 question is how to elegantly pass a command line parameter from Instance_B to Instance_A where Instance_A was running prior to Instance_B. For...
11
1984
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, that won't neccessarily be on the same machine. This will be my first attempt at this, and I'm not sure if this falls under COM, or ActiveX. ...
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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 we have to send another system
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.