微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果一些参数只是inttypes,ShellExecute不起作用

我有下面的代码是启动任何.exe(在这个例子中它的notepad.exe)。 但是这个代码不工作。 虽然没有编译问题。

[DllImport("shell32.dll",CharSet = CharSet.Auto)] static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo); public static void exev() { SHELLEXECUTEINFO info = new SHELLEXECUTEINFO(); info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info); info.lpVerb = "open"; info.lpFile = "c:\windows\notepad.exe"; info.nShow = 5; info.fMask = 0x440; info.hwnd = IntPtr.Zero; ShellExecuteEx(ref info); } } [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Auto)] public struct SHELLEXECUTEINFO { public int cbSize; public uint fMask; public IntPtr hwnd; [MarshalAs(UnmanagedType.LPTStr)] public string lpVerb; [MarshalAs(UnmanagedType.LPTStr)] public string lpFile; [MarshalAs(UnmanagedType.LPTStr)] public string lpParameters; [MarshalAs(UnmanagedType.LPTStr)] public string lpDirectory; public int nShow; public int hInstApp; public int lpIDList; [MarshalAs(UnmanagedType.LPTStr)] public string lpClass; public int hkeyClass; public uint dwHotKey; public int hIcon; public int hProcess; }

我尝试了下面的代码,我改变了SHELLEXECUTEINFO结构,然后开始工作。 我所做的更改将variableshInstApp,lpIDList,hkeyClass,hIcon和hProcess从int重命名为inptr。

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Auto)] public struct SHELLEXECUTEINFO { public int cbSize; public uint fMask; public IntPtr hwnd; [MarshalAs(UnmanagedType.LPTStr)] public string lpVerb; [MarshalAs(UnmanagedType.LPTStr)] public string lpFile; [MarshalAs(UnmanagedType.LPTStr)] public string lpParameters; [MarshalAs(UnmanagedType.LPTStr)] public string lpDirectory; public int nShow; public IntPtr hInstApp; public IntPtr lpIDList; [MarshalAs(UnmanagedType.LPTStr)] public string lpClass; public IntPtr hkeyClass; public uint dwHotKey; public IntPtr hIcon; public IntPtr hProcess; }

我想知道如果我们可以使它与int数据types只适用于这些variables。 还是仅与IntPtr一起使用? 除了数据types大小之外,它们在这种情况下有什么不同呢? 因为它不给我任何语法错误,当我使用int只为hInstApp,lpIDList,hkeyClass,hIcon和hProcessvariables,但它不起作用。

使用Excel Interop打开Excel工作簿时出现问题

在第三方应用程序中模拟用户input

在C#程序中访问Linux文件系统

专门用于Windows的java库

进程树

如何检查窗口句柄是否用于控制台窗口?

在C#中使用TaskDialogIndirect

COM +库应用程序的目的是什么?

由tlbimp生成的COM Wrapper是与操作系统版本相关的

哪里使用null BSTRlogging?

您需要深入了解Windows SDK标头,以查看类型的大小。 例如,对于一个64位的进程,sizeof(HWND)在C ++中是8,在C#中sizeof(int)是4,因此如果你使用int来存储HWND,你就是在破坏内存。 相同的HKEY,LPITEMIDLIST,HINSTANCE和HICON。 IntPtr是为这种特定于平台的数据类型大小而设计的。

编译器不会警告你有关内存损坏等运行时错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐