By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,123 Members | 807 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,123 IT Pros & Developers. It's quick & easy.

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

P: 3
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
Share this Question
Share on Google+
7 Replies

Expert 100+
P: 190
deleted...Pointed to incorrect marshalling method...sorry.
Sep 26 '08 #2

Expert 100+
P: 190
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

P: 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

Expert 100+
P: 190
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

P: 3
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

Expert 100+
P: 190
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

Expert 100+
P: 190
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

Post your reply

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