473,789 Members | 2,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing memory pointer to unmanaged C++ dll

I have a dll that takes a byte pointer. Similar to this:

writeDataToBuff er(byte *buffer, int *BufferSize);

The buffer is allocated by the dll.

I have no problem with the BufferSize parameter it is the buffer parameter
that I am not sure of the best way to handle. What is the recommended way to
call this function from C#?

I appreciate any help or documentation pointers that anyone can supply.

thanks,
-joe-
Apr 19 '06 #1
4 5261
"Joe Tavares" <Joe Ta*****@discuss ions.microsoft. com> wrote in message
news:72******** *************** ***********@mic rosoft.com...
I have a dll that takes a byte pointer. Similar to this:

writeDataToBuff er(byte *buffer, int *BufferSize);


You can just do it like this:

[DllImport("kern el32", EntryPoint= "GetComputerNam eA")]
private static extern int GetComputerName (byte[] lpBuffer, ref int nSize);

or like this:

[DllImport("kern el32", EntryPoint= "GetComputerNam eA")]
private static extern int GetComputerName (IntPtr lpBuffer, ref int nSize);

[STAThread]
static void Main()
{
byte[] data = new byte[256];
GCHandle handle = GCHandle.Alloc( data, GCHandleType.Pi nned);
int size = data.Length;
GetComputerName (handle.AddrOfP innedObject(), ref size);
handle.Free();

Michael
Apr 19 '06 #2
"Joe Tavares" <Joe Ta*****@discuss ions.microsoft. com> wrote in message
news:72******** *************** ***********@mic rosoft.com...
I have a dll that takes a byte pointer. Similar to this:

writeDataToBuff er(byte *buffer, int *BufferSize);

Oops, I just read this bit:
The buffer is allocated by the dll.


In that case you should do it like this:

[DllImport("kern el32", EntryPoint= "GetComputerNam eA")]
private static extern int GetComputerName ([Out] out IntPtr lpBuffer, ref int
nSize);

and then use Marshal.Copy to copy the data to a local buffer.

I don't have something to test but I'm pretty sure you'll need to [Out] out
so the dll can return a pointer.

On second thoughts are you sure your C dll allocates the buffer, shouldn't
it be "byte **buffer" then?

Michael
Apr 19 '06 #3
Michael,
You are correct that it is Byte **, it was a typo on my part. Thank you very
much for the help. Your advice was right on and helped me to move forward
with the project. I actually took your first example in the previous post
(Byte[]buffer) and wrote a C++ wrapper around the memory
allocation/deallocation and have everything working perfectly now.

-joe-

"Michael C" wrote:
"Joe Tavares" <Joe Ta*****@discuss ions.microsoft. com> wrote in message
news:72******** *************** ***********@mic rosoft.com...
I have a dll that takes a byte pointer. Similar to this:

writeDataToBuff er(byte *buffer, int *BufferSize);


Oops, I just read this bit:
The buffer is allocated by the dll.


In that case you should do it like this:

[DllImport("kern el32", EntryPoint= "GetComputerNam eA")]
private static extern int GetComputerName ([Out] out IntPtr lpBuffer, ref int
nSize);

and then use Marshal.Copy to copy the data to a local buffer.

I don't have something to test but I'm pretty sure you'll need to [Out] out
so the dll can return a pointer.

On second thoughts are you sure your C dll allocates the buffer, shouldn't
it be "byte **buffer" then?

Michael

Apr 19 '06 #4
"Joe Tavares" <Jo********@dis cussions.micros oft.com> wrote in message
news:D0******** *************** ***********@mic rosoft.com...
Michael,
You are correct that it is Byte **, it was a typo on my part. Thank you
very
much for the help. Your advice was right on and helped me to move forward
with the project. I actually took your first example in the previous post
(Byte[]buffer) and wrote a C++ wrapper around the memory
allocation/deallocation and have everything working perfectly now.


You can get away without the wrapper but if you do write one another option
is to write it as a dot net dll in C++. This is easy to do (even I managed
to do it) as the wizards step you through everything. Then you can add the
dll as a managed reference.

Michael
Apr 20 '06 #5

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

Similar topics

58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
1
9503
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this worked pretty well, accepting to write unsafe code ;-) But my next struct has an array inside and I don't get it passed over to the DLL correctly. Here my struct in c#:
3
4570
by: John Sun | last post by:
Dear Group Gurus, If I use a COM class in my C# code, will the memory used by COM object be garbage collected, or do I have to manually collect it. Thanks, John
8
6784
by: promko | last post by:
Hi! I need to call the following unmanged method: HFCI __cdecl FCICreate( void FAR* pv) I have written the next managed declaration: public static extern IntPtr FCICreate( IntPtr pv );
4
8437
by: dogalacar | last post by:
Hi All, I am trying to pass array of structures from a C dll to C# as msdn sample does(outarrayofstructs sample) but PtrToStructure function gives error : --> "structure must not be a value class" What i am doing wrong ? here is the code
3
2905
by: dbru | last post by:
I need to pass an address of a Managed float array to a DLL. The following doesn't seem to work extern static float GetXXX( StringBuilder HWND, long nWhat, ref float lparam );
4
5724
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage collected. I also noticed that when replaced some of the existing MFC dialogs with managed winforms that everything is still running in the same app domain.( No context change) So my question is this, what are the performance differences in using...
0
2019
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
17
7255
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 to show the array data to the end user. Can I do that? How?
0
1661
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of other pointers, to structs. Horrible isn't it? I have no way of modifying the unmanaged code, I...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.