Using an array that is allocated in a native dll?

  • Thread starter Thread starter Ole
  • Start date Start date
O

Ole

Hi,

I have a native dll that has a byte array to which I want a pointer to in my
C# application but I will need some help to do that.

the dll exports this function:

void DllFunction (PBYTE pArray, DWORD dwLen)
{
.....
}

How should I specify the PBYTE parameter in my Dllimport?

In my C# code I would like to use the imported array like:
ByteVariable = ImportedArray[5];

Is that possible - and if then how? If not - what would you suggest to do
instead?

Thanks
Ole
 
Should ofcourse have been:
void DllFunction (PBYTE pArray, DWORD *dwLen)

If you want to return a pointer to the byte array, it has to be

void DllFunction (PBYTE *pArray, DWORD *dwLen)


Mattias
 
Mattias Sjögren said:
If you want to return a pointer to the byte array, it has to be

void DllFunction (PBYTE *pArray, DWORD *dwLen)

Thanks. I was thinking of passing a pointer to the unmanaged function and
then in that function let the pointer point to the Byte Array that I'm
interested in, but how do I declare and use the pointer in C# ?

Thanks
Ole
 
Thanks. I was thinking of passing a pointer to the unmanaged function and
then in that function let the pointer point to the Byte Array that I'm
interested in, but how do I declare and use the pointer in C# ?

Thanks
Ole

first way alloc memory in C# and pass it to unmanaged dll to fill data
in C#

using System;
using System.Runtime.InteropServices;

[DllImport("native.dll")]
public static extern bool DllFunction(IntPtr data, int len);


void somefunction()
{
int len = 70;
IntPtr ptr = Marshal.AllocHGlobal(len);
DllFunction(ptr, len);
byte[] returnbytes = new byte[len];
Marshal.Copy (returnbytes, 0, ptr, len);
Marshal.FreeHGlobal(ptr);
}


in C/CPP

BOOL DllFunction(PBYTE data, DWORD len)
{
unsigned char* mdata = (unsigned char*)data;
for(int i = 0; i < len; i ++)
{
mdata = 44; // fill the data
}
}


second way: alloc in unmanaged and fill there back to managed copy and free
in unmanaged
in C#

using System;
using System.Runtime.InteropServices;

[DllImport("native.dll")]
public static extern bool DllFunction(ref IntPtr data, ref int len);
[DllImport("native.dll")]
public static extern bool DllFunctionFree(IntPtr data);

void somefunction()
{
int len = 0;
IntPtr ptr = IntPtr.Zero;
byte[] returnbytes = null;
if(DllFunction(ref ptr, ref len))
{
returnbytes = new byte[len];
Marshal.Copy (returnbytes, 0, ptr, len);
DllFunctionFree(ptr);
}
}


in C/CPP

BOOL DllFunction(PBYTE* data, DWORD* len)
{
DWORD mlen = 255;
*len = mlen;
unsigned char* mdata = (unsigned char*)GlobalAlloc(GMEM_FIXED, mlen);
for(int i = 0; i < mlen; i ++)
{
mdata = 44; // fill the data
}
data = (PBYTE)mdata;
if(somethinggonewrongwithfilling)
{
GlobalFree((HGLOBAL)mdata);
return FALSE;
}
return TRUE;
}

void DllFunctionFree(HGLOBAL data)
{
GlobalFree(data);
}

pozdrawiam

Przemek Sulikowski
 
<ciach>

wrong Marshal.Copy ...
should be Marshal.Copy (ptr, returnbytes, 0, len);


pozdrawiam

Przemek Sulikowski
 
Back
Top