Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Interop and marshalling pointers

Posted on 2004-10-10
6
Medium Priority
?
594 Views
Last Modified: 2008-02-01
I have a C struct defined as follows:
struct CStruct {
     char *myChar;
     char **myChar2;
     int *myInt;
     CStruct *nextStruct;
}

I need to pass an object of this type to an unmanaged function:

[DllImport("MyDll.dll")]
public extern int myFunc(CStruct);

Can someone show me the managed class I should make for CStruct?  I don't know how to handle the pointers and double pointers.
0
Comment
Question by:thedude112286
  • 2
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 1500 total points
ID: 12272726
Can't you simply declare the struct as unsafe?

unsafe struct Cstruct
{
      char *myChar;
      char **myChar2;
      int *myInt;
      Cstruct *nextStruct;
}
0
 
LVL 4

Author Comment

by:thedude112286
ID: 12273612
I'm working in C# so unsafe code is not an option.  I need to use interop marshalling.
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 12273993

Razzie_  says correct .

And compile your code /unsafe option.

Or if you using IDE
 On the Project Property Pages Dialog set Allow unsafe code blocks to true

Regards
Prakash
0
 
LVL 4

Author Comment

by:thedude112286
ID: 12280659
Isn't there any way I could do this without unsafe code, using marshalling and stuff?  I was going to Marshal the pointers as IntPtrs and I did this so far:

[StructLayout(LayoutKind.Sequential)]
class CStructPointer {
     string myChar;
     ref string myChar2; // This isn't legal, I don't think
     ref int myInt; // not legal
     CStructPointer nextStruct;
}

[StructLayout(LayoutKind.Sequential)]
struct CStruct {
     string myChar;
     ref string myChar2; // Not legal
     ref int myInt; // I don't think this is legal
     CStructPointer nextStruct;
}

Any help????
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12281144
No you can't use the ref keywoard that way.

If you don't want to use unsafe code, take a look at this MSDN article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstructssample.asp

This article: http://www.c-sharpcenter.com/Tutorial/UnManaged_02.htm shows how to use Marshalling so you can use a pointer to a pointer.

HTH,

Razzie
0

Featured Post

Independent Software Vendors: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

927 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