很抱歉我太天真了,这是我第一次处理DLL。我一直试图在一个ShellAboutA应用程序上运行C#,并且完全不知道如何运行。我在谷歌上搜索了几个问题,并想出了这段代码
[DllImport("shell32.dll")]
public static extern Int32 ShellAboutA(
IntPtr? hWnd,
IntPtr? szApp,
IntPtr? szOtherStuff,
UInt64? hIcon);
ShellAboutA(null, null, null, null);但一旦我运行它,它就犯了错误
System.Runtime.InteropServices.MarshalDirectiveException: 'Cannot marshal 'parameter #1': Generic types cannot be marshaled.'(我不仅不知道如何使用DLL,而且也不知道这意味着什么)
我猜可能是因为它们都是空的。我再次检查了文档和所有内容,但是szApp是NULLable,所以我尝试了下一个函数
string _str = "test string";
Int64 _int = Convert.ToInt64(_str, 16);
IntPtr test = new IntPtr(_int);
ShellAboutA(null, test, null, null);_int在System.FormatException: 'Could not find any recognizable digits.'上失败了,不管我在谷歌搜索了多少之后,我都没有找到解决方案。
发布于 2022-07-31 00:55:17
您的现有代码有很多不同的问题,所以我将向您展示它的外观。
注意,ShellAboutA是函数的ASCII版本,ShellAboutW是Unicode版本。您可以让C#自动映射它,但最好指定它,现在您应该始终使用Unicode。
[DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern int ShellAboutW(
IntPtr hWnd,
string szApp,
string szOtherStuff,
IntPtr hIcon);你这样叫它
ShellAboutW(IntPtr.Zero, "hello#whats up", null, IntPtr.Zero)如果有必要,第一个参数是父窗口的Handle。
如果有必要,最后一个参数是图标的Handle。例如,要使用图标,可以使用GDI+加载它。
using (var icon = new Icon(filePath))
{
ShellAboutW(IntPtr.Zero, "hello#whats up", null, icon.Handle);
}https://stackoverflow.com/questions/73175801
复制相似问题