Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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