如何将对象实例发送到 WndProc

发布于 2024-11-17 07:57:14 字数 559 浏览 5 评论 0原文

我正在使用描述一些状态和值的自定义类:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2024-11-24 07:57:14

你将无法做到这一点。
托管语言中的指针没有任何意义,它们通常在不再被引用时被重新定位和杀死。
好吧,也许你能够通过不安全的代码和固定指针来实现某种方式(在过程中)工作的东西,但这将是你的厄运。

如果您只想进行进程内通信,那么请注意跨线程通信的影响。

如果您需要跨进程通信,请参阅此线程:
C# 中的 IPC 机制 - 用法和最佳实践

编辑:

通过SendMessage发送uniqueID来获取序列化对象。
我不建议这样做,因为它很容易被黑客攻击并且容易出现错误,但您请求:

发送消息时:

IFormatter formatter = new BinaryFormatter();
string filename = GetUniqueFilenameNumberInFolder(@"c:\storage"); // seek a freee filename number -> if 123.dump is free return 123 > delete those when not needed anymore
using (FileStream stream = new FileStream(@"c:\storage\" + filename + ".dump", FileMode.Create))
{
   formatter.Serialize(stream, myClass);
}
PostMessage(_window, MSG_SENDING_OBJECT, IntPtr.Zero, new IntPtr(int.Parse(filename)));

在 WndProc 中接收时:

if (msg == MSG_SENDING_OBJECT)
{
    IFormatter formatter = new BinaryFormatter();
    MyClass myClass;
    using (FileStream stream = new FileStream(@"c:\storage\" + lParam.ToInt32().ToString() + ".dump", FileMode.Open))
    {
        myClass = (MyClass)formatter.Deserialize(stream);
    }
    File.Delete(@"c:\storage\" + lParam.ToInt32().ToString() + ".dump");
}

对于代码中的拼写错误,我们深表歉意,我正在临时写这篇文章,无法测试......

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:

IFormatter formatter = new BinaryFormatter();
string filename = GetUniqueFilenameNumberInFolder(@"c:\storage"); // seek a freee filename number -> if 123.dump is free return 123 > delete those when not needed anymore
using (FileStream stream = new FileStream(@"c:\storage\" + filename + ".dump", FileMode.Create))
{
   formatter.Serialize(stream, myClass);
}
PostMessage(_window, MSG_SENDING_OBJECT, IntPtr.Zero, new IntPtr(int.Parse(filename)));

when receiving in WndProc:

if (msg == MSG_SENDING_OBJECT)
{
    IFormatter formatter = new BinaryFormatter();
    MyClass myClass;
    using (FileStream stream = new FileStream(@"c:\storage\" + lParam.ToInt32().ToString() + ".dump", FileMode.Open))
    {
        myClass = (MyClass)formatter.Deserialize(stream);
    }
    File.Delete(@"c:\storage\" + lParam.ToInt32().ToString() + ".dump");
}

Sorry for typos in the code, I'm writing this ad hoc and cannot test...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文