gpt4 book ai didi

c# - 如何将非托管 IntPtr 类型转换为 C# 字符串?

转载 作者:IT王子 更新时间:2023-10-29 04:54:26 28 4
gpt4 key购买 nike

我是 C# 的新手(来自 native C++ 背景),我正在尝试编写一个小 UI 来打印 Windows 广播消息等。我已经覆盖了我的 C# 程序中的默认 WndProc 消息循环,如下所示:

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for operating system broadcasts.
switch (m.Msg)
{
case WM_SETTINGCHANGE:

this.richTextLog.Text += "WM_SETTINGCHANGE - lParam=" + m.LParam.ToString() + "\n";

break;
}
base.WndProc(ref m);
}

我想知道的是如何获得类型为 IntPtr 的 lParam 对象的字符串表示形式。它在 C++ 领域本质上是一个 void*,我可以以某种方式将它转换到 C# 中吗?据推测,这样做本质上是不安全的。

最佳答案

Marshal.PtrToStringAuto Method (IntPtr)

Allocates a managed String and copies all characters up to the first null character from a string stored in unmanaged memory into it.

关于c# - 如何将非托管 IntPtr 类型转换为 C# 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9175861/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com