Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET:如何使用PInvoke UpdateProctThread属性_C#_.net_Pinvoke - Fatal编程技术网

C# .NET:如何使用PInvoke UpdateProctThread属性

C# .NET:如何使用PInvoke UpdateProctThread属性,c#,.net,pinvoke,C#,.net,Pinvoke,我正在Windows 7上尝试PInvoke UpdateProcThreadAttribute(),但我的尝试一直返回FALSE,最后一个Win32错误为50 Function declaration (from MSDN) BOOL WINAPI UpdateProcThreadAttribute( __inout LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList, __in DWORD dwFlags, __in

我正在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
);

这个声明明智吗?谢谢。

您的声明有一些问题,但导致不支持错误的是属性参数。DWORD_PTR不是指针,而是指针大小的无符号整数,因此它应该是IntPtr而不是ref uint

我将使用的声明是:

    [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.

谢谢你。我更新了签名,但仍然得到50。您建议如何设置(比如)lpValue来保存进程的句柄。我把它作为一个IntPtr(比如说,幻影),所以我是否直接传递它,对于cbSize,执行如下操作:IntPtr pSize=(System.IntPtr)(Marshal.SizeOf(pHandle))?正常情况下,我的pinvoke是可以的,但这一个似乎特别邪恶。我非常感谢你的答复。谢谢,非常感谢。你几乎帮我到了那里。。现在我得到错误24(错误长度)。在上面的示例中,您讨论了将pass IntPtr.Size作为cbSize参数,而在最初的文章中,您将其作为IntPtr。我尝试了IntPtr lpSize=Marshal.AllocHGlobal(IntPtr.Size);WriteInt32(lpSize,IntPtr.Size)并将其传递给。显然这是错误的,但我不知道正确的用法应该是什么。有什么想法吗?谢谢。对不起,我忘了转换。它应该是(IntPtr)IntPtr.Size。我已经更正了这篇帖子。这类问题总是值得检查的。