Solved

Interop and marshalling pointers

Posted on 2004-10-10
6
581 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
[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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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.
Suggested Courses

751 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