Solved

Interop and marshalling pointers

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

821 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