Hi guys,
this is my problem.
I have some C++ structure in unmanaged DLL. I need to use those structures in C#, in managed code.It seems all simple.... This is the C++ code of structures. Be careful to the structure "T__KRON_Struct_Error_Context" that is the structure that causes problems.
****************************
typedef char T__Char;
typedef unsigned __int32 T__UInt32;
typedef enum {
KRON_ERR_SUCCESS=1000,
KRON_ERR_RQC_DUPLICATED=1601,
KRON_ERR_RQC_BAD_CONF_FILE=1700,
}T__KRON_Returned_Codes;
typedef struct {
T__KRON_Returned_Codesm__errorcode;
T__Charm__errormessage[150];
T__Charm__errorfile[256];
T__UInt32m__errorline;
}T__KRON_Struct_Error_Context;
*****************************
My marshaling of structures in the C# Wrapper is the following:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
public struct T__KRON_Struct_Error_Context
{
public T__KRON_Returned_Codes m__errorcode;
/// T__Char[150]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 150)]
public string m__errormessage;
/// T__Char[256]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 256)]
public string m__errorfile;
/// T__UInt32->unsigned int
public uint m__errorline;
}
Now,the problem. From my managed code I call an unmanaged function, that i wrapped before. The signature of this function is
[DllImport(@"KRON.dll")]
public static extern T__KRON_Struct_Error_Context KRON_RecordQualityCheck(...some parameter...);
This unmanaged function would return the T__KRON_Struct_Error_Context, but if i call it, i obtain the well known exception "Method's type signature is not PInvoke compatible". THe problem IS NOT in the parameters that i pass to the function,the problem is in the T__KRON_Struct_Error_Context because if i change the marshaling of T__KRON_Struct_Error_Context in this way:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi )]
public struct T__KRON_Struct_Error_Context
{
public T__KRON_Returned_Codes m__errorcode;
public byte m__errormessageChar1;
public byte m__errormessageChar2;
public byte m__errormessageChar3;
(i could add until 150 declaration of single byte!)
};
so the call works and i'm able to read in the byte "m__errormessageChar1" the first character of the original m_errorMessage, in the byte "m__errormessageChar2" the second character and so on...without exception of "P/Invoke not compatible".
The question is: What is wrong in my marshaling? I tried also to replace the type "string" with "char[]" or "byte[]" or "StringBuilder" and a lot of others temptatives.....but nothing to do...It doesn't work.
Thanks in advance for your help.