native struct --> C#

Hello.

I have a C# application and a native dll written in C++. I have a function called ReadHash() in native dll. I need this function to return an array of bytes. I made a struct called HashRecord in C# and C++. it looks like this

typedef struct tag_Hash_Record {

BYTE HashCode[];

} HashRecord, *PHashRecord;

and

public struct HashRecord

{

public byte[] HashCodes;

}

the ReadHash() function returns HashRecord structure. but when i call this function from c# app, I get a MarshalDirectiveException sayng "Method's type signature is not PInvoke compatible.". anyone knows how to fix this problem thanks.




Answer this question

native struct --> C#

  • Qiao

    Try signatures like these

    // C++ side

    void YourFunc(BYTE hashCode[]);

    // C# side

    static extern void YourFunc(byte[] hasCode);

    You may have to add a parameter indicating the length of the buffer unless the hash has a well known fixed size.



  • HairyDan

    Change the type of ImagenM to IntPtr.



  • JosephCooney

    no one knows the answere :(

  • geoffacox

    yes I control both side, but I dont understand how do i do that :)

  • Paul Russell

    thanks for your help, I already found the key of the problem using [Out] directive :)

  • pravej

    Hi, I've the same problem.

    My struct in C++ is:

    typedef struct{
    float pintura;
    float sustrato;
    float adherencia;
    char* codiErrorAdMalla;
    unsigned char * ImagenM;
    int alto;
    int ancho;
    }DatosAdMalla;

    and the struct in C# is:

    public struct DatosAdMalla
    {
    public float pintura;
    public float sustrato;
    public float adherencia;
    public string codiErrorAdMalla;
    public byte[] ImagenM;
    public int alto;
    public int ancho;
    }


    When I call the function i get the same error like you.
    "Method's type signature is not PInvoke compatible.".

    what can i do

    thanks

  • Seba85

    If you control both the native and managed side it's easier to change the function to return the data via output parameters.



  • native struct --> C#