0

I have structure:

public struct MyStruct
{
   public int a;
   public int b;
   public byte[] mass;
}  

I need: Pass poiner to "mass" array to C++ unmanaged function. And after it done all work it will return me pointer to "mass".

So I have the list of MyStruct. And I need to know what the MyStruct in the list contains returned "mass"(wich pointer to I have). If I know pointer to "mass" can I reduce pointer to 8 bytes and take pointer to MyStruct?

HOW TO :

1.Get IntPtr to "mass" array?

2.Get IntPtr to MyStruct structure?

3.Get MyStruct from IntPtr?

But, do not using any copy procedure, like Marshal.Copy...

Or is there a better way to do what I need ? Can I use pointers like in C++ or IntPtr is enought, and how can I do that?

user2377299
  • 93
  • 2
  • 8

2 Answers2

1

Assuming that the memory for the array is allocated by the managed code:

When you pass an array to an unmanaged function via P/Invoke, then by default the array is generally pinned in memory by the marshaller so that the memory used by the array does not have to be copied.

You should not need to use an IntPtr at all - you just need to declare the P/Invoke so that it is accepting an array parameter.

However, things are different if the unmanaged code is allocating memory to be returned to the managed code; then things get MUCH more difficult.

Assuming that's not the case, then if you can show us the "C" function declaration we might be able to come up with a P/Invoke declaration.

(I do have a feeling that your situation may be a bit more complicated though...)

Some useful links for you:

http://msdn.microsoft.com/en-us/library/z6cfh6e6%28v=vs.80%29.aspx

http://msdn.microsoft.com/en-us/library/zah6xy75.aspx

How can I pass a pointer to an array using p/invoke in C#?

And some lower level information about the optimizations that the marshaller makes when calling unmanaged code and passing arrays. Essentially, if it can it doesn't make a copy of the data at all:

http://msdn.microsoft.com/en-us/library/23acw07k%28v=vs.80%29.aspx

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • I need to pass array to some Dll written on C++. I can use any declaration. So you say SomeFunk(byte[] mass) is the same as SomeFunk(IntPtr pMass)? – user2377299 May 14 '13 at 15:36
  • @user2377299 Yes, if you pass a byte array like that, the unmanaged side can get it as an `unsigned char*`. I'll update my answer with some useful links in a minute. Let me know if there's anything else you need to know (but I'll be away from computer for the next few hours). – Matthew Watson May 14 '13 at 16:36
  • So. Let's say, DLL returns me byte array using some func like Return(byte[] b). This byte array belong to some parent class. How can I determinate it, not using foreach pattern and compare all byte arrays in them ? Can I somehow reduce pointer adress by 8 bytes and get pointer to my parent class? If,yes, the how ? – user2377299 May 15 '13 at 08:36
0

Take care as the structure may have some specific memory alignment. You may be interested by this link

Marshall777
  • 1,196
  • 5
  • 15
  • Ok. If I use [StructLayout(LayoutKind.Sequential,Pack = 1)] before struct and all in memory is good. What is next? I need some code example for my needs. – user2377299 May 14 '13 at 15:38
  • @user2377299 I'm sorry, I don't know that much about the subject. I just remembered having face this kind of issues of few years ago. – Marshall777 May 15 '13 at 13:08