问题描述
我想的PInvoke CreateDesktop在传递标志继承桌面由子进程的方式。声明如下:
I am trying to PInvoke CreateDesktop in a way that passes the flag to inherit the desktop by child processes. The declaration is as follows:
[DllImport("user32", EntryPoint = "CreateDesktopW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags,
int dwDesiredAccess, [MarshalAs(UnmanagedType.LPStruct)] SECURITY_ATTRIBUTES lpsa);
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
和我用它如下:
Win32.SECURITY_ATTRIBUTES sa = new Win32.SECURITY_ATTRIBUTES();
sa.nLength = Marshal.SizeOf(sa);
sa.bInheritHandle = 1;
testDesktopHandle = Win32.CreateDesktop(name, IntPtr.Zero, IntPtr.Zero, 0, Win32.GENERIC_ALL, sa);
和它遗憾的是不工作,我得到以下错误:
And it unfortunately doesn't work, I get the following error:
System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #6': Invalid managed/unmanaged type combination (this value type must be paired with Struct).
任何想法我做错了吗?
Any ideas what I am doing wrong?
推荐答案
试着改变参数#6〜
static extern IntPtr CreateDesktop(..., [In] ref SECURITY_ATTRIBUTES lpsa);
(此编译并不会抛出异常在运行时,但我只用伪造的参数进行了测试。)的
与 CreateDesktop C ++的声明比较
Compare with C++ declaration of CreateDesktop:
HDESK WINAPI CreateDesktop(..., __in_opt LPSECURITY_ATTRIBUTES lpsa);
↑ ↑ ↑
[In] ref SECURITY_ATTRIBUTES lpsa
LP
代表长指针,即 LPSECURITY_ATTRIBUTES
是指向一个 SECURITY_ATTRIBUTES
结构。因此,在C#中,你需要按引用传递的结构实例(价值型)。
LP
stands for "long pointer", i.e. LPSECURITY_ATTRIBUTES
is a pointer to a SECURITY_ATTRIBUTES
struct. So in C# you need to pass your struct instance (value type) by reference.
这篇关于PInvoke的CreateDesktop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!