如何将对象实例发送到 WndProc
我正在使用描述一些状态和值的自定义类:
class MyClass
{
int State;
String Message;
IList<string> Values;
}
由于应用程序体系结构,表单交互使用消息及其基础结构(SendMessage/PostMessage、WndProc)。 问题是 - 如何使用 SendMessage/PostMessage 将 MyClass 的实例发送到 WndProc? 在我的代码中 PostMessage 是这样定义的:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
所以,我需要在我的自定义消息号下面以某种方式发送和一个 MyClass 实例,所以在 WndProc 中我可以将它用于逻辑需求。 有可能吗?
I'm using my custom class that describes some states and values:
class MyClass
{
int State;
String Message;
IList<string> Values;
}
Because of application architecture, for forms interaction is used messages and its infrastructure (SendMessage/PostMessage, WndProc).
The question is - how, using SendMessage/PostMessage, to send an instance of MyClass to WndProc?
In my code PostMessage is defined next way:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
So, I need that below my custom message number somehow to send and an MyClass instance, so in WndProc I could use it for logics needs.
It's possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你将无法做到这一点。
托管语言中的指针没有任何意义,它们通常在不再被引用时被重新定位和杀死。
好吧,也许你能够通过不安全的代码和固定指针来实现某种方式(在过程中)工作的东西,但这将是你的厄运。
如果您只想进行进程内通信,那么请注意跨线程通信的影响。
如果您需要跨进程通信,请参阅此线程:
C# 中的 IPC 机制 - 用法和最佳实践
编辑:
通过SendMessage发送uniqueID来获取序列化对象。
我不建议这样做,因为它很容易被黑客攻击并且容易出现错误,但您请求:
发送消息时:
在 WndProc 中接收时:
对于代码中的拼写错误,我们深表歉意,我正在临时写这篇文章,无法测试......
You won't be able to do that.
Pointers in a managed language mean nothing, they are often relocated and killed when no longer referenced.
Well maybe you would be able to achieve something that kinda works this way (in process), with unsafe code and pinned pointers but that will be your doom.
If you want only inprocess communication then beware of cross thread communication implications.
If you need cross process communication see this thread:
IPC Mechanisms in C# - Usage and Best Practices
Edit:
Sending uniqueID through SendMessage to fetch serialized object.
I don't advise to to this since it is hacking and bug prone but you requested:
when sending the message:
when receiving in WndProc:
Sorry for typos in the code, I'm writing this ad hoc and cannot test...