473,406 Members | 2,404 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,406 software developers and data experts.

Marshal byte array from c++ code to c# dll

How is possible to pass a byte array from c++ dll to byte array in c# dll?
Should I use pointers marshaling or there is a dedicated way of marshalling arrays? I've tried using System.IntPtr from .net , but it failds.

Thank you!!
Sep 26 '08 #1
7 18374
mldisibio
190 Expert 100+
deleted...Pointed to incorrect marshalling method...sorry.
Sep 26 '08 #2
mldisibio
190 Expert 100+
If you have an IntPtr to the c++ array and know its size, this might help:
Marshal.Copy(IntPtr, byte[], int32, int32)

Let me know if that is what you needed, as I haven't had to implement such a copy but am very interested in how it's done.
Sep 26 '08 #3
"If you have an IntPtr to the c++ array and know its size..."

//C++ Code:

BYTE *cert= pCert->pbCertEncoded; //get pointer to byte array

while(*cert!= NULL)
{
byte_counter++;
*cert++;
} // get byte array length

So, I have pointer (*cert) to byte array and I know the size if that array (byte_counter) . I'm troubled passing that pointer to the C# code. How should I do that?
Sep 27 '08 #4
mldisibio
190 Expert 100+
In which language do you want to make the marshalling call? C++ or C#?

Can you clarify how the byte array is getting exposed (return value of a method, a property).

So do you want to copy the byte array using C# and copying it from an exposed property in a C++ class, or are you trying to "push" it from c++ as a parameter to a C# method? ... or something else?
Sep 27 '08 #5
I need to call a C# method from C++ code. I've made a reference from the C++ source code(it's a Win32 dll) to the C# source code(it's a Class Labrary Project).

Actually I'm writing a Revocation Provider, which functionallity has to be defined in a Win32 DLL, exporting a special function named CertDllVerifyRevocation. However, I need that functionallity to be defined in a C# DLL.

So, I call a function defined in c# dll from c++.One of the parameters I want to pass to that C# function is a pointer to a byte array, containing the encoded certificate, that I want to check if it is revoked.My problem is: How that pointer to a byte array has to be passed from a c++ code to a C# function.

P.S The actual call is made after creating object of the class defined in the c# dll, and after that I invoke the method.
Sep 28 '08 #6
mldisibio
190 Expert 100+
OK Phillo, I jumped into this one thinking you would want to marshal the unmanaged array into the managed code, using c#.

This is out of my scope. In doing a quick search, as I am sure you have done, I notice that even when people manage to successfully interop a managed C# dll from a C++ module, they also get stuck on the issue of passing a byte array into managed code.

Here is one thread in this forum that might help: Using array allocated in native dll but here is my suggestion:

1. Post your question in the C++ forum.
2. If still no answer, post in this forum again. Hopefully, the moderator will acknowledge that I was of no help here and since no one else has jumped in, a new thread is acceptable.
Sep 29 '08 #7
mldisibio
190 Expert 100+
Also, just as a side note in case you continue to hit a brick wall: if you need the c# dll to read a certificate, have you checked out the System.Security.Cryptography and System.Net.Security libraries to see if the Framework already natively provides what you are trying to provide from your win32 dll?
Sep 29 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
1
by: Me | last post by:
I'm trying to get a structure into a byte array. I can't seem to figure out how to get a non-fixed length null-terminated string into the array (without rolling my own logic). For example, a...
1
by: Jon Finch | last post by:
Hi All this is a follow up to a post I did a few days back regarding reading a font from an assembly into a memory font object. The only thing I am stuck on is getting the actual memory size of a...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
2
by: Nikolay Petrov | last post by:
How can I convert variable of type Object to Byte array? TIA
3
by: tash.robinson | last post by:
I have been pulling my hair out with this and have searched and found some solutions but I can't seem to get them to work as expected. I have a structure, which I have declared explicitly using...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.