[英]Failing when using Pinvoke calling UpdateProcThreadAttribute for AppContainer security capabilities
[英].NET : How to PInvoke UpdateProcThreadAttribute
我試圖在Windows 7上PInvoke UpdateProcThreadAttribute()
但我的嘗試只是返回FALSE與最后Win32錯誤50。
Function declaration (from MSDN)
BOOL WINAPI UpdateProcThreadAttribute(
__inout LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList,
__in DWORD dwFlags,
__in DWORD_PTR Attribute,
__in PVOID lpValue,
__in SIZE_T cbSize,
__out_opt PVOID lpPreviousValue,
__in_opt PSIZE_T lpReturnSize
);
這是我對PInvoke簽名的嘗試:
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
public static extern bool UpdateProcThreadAttribute
(
IntPtr lpAttributeList,
UInt32 dwFlags,
ref UInt32 Attribute,
ref IntPtr lpValue,
ref IntPtr cbSize,
IntPtr lpPreviousValue,
IntPtr lpReturnSize
);
這個宣言是否明智? 謝謝。
您的聲明存在一些問題,但是給您不支持錯誤的問題是Attribute參數。 DWORD_PTR不是指針而是指針大小的無符號整數,因此它不應該是引用它應該是IntPtr。
我將使用的聲明是:
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UpdateProcThreadAttribute(
IntPtr lpAttributeList, uint dwFlags, IntPtr Attribute,
IntPtr lpValue, IntPtr cbSize, IntPtr lpPreviousValue,
IntPtr lpReturnSize);
編輯:
我嘗試將此作為注釋,但它不能很好地編寫代碼。
對於進程句柄,您需要一個IntPtr來保存句柄。 所以你需要這樣的東西:
IntPtr hProcess //previously retrieved.
IntPtr lpAttributeList //previously allocated using InitializeProcThreadAttributeList and Marshal.AllocHGlobal.
const int PROC_THREAD_ATTRIBUTE_PARENT_PROCESS = 0x00020000;
IntPtr lpValue = Marshal.AllocHGlobal(IntPtr.Size);
Marshal.WriteIntPtr(lpValue, hProcess);
if(UpdateProcThreadAttribute(lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValue, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
{
//do something
}
//Free lpValue only after the lpAttributeList is deleted.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.