C# p/invoke Secur32.dll 问题
我正在尝试包装 Secur32.dll 的 EnumerateSecurityPackages 函数声明如下:
SECURITY_STATUS SEC_Entry EnumerateSecurityPackages(
__in PULONG pcPackages,
__in PSecPkgInfo *ppPackageInfo
);
我有以下 C# 代码,但当我尝试运行它时,出现 AccessViolationException。在调试器中,pcPackages 变量确实设置正确,但我认为我对 SecPkgInfos 数组做错了。
[StructLayout(LayoutKind.Sequential)]
public struct SecPkgInfo
{
public ulong fCapabilities;
public ushort wVersion;
public ushort wRPCID;
public ulong cbMaxToken;
public string Name;
public string Comment;
}
[DllImport("Secur32.dll")]
public extern static int EnumerateSecurityPackages(
ref ulong pcPackages,
ref SecPkgInfo[] ppPackageInfo
);
///Calling code
ulong count = 0;
SecPkgInfo[] buffer = new SecPkgInfo[256];
EnumerateSecurityPackages(ref count, ref buffer);
有什么想法我做错了吗?
I'm trying to wrap the Secur32.dll's EnumerateSecurityPackages function which is declared below:
SECURITY_STATUS SEC_Entry EnumerateSecurityPackages(
__in PULONG pcPackages,
__in PSecPkgInfo *ppPackageInfo
);
I have the following C# code, but I get an AccessViolationException when I try to run it. In the debugger the pcPackages variable does get set correctly, but I think I'm doing something wrong with the array of SecPkgInfos.
[StructLayout(LayoutKind.Sequential)]
public struct SecPkgInfo
{
public ulong fCapabilities;
public ushort wVersion;
public ushort wRPCID;
public ulong cbMaxToken;
public string Name;
public string Comment;
}
[DllImport("Secur32.dll")]
public extern static int EnumerateSecurityPackages(
ref ulong pcPackages,
ref SecPkgInfo[] ppPackageInfo
);
///Calling code
ulong count = 0;
SecPkgInfo[] buffer = new SecPkgInfo[256];
EnumerateSecurityPackages(ref count, ref buffer);
Any ideas what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下这段代码,它是从 VB.Net(我的母语)转换而来的,但在 C# 中运行得很好。只需调用 Call_EnumerateSecurityPackages() 即可,它会为您返回一个列表。
为了将来使用,这里是 VB 版本:
Try this code, its converted from VB.Net (my native language) but runs fine for me in C#. Just call
Call_EnumerateSecurityPackages()
and it will return a list for you.And for future use, here's the VB version:
您对 SecPkgInfo 的定义不正确,C 的
ULONG
!= C# 的ulong
。您还需要非常清楚地指定如何通过 MarshalAs 编组 Name 和 CommentYour definition of SecPkgInfo is incorrect, C's
ULONG
!= C#'sulong
. You also need to very clearly specify how to marshal Name and Comment via MarshalAs