c# - calling unmanaged function char returns char * -
I have a function in unmanaged C / C ++ code (DLL) which gives a four array structure. I have created C # struct to get this return value to call function. And call this function call uppon I get 'System.Runtime.InteropServices.MarshalDirectiveException'
This is a declaration:
typedef struct T_SAMPLE_STRUCT {integer number; Four lessons [20]; } SAMPLE_STRUCT; SAMPLE_STRUCT Sample Function (SAMPLE_STRUCT SS); This is C # declaration:
struct SAMPLE_STRUCT {public ANN; Public string text; } Class Dllwrapper {[DllImport ("samplecdll.dll")] Public static extern SAMPLE_STRUCT Sample Function (SAMPLE_STRUCT SS); } I am using 1-byte ASCII.
Do anyone have an indication or solution to do this?
The trick to change a C array member is using MarshalAs (UnmanagedType.ByValTStr). This CLR can be used as an array to be interpreted as an array, such as a normal non-inline array. Try these signature
[System.Runtime.InteropServices.StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind.Sequential, charset = System.Runtime.InteropServices.CharSet.Ansi)] public struct T_SAMPLE_STRUCT {/// integer public number number; /// Four [20] [System.Runtime.InteropServices.MarshalAsAttribute (System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 20)] Public String Text; } Public Partial Class NativeMethods {Return Return ///: SAMPLE_STRUCT- & gt; T_SAMPLE_STRUCT /// ss: SAMPLE_STRUCT- & gt; T_SAMPLE_STRUCT [System.Runtime.InteropServices.DllImportAttribute ( "& lt; unknown & gt;", EntryPoint = "sampleFunction")] public static extern T_SAMPLE_STRUCT sample function (T_SAMPLE_STRUCT SS); } This signature has been brought by the PinoVek Interop Assistant (available on CodeVix). It can automatically translate most pennvoke signatures from the original code to C # or VBnet.
Comments
Post a Comment