简体   繁体   English

C# 如何在调用 P/Invoke Sendmessage 时捕获 WIN32 0XFFFF 异常

[英]C# How to catch WIN32 0XFFFF exception when calling P/Invoke Sendmessage

When I try to use unmanaged code in WPF.当我尝试在 WPF 中使用非托管代码时。 eg SendMessage(IntPtr hWnd, int Msg, int wParam, ref TOOLINFO toolInfo), this function may return the 0XFFFF for lpszText in TOOLINFO and application crash directly.例如 SendMessage(IntPtr hWnd, int Msg, int wParam, ref TOOLINFO toolInfo),这个 function 可能会返回 TOOLINFO 中 lpszText 的 0XFFFF 并且应用程序直接崩溃。 I have referred MSDN and found that it is a ERROR_ILLEGAL_CHARACTER error.我参考了 MSDN,发现这是一个 ERROR_ILLEGAL_CHARACTER 错误。 So I want to ask: How can I catch this kind of error in managed code, or how can I return TOOLINFO a good result.所以我想问:我怎样才能在托管代码中捕捉到这种错误,或者我怎样才能返回 TOOLINFO 一个好的结果。

    struct TOOLINFO
    {
        public int cbSize;
        public int uFlags;
        public IntPtr hwnd;
        public IntPtr uId;
        public RECT rect;
        public IntPtr hinst;
        [MarshalAs(UnmanagedType.LPStr)]
        public string lpszText;
        public IntPtr lParam;
    }
[DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, ref TOOLINFO toolInfo);

What message are you sending?你要发送什么信息?

Still the solution is easy:解决方案仍然很简单:

public string lpszText; 

change it to:将其更改为:

public IntPtr lpszText; 

and then marshal the string with Marshal.PtrToStringAuto() (inside a try / catch )然后用Marshal.PtrToStringAuto()编组字符串(在try / catch内)

But I see in the description of ToolInfo that YOU have to allocate the buffer.但是我在ToolInfo的描述中看到您必须分配缓冲区。 You could try with a StringBuilder() with preallocated length:您可以尝试使用预先分配长度的StringBuilder()

public StringBuilder lpszText;

and then BEFORE sending the message然后在发送消息之前

tt.lpszText = new StringBuilder(200);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#P / Invoke中表示Win32类型? - How are Win32 types represented in C# P/Invoke? 使用P / Invoke时如何将Win32类型映射到C#类型? - How to map Win32 types to C# types when using P/Invoke? C#P / Invoke Win32函数RegQueryInfoKey - C# P/Invoke Win32 function RegQueryInfoKey 在C#richtextbox中调用Win32 API SendMessage复制所选文本将少返回一个字符 - Calling Win32 API SendMessage in C# richtextbox to copy selected text returns one character less 当我使用 Win32 API 和 P/Invoke 更改 C# WinForms ComboBox.SelectedIndex 时,如何触发 SelectedIndexChanged? - How do I make SelectedIndexChanged trigger when I change C# WinForms ComboBox.SelectedIndex using Win32 API and P/Invoke? 在C#中调用Win32 EnumThreadWindows() - Calling Win32 EnumThreadWindows() in C# C# 应用程序中的 Win32 异常 - Win32 exception in C# application 如何从C#处理非托管Win32异常 - How to handle unmanaged Win32 exception from C# 访问 C# 中的 Process.MainModule.FileName 时如何避免 Win32 异常? - How to avoid a Win32 exception when accessing Process.MainModule.FileName in C#? 如何捕获“在AppName [procId]中发生未处理的win32异常。” - How to catch “Unhandled win32 exception occured in AppName [procId].”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM