Solved

Interop and marshalling pointers

Posted on 2004-10-10
6
576 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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