如果某些参数只是 int 类型,ShellExecute 不起作用
ShellExecute doesnt work if some parameters are just int type
我有下面的代码来启动任何 .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 结构,然后它开始工作。我所做的更改将变量 hInstApp、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 数据类型。或者它只适用于 IntPtr 吗?除了数据类型大小之外,它们在这种情况下有何不同?因为当我仅将 int 用于 hInstApp、lpIDList、hkeyClass、hIcon 和 hProcess 变量时它不会给我任何语法错误,但它不起作用。
您需要深入研究 Windows SDK headers 以查看该类型有多大。
例如,对于 64 位进程,sizeof(HWND) 在 C++ 中为 8,而 sizeof(int) 在 C# 中为 4,因此如果您使用 int 存储 HWND,就会破坏内存。 HKEY、LPITEMIDLIST、HINSTANCE 和 HICON 也一样。 IntPtr 是为这种特定于平台的数据类型大小而设计的。
编译器不会就内存损坏等运行时错误向您发出警告。
我有下面的代码来启动任何 .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 结构,然后它开始工作。我所做的更改将变量 hInstApp、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 数据类型。或者它只适用于 IntPtr 吗?除了数据类型大小之外,它们在这种情况下有何不同?因为当我仅将 int 用于 hInstApp、lpIDList、hkeyClass、hIcon 和 hProcess 变量时它不会给我任何语法错误,但它不起作用。
您需要深入研究 Windows SDK headers 以查看该类型有多大。 例如,对于 64 位进程,sizeof(HWND) 在 C++ 中为 8,而 sizeof(int) 在 C# 中为 4,因此如果您使用 int 存储 HWND,就会破坏内存。 HKEY、LPITEMIDLIST、HINSTANCE 和 HICON 也一样。 IntPtr 是为这种特定于平台的数据类型大小而设计的。
编译器不会就内存损坏等运行时错误向您发出警告。