.NET: Come installare UpdateProcThreadAttribute
Domanda
Sto provando a PInvoke UpdateProcThreadAttribute ()
su Windows 7 ma i miei tentativi continuano a restituire FALSE con un errore Win32 Last Last32 di 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
);
Ecco il mio tentativo di firma 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
);
Questa dichiarazione è sensata? Grazie.
Soluzione
Hai qualche problema con la tua dichiarazione ma quello che ti sta dando l'errore non supportato è il parametro Attribute. Un DWORD_PTR non è un puntatore ma piuttosto un intero senza segno delle dimensioni di un puntatore, quindi piuttosto che rifare dovrebbe essere un IntPtr.
La dichiarazione che vorrei usare è:
[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);
EDIT:
Ho provato a farlo come commento, ma non ci vuole molto per codificare.
Per un handle di processo è necessario un IntPtr per contenere l'handle. Quindi avresti bisogno di qualcosa del tipo:
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.