Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now