Solved

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

Posted on 2008-10-17
3
536 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

823 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