Reputation: 43
I am working on a project where I need to PInvoke the secur32!AddSecurityPackageA
function, but I am still learning the ins and outs of how to do this by hand and could use some help.
Here are a the references I am working with:
And here's a sample of my code where I am trying to define the struct and call the function:
[DllImport("secur32.dll", EntryPoint = "AddSecurityPackageA")]
public static extern void AddSecurityPackageA(
ref string pszPackageName,
ref SECURITY_PACKAGE_OPTIONS[] Options
);
[StructLayout(LayoutKind.Sequential, CharSet =CharSet.Ansi)]
public class SECURITY_PACKAGE_OPTIONS
{
public ulong Size;
public ulong Type;
public ulong Flags;
public ulong SignatureSize;
public IntPtr Signature;
}
string dll = @"c:\temp\test.dll";
SECURITY_PACKAGE_OPTIONS[] pkgOpts = new SECURITY_PACKAGE_OPTIONS();
AddSecurityPackageA(ref dll, ref pkgOpts);
My questions are:
ref
and is this generally correct according to the MSDN docs?IntPtr
. Is that correct or do I need to use unsafe
?Thank you!
Upvotes: 0
Views: 376
Reputation: 23501
This one works for me:
[DllImport("secur32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint AddSecurityPackage(
string pszPackageName,
SECURITY_PACKAGE_OPTIONS Options
);
[DllImport("secur32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint DeleteSecurityPackage(
string pszPackageName
);
Upvotes: 0
Reputation: 612993
Some comments:
W
function rather than the A
function. You don't want to limit yourself to ANSI. This is a Unicode world.uint
or int
but you should check in the C++ header file.ref string
is wrong. It should be string
.ref SECURITY_PACKAGE_OPTIONS[]
is wrong. It is not an array. It is a pointer to a struct
. Since you declared SECURITY_PACKAGE_OPTIONS
as a class, a reference type, you can replace ref SECURITY_PACKAGE_OPTIONS[]
with SECURITY_PACKAGE_OPTIONS
.unsigned long
is 32 bits, so it should be uint
in C#.IntPtr
is correct, but that leaves unresolved the question of how to declare the digital signature and obtain a pointer to it. I think it's outside the remit of this question for us to track down an example of how to do that.Upvotes: 1