Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Function Pointers in a struct

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fvillaf - 17 Nov 2005 20:45 GMT
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
Mattias Sjögren - 17 Nov 2005 21:42 GMT
>    [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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.