native struct --> C#  
Author Message
Dato0011





PostPosted: Visual C# General, native struct --> C# Top

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.




Visual C#2  
 
 
Dato0011





PostPosted: Visual C# General, native struct --> C# Top

no one knows the answere :(

 
 
Mattias Sjogren





PostPosted: Visual C# General, native struct --> C# Top

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



 
 
Dato0011





PostPosted: Visual C# General, native struct --> C# Top

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

 
 
Mattias Sjogren





PostPosted: Visual C# General, native struct --> C# Top

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.



 
 
Dato0011





PostPosted: Visual C# General, native struct --> C# Top

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

 
 
JosepMola





PostPosted: Visual C# General, native struct --> C# Top

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

 
 
Mattias Sjogren





PostPosted: Visual C# General, native struct --> C# Top

Change the type of ImagenM to IntPtr.