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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phillio
    New Member
    • Sep 2008
    • 3

    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!!
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    deleted...Point ed to incorrect marshalling method...sorry.
    Last edited by mldisibio; Sep 26 '08, 06:01 PM. Reason: Pointed to incorrect marshalling method...sorry.

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      If you have an IntPtr to the c++ array and know its size, this might help:
      Marshal.Copy(In tPtr, 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.

      Comment

      • phillio
        New Member
        • Sep 2008
        • 3

        #4
        marshal byte* pointer from c++ to c#

        "If you have an IntPtr to the c++ array and know its size..."

        //C++ Code:

        BYTE *cert= pCert->pbCertEncode d; //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?
        Last edited by phillio; Sep 27 '08, 12:12 PM. Reason: better expression

        Comment

        • mldisibio
          Recognized Expert New Member
          • Sep 2008
          • 191

          #5
          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?

          Comment

          • phillio
            New Member
            • Sep 2008
            • 3

            #6
            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 CertDllVerifyRe vocation. 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.

            Comment

            • mldisibio
              Recognized Expert New Member
              • Sep 2008
              • 191

              #7
              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.

              Comment

              • mldisibio
                Recognized Expert New Member
                • Sep 2008
                • 191

                #8
                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.Secu rity libraries to see if the Framework already natively provides what you are trying to provide from your win32 dll?

                Comment

                Working...