如何从C#调用AllocateAndInitializeSid函数?
有人能给我一个完整的工作示例,从C#code调用如何从C#调用AllocateAndInitializeSid函数?,c#,.net,winapi,C#,.net,Winapi,有人能给我一个完整的工作示例,从C#code调用AllocateAndInitializeSid函数吗 我发现: 我不知道如何构造此方法的签名-我应该如何处理PSID\u IDENTIFIER\u AUTHORITY和PSID类型?我应该如何传递它们-使用ref或out?For Platform Invoke www.pinvoke.net是您的新好友 如果您的目标是.NET 2.0或更高版本,则类System.Security.Principal.SecurityIdentifier包装SID
AllocateAndInitializeSid
函数吗
我发现:
我不知道如何构造此方法的签名-我应该如何处理
PSID\u IDENTIFIER\u AUTHORITY
和PSID
类型?我应该如何传递它们-使用ref
或out
?For Platform Invoke www.pinvoke.net是您的新好友
如果您的目标是.NET 2.0或更高版本,则类System.Security.Principal.SecurityIdentifier包装SID,并允许您避免易出错的Win32 API 不完全是你问题的答案,但谁知道它可能有用。使用:
我去过那里。根据MSDN,第一个参数是“指向SID_标识符_权限结构的指针”,pinvoke.net称其为IntPtr。是一样的吗?我应该如何创建thsi指针?请张贴一个完整的例子。我尝试了我在网上找到的所有东西,但我无法让它工作。
BOOL WINAPI AllocateAndInitializeSid(
__in PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
__in BYTE nSubAuthorityCount,
__in DWORD dwSubAuthority0,
__in DWORD dwSubAuthority1,
__in DWORD dwSubAuthority2,
__in DWORD dwSubAuthority3,
__in DWORD dwSubAuthority4,
__in DWORD dwSubAuthority5,
__in DWORD dwSubAuthority6,
__in DWORD dwSubAuthority7,
__out PSID *pSid
);
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct SidIdentifierAuthority {
/// BYTE[6]
[System.Runtime.InteropServices.MarshalAsAttribute(
System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 6,
ArraySubType =
System.Runtime.InteropServices.UnmanagedType.I1)]
public byte[] Value;
}
public partial class NativeMethods {
/// Return Type: BOOL->int
///pIdentifierAuthority: PSID_IDENTIFIER_AUTHORITY->_SID_IDENTIFIER_AUTHORITY*
///nSubAuthorityCount: BYTE->unsigned char
///nSubAuthority0: DWORD->unsigned int
///nSubAuthority1: DWORD->unsigned int
///nSubAuthority2: DWORD->unsigned int
///nSubAuthority3: DWORD->unsigned int
///nSubAuthority4: DWORD->unsigned int
///nSubAuthority5: DWORD->unsigned int
///nSubAuthority6: DWORD->unsigned int
///nSubAuthority7: DWORD->unsigned int
///pSid: PSID*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint = "AllocateAndInitializeSid")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool AllocateAndInitializeSid(
[System.Runtime.InteropServices.InAttribute()]
ref SidIdentifierAuthority pIdentifierAuthority,
byte nSubAuthorityCount,
uint nSubAuthority0,
uint nSubAuthority1,
uint nSubAuthority2,
uint nSubAuthority3,
uint nSubAuthority4,
uint nSubAuthority5,
uint nSubAuthority6,
uint nSubAuthority7,
out System.IntPtr pSid);
}