Solved

Interop and marshalling pointers

Posted on 2004-10-10
6
542 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 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

18 Experts available now in Live!

Get 1:1 Help Now