?
Solved

Pass pointer to pointer to struct into C method from C#

Posted on 2008-10-17
3
Medium Priority
?
545 Views
Last Modified: 2013-12-17
I'm trying to pass a pointer to a pointer to a struct to a C method.  Here is the C method I'm trying to call:
__declspec(dllexport)unsigned int GetValue(void *handle,utf8_string tokenArray[], utf8_string tokenType,
utf8_string userData, struct SecurityObject ** & so);

Note: utf8_string is unsigned char*

Here is my code on the C# side
DllImport("somtokenclient_vc2005.dll", CharSet = CharSet.Unicode)]
public static extern int GetValue(int handle,
[In, MarshalAs(UnmanagedType.LPArray, SizeConst = 20, ArraySubType = UnmanagedType.LPStr)] string[] tokenArray,
[In, MarshalAs(UnmanagedType.LPStr)] string tokenType,
[In, MarshalAs(UnmanagedType.LPStr)] string userData,
[Out]out IntPtr SOA);

It looks like I'm getting a value back for the IntPtr but I'm not sure how to grab the struct out of it.  Any help would be appreciated.  Thanks.
0
Comment
Question by:chuck620
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22740974
0
 

Author Comment

by:chuck620
ID: 22741127
I think I have the C method call correct, I'm passing an IntPtr as an out parameter, I'm just not sure where to go from there.  I know there are 2 elements in the struct array coming back, so I've tried this (which I found from Microsoft) and didn't get the results I was expecting:

SecurityObject[] ppSOA2 = new SecurityObject[2];
IntPtr current = ppSOA;//out parameter from method call
for (int i = 0; i < ppSOA2.Length; i++)
{
          ppSOA2[i] = new SecurityObject();
          Marshal.PtrToStructure(current, ppSOA2[i]);
          Marshal.DestroyStructure(current, typeof(SecurityObject));
          current = (IntPtr)((long)current + Marshal.SizeOf(ppSOA2[i]));
          string textValue = Marshal.PtrToStringAnsi(ppSOA2[i].textValue);
}

Here is the struct I'm using on the C# side:
    using uchar = IntPtr;
    [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
    unsafe public class SecurityObject
    {
        public int returnCode;
        public int objectType;
        public uchar token;
        public uchar tokenType;
        public uchar textValue;
        public uchar userData;
        public int lastTranDate;
        public uchar retentionRuleID;
        public uchar profileID;
        public uchar profiledescription;
        public uchar systemID;
        public int logicalDelete;
    }
0
 

Accepted Solution

by:
chuck620 earned 0 total points
ID: 22771448
I was able to figure it out searching other posts on other sites.

 [DllImport("somtokenclient_vc2005.dll", CharSet = CharSet.Unicode)]
private static extern int GetValue(int handle,
[In, MarshalAs(UnmanagedType.LPArray, SizeConst = 20, ArraySubType = UnmanagedType.LPStr)] string[] tokenArray,
[In, MarshalAs(UnmanagedType.LPStr)] string tokenType,
[In, MarshalAs(UnmanagedType.LPStr)] string userData,
[Out, MarshalAs(UnmanagedType.SysInt)]out IntPtr SOA);

Then I called it passing an IntPtr(SOA) as an out parameter:

SecurityObject[] SOA2 = new SecurityObject[2];
IntPtr current = SOA;
IntPtr[] curr = new IntPtr[2];
Marshal.Copy(current, curr, 0, 2);
valueTextArray = new string[2];
for (int i = 0; i < SOA2.Length; i++)
{
          SOA2[i] = new SecurityObject();
          Marshal.PtrToStructure(curr[i], SOA2[i]);
          Marshal.DestroyStructure(curr[i], typeof(SecurityObject));
          valueTextArray[i] = SOA2[i].textValue;
}
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question