I need to utilize in managed code some security functions from secur32.dll.
To do this, i have to call 'InitSecurityInterface' function, which returns
a
pointer to a 'SecurityFunctions' struct. This struct have pointers to the
secure methods.
I've defined:
[DllImport("secur32.dll")]
static extern IntPtr InitSecurityInterfaceA();
and
[StructLayout(LayoutKind.Sequential)]
public struct SecurityFunctions
{
public ulong dwVersion;
public IntPtr EnumerateSecurityPackages;
public IntPtr QueryCredentialsAttributes;
.......
}
Now, my initialization code looks like:
IntPtr functionsPointer = InitSecurityInterface();
SecurityFunctions funcs = (SecurityFunctions)
Marshal.PtroToStructure(functionsPointer, typeof(SecurityFunctions));
And i cannot go beyond this. How can i invoke, as example,
"funcs.EnumerateSecurityPackages(...)" method. It have
it's own signature. I've investigated System.Reflection.Emit because
TypeBuilder have the "DefinePInvokeMethod",
but i cannot find how link this dynamic methods with the fixed address that
i have in "SecurityFunctions" structure.
Can anybody help me?
Thanks in advance.
Federico
> [StructLayout(LayoutKind.Sequential)]
> public struct SecurityFunctions
> {
> public ulong dwVersion;
Should be an uint.
>How can i invoke, as example,
>"funcs.EnumerateSecurityPackages(...)" method. It have
>it's own signature. I've investigated System.Reflection.Emit because
>TypeBuilder have the "DefinePInvokeMethod",
>but i cannot find how link this dynamic methods with the fixed address that
>i have in "SecurityFunctions" structure.
If you're lucky enough to use .NET frameowrk v2.0 you can use
Marshal.GetDelegateForFunctionPointer. In previous versions it's a lot
more complicated. It's possible to use Reflection Emit and generate IL
code that uses the calli instruction to call a function through a
pointer. But I'd recommend using managed C++ instead where this is so
much easier.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
fvillaf - 18 Nov 2005 00:08 GMT
Mattias:
Thank you for your answer.
In 2.0, i can directly declare a delegate, put the delegate as the type for
the
struct's members and, after getting the pointer for the whole struct,
Marshal.PtrToStructure(...) converts all the function pointers to delegates,
it's awesome!.
But trying the same in 1.1 throws an exception that says something like it
cannot creates the delegate because the passed function pointer was not
obtained from a delegate. (Something related to security, i suppose).
But i'm enforced to write the solution in c#, so, i'm going for the dynamic
il solution...
Again, thank you.
Federico
"
>> [StructLayout(LayoutKind.Sequential)]
>> public struct SecurityFunctions
[quoted text clipped - 19 lines]
>
> Mattias