问题描述
我想用EnumWindows和SendMessage模拟(同步)BroadCastSystemMessage
。我不想使用
的BroadcastSystemMessage因为它需要SE_TCB_NAME
特权(你没有正常情况)。所以我决定使用带有SendMessage的
EnumWindows。我在这里尝试的是传递一个
WM_USER + 1111与LParam和WParam,指向字符串。
但是当我尝试用另一端的字符串获取
Marshal.PtrToStringAnsi,
String本身为空。我不知道这里的错误是什么,所以
a小代码:
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);
在另一端我尝试这个,但没有任何东西在那里:
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);
}
如何做到这一点?我不能使用远程处理或类似的东西,因为
消息应该可用于运行在系统上的任何应用程序,......似乎全局内存不是真的全球,...
问候
Kerem
-
-----------------------
BesteGrüsse/致敬/ Votre bien devoue
KeremGümrükcü
Microsoft Live Space:
最新的开源项目:
-----------------------
此回复按原样提供,不附带任何明示或暗示的保证。
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."
推荐答案
你不能通过指针交叉过程。每个应用程序都拥有它自己的完全独立的内存空间。什么是您的应用程序的有效指针是
不是另一个的有效指针。您应该查看WM_COPY_DATA消息
,因为它将复制您的字符串数据跨进程并且非常易于使用。
Michael
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
看起来你想尝试使用SendMessage(HWND_BROADCAST,a?|)。
为什么不使用它?
(H)Serge
Seems like you''re trying to enumate SendMessage(HWND_BROADCAST, a?|) instead.
Why not use it?
(H) Serge
这篇关于使用SendMessage将字符串传递给其他应用程序,...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!