Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
transition to visual .net from vb6 5 64
Dictionary and array of [N] size - performance tuned 12 42
SSIS GUID Variable 2 38
How to set focus on a dynamic control 18 29
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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!
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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