473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Using an array that is allocated in a native dll?

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
Nov 4 '06 #1
5 9412
Ole
"Ole" <ol*@blabla.comwrote in message
news:Of**************@TK2MSFTNGP02.phx.gbl...
void DllFunction (PBYTE pArray, DWORD dwLen)
{
....
}
Should ofcourse have been:
void DllFunction (PBYTE pArray, DWORD *dwLen)

Ole
Nov 4 '06 #2
>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 [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 5 '06 #3
Ole
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:uy**************@TK2MSFTNGP03.phx.gbl...
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)
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
Nov 5 '06 #4
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[i] = 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[i] = 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
Nov 24 '06 #5
<ciach>

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

Przemek Sulikowski
Nov 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
1
by: Stephen Holly | last post by:
Hi All, I have some source code writen in c that implements the blowfish encryption algorithm which I have created as a DLL and want to use it in a .NET solution. I want to pass some params...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
by: Peter Oliphant | last post by:
While it does look like 2005 does use a better syntax in general for garbage collection than 2003, here is something I think went the other way. arrays. Do people really think that: ...
9
by: Peter Olcott | last post by:
// // Array2D.h 2005-11-27 5:50 AM // #define UINT unsigned int // // // class ArrayType2D { private: int Width;
6
by: Ananas | last post by:
Hi, My native C++ function creates a dynamic array. I'm marshalling it to managed code and got to delete after. How to make it: c++ code: void CreateArrayInside( pTestStruct &TestStruct,...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.