implement c function and parameter pointer struct to c#? - c#

I Have a problem about implementint of C++ DLL to C#
My Header File code is this :
typedef struct{
uchar Command[4];
ushort Lc;
uchar DataIn[512];
ushort Le;
}CON_SEND;
typedef struct{
ushort LenOut;
uchar DataOut[512];
uchar outA;
uchar outB;
}CON_RESP;
SDK_C_DLL_API int Csdk_Cmd(uchar port, CON_SEND * ConSend,CON_RESP * ConResp);
SDK_C_DLL_API int Csdk_Open(uchar port);
and my C# class
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CON_SEND
{
public byte[] Command;
public System.UInt16 Lc;
public byte[] DataIn;
public System.UInt16 Le;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CON_RESP
{
public System.UInt16 LenOut;
public byte[] DataOut;
public byte outA;
public byte outB;
}
[DllImport("SDK_C.dll")]
public static extern System.Int16 Csdk_Cmd(System.Byte port, ref CON_SEND ConSend, ref CON_RESP ConResp);
[DllImport("SDK_C.dll")]
public static extern System.Int16 Csdk_Open(System.Byte port);
now i use Csdk_Cmd; is working no problem this code :
if(SDK_C.Csdk_Open(0x00)== 0)
lblStatus.Text = "Gate 0 is busy";
else
lblStatus.Text = "You can use Gate 0";
but when i try to use this code Csdk_Cmd given "NotSupportedException"
CON_SEND SendCon = new CON_SEND();
SendCon.Command = new byte[] { 0xE0, 0xA0, 0xC4, 0x72 };
SendCon.DataIn = new byte[]{0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xA0};
SendCon.Lc = 0x0007;
SendCon.Le = 0x0000;
CON_RESP RespCon = new CON_RESP();
RespCon.DataOut = new byte[512];
SDK_C.Csdk_Cmd(0, ref SendCon, ref RespCon); // THIS LINE

Your struct declarations are not correct. The C code has inline byte arrays and simply put, they do not match the default marshalling for byte[]. The easiest way to fix it is to use MarshalAs(UnmanagedType.ByValArray). Like this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CON_SEND
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Command;
public ushort Lc;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public byte[] DataIn;
public ushort Le;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CON_RESP
{
public ushort LenOut;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public byte[] DataOut;
public byte outA;
public byte outB;
}
Your functions also have incorrect return value. A C int maps to C# int. So they declarations should be:
[DllImport("SDK_C.dll")]
public static extern int Csdk_Cmd(byte port, ref CON_SEND ConSend,
ref CON_RESP ConResp);
[DllImport("SDK_C.dll")]
public static extern int Csdk_Open(byte port);
The other thing to double check is the calling convention. The C# function above use the default of Stdcall. Are you sure that the C code also does? Since there is nothing specified between the return value and the function name, I suspect that the functions are actually Cdecl. In which case you need:
[DllImport("SDK_C.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Csdk_Cmd(byte port, ref CON_SEND ConSend,
ref CON_RESP ConResp);
[DllImport("SDK_C.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Csdk_Open(byte port);

This is more of a guess really, but you are declaring your structs with inline (fixed) arrays in C++, so you'll need to tell c# to to do that too. You can do this with Fixed Size Buffers:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CON_SEND
{
public fixed byte Command[4];
public System.UInt16 Lc;
public fixed byte DataIn[512];
public System.UInt16 Le;
}
Warning: this code needs to be compiled with unsafe and appear in unsafe block.
Alternatively, you could look into better defining the marshaling attributes for your struct:
http://www.codeproject.com/Articles/66244/Marshaling-with-C-Chapter-2-Marshaling-Simple-Type
(See #DavidHeffernan, answer below)

Related

C struct and use C dll in C#

everyone, I'm newbie in C#, and I have a problem with using "C" dll in C#, below is the C side code which work fine.
// myCode.h
typedef struct _SubABC
{
unsigned short WordCount;
unsigned char *WordData;
unsigned char SpeedUp;
} SubABC;
typedef struct _ABC
{
unsigned char SubFontNum;
SubABC subABC[5];
} ABC
extern __declspec(dllimport) int __stdcall MY_SetSth(unsigned char SerialNum, ABC *pABC);
//myCode.c
ABC myFun = { '\0' };
unsigned char text_c[] =
{
0x00,0x27,0xff,0xA5,0xC3, 0x00,0x27,0xff,0xA6,0x26, 0x00,0x23,0xff,0xA7,0xAE, 0x00,0x27,0xff,0xBA,0x7E,
0x00,0x27,0xff,0xC1,0x52, 0x00,0x27,0xff,0xAC,0xF9, 0x00,0x27,0xff,0x20,0x00,0x27,0xff,0x31, 0x00,0x27,0xff,0x20, 0x00,0x27,0xff,0xA4,0xC0, 0x00,0x27,0xff,0xC4,0xC1,
};
myFun.SubFontNum = 1;
myFun.subMABC[0].WordCount = sizeof(text_c);
myFun.subMABC[0].WordData = text_c;
myFun.subMABC[0].SpeedUp = 0;
int retvalue = MY_SetSth(1, &myFun);
and my C# code is below
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SubABC
{
public ushort WordCount;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] WordData;
public byte SpeedUp;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ABC
{
public byte SubFontNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public SubABC[] subABC;
}
[DllImport("myDLL.dll", CharSet = CharSet.Ansi)]
public extern static int MY_SetSth(byte SerialNum, ref ABC pABC);
ABC myFun = new ABC();
ABC.subABC = new SubABC[5];
unsigned char text_c[] =
{
0x00,0x27,0xff,0xA5,0xC3, 0x00,0x27,0xff,0xA6,0x26, 0x00,0x23,0xff,0xA7,0xAE, 0x00,0x27,0xff,0xBA,0x7E,
0x00,0x27,0xff,0xC1,0x52, 0x00,0x27,0xff,0xAC,0xF9, 0x00,0x27,0xff,0x20,0x00,0x27,0xff,0x31, 0x00,0x27,0xff,0x20, 0x00,0x27,0xff,0xA4,0xC0, 0x00,0x27,0xff,0xC4,0xC1,
};
myFun.SubFontNum = 1;
myFun.subMABC[0].WordCount = (ushort)text_c.Length;
myFun.subMABC[0].WordData = text_c;
myFun.subMABC[0].SpeedUp = 0;
int retvalue = MY_SetSth(1, ref myFun);
I always get TypeLoadException: Cannot marshal field 'WordData' of type 'subABC': Invalid managed/unmanaged type combination (Array fields must be paired with ByValArray or SafeArray), because WordData is unknown size, I can't use ByValArray, and SafeArray get AccessViolationException: Attempted to read or write protected memory
I try to use IntPtr like below
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SubABC
{
public ushort WordCount;
public IntPtr WordData;
public byte SpeedUp;
}
ABC.subABC[0].WordData = Marshal.AllocHGlobal(text_c.Length); ;
Marshal.Copy(text_c, 0, ABC.subABC[0].WordData, text_c.Length);
int retvalue = MY_SetSth(1, ref myFun);
I get error message AccessViolationException: Attempted to read or write protected memory
Can anyone help me? thank a lot.

How to correctly parse IntPtr to Object in C# when marshaling from C++

I'm invoking a C++ function from within C#.
The function I am trying to invoke returns following C++ struct type result:
LPWFSCIMNOTETYPELIST lpNoteTypeList;
typedef struct _wfs_cim_note_type_list
{
USHORT usNumOfNoteTypes;
LPWFSCIMNOTETYPE *lppNoteTypes;
} WFSCIMNOTETYPELIST, *LPWFSCIMNOTETYPELIST;
typedef struct _wfs_cim_note_type
{
USHORT usNoteID;
CHAR cCurrencyID[3];
ULONG ulValues;
USHORT usRelease;
BOOL bConfigured;
} WFSCIMNOTETYPE, *LPWFSCIMNOTETYPE;
I found the suitable struct in C#:
[StructLayout(LayoutKind.Sequential, Pack = XFSConstants.STRUCTPACKSIZE, CharSet = CharSet.Ansi)]
public unsafe struct WfsCimNoteType
{
public ushort usNoteID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public char[] cCurrencyID;
public uint ulValues;
public ushort usRelease;
public byte bConfigured;
}
[StructLayout(LayoutKind.Sequential, Pack = XFSConstants.STRUCTPACKSIZE, CharSet = CharSet.Ansi)]
public unsafe struct WfsCimNoteTypeList
{
public ushort usNumOfNoteTypes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public WfsCimNoteType[] lppNoteTypes;
}
I used the following code to parse the result from invoked function:
WfsCimNoteTypeList data = new WfsCimNoteTypeList();
XFSUtil.PtrToStructure<WfsCimNoteTypeList>(lpBuffer, ref data);
(here lbBuffer is IntPtr)
XFSUtil.PtrToStructure is defined like this:
public static void PtrToStructure<T>(IntPtr ptr, ref T p) where T : struct
{
p = (T)Marshal.PtrToStructure(ptr, typeof(T));
}
But after parsing from IntPtr to WfsCimNoteType object array, I am getting different values???

Convert c++ struct to c# struct. Error output

My C++ dll has two struct.
C++:
typedef struct interface_info {
DWORD index;
DWORD ipv4Addr;
BYTE macAddr[6];
DWORD gateway;
}I_INFO;
typedef struct user_info {
DWORD uniqueid;
BYTE userid[32];
BYTE pwd[32];
BYTE info[5][64];
}U_INFO;
C#:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct I_INFO
{
public uint Index;
public uint ipv4Addr;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] macAddr;
public uint gateway;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct U_INFO
{
public uint uniqueid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public byte[] userid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public byte[] pwd;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 320)]
public byte[] info;
};
I'm calling this method:
C++:
void GetNetworkInfo(I_INFO *n); // Show interface Info
dword RegNewUser(U_INFO *u); // Save to test.ini
C#:
[DllImport("TestNet.dll")]
public static extern void GetNetworkInfo(ref I_INFO n);
[DllImport("TestNet.dll")]
public static extern uint RegNewUser(ref U_INFO u);
I'm calling this way:
C#:
I_INFO ii = new I_INFO();
GetNetworkInfo(ref ii);
MessageBox.Show(ii.index+ "\n"
+ Long2ip(ii.ipAddr) + "\n"
+ Long2ip(ii.gateway) + "\n"
+ Bytes2Str(ii.macAddr);
....
I_INFOuser = new I_INFO
{
uid = Encoding.ASCII.GetBytes("testUser"),
pwd = Encoding.ASCII.GetBytes("testPassword"),
info = Encoding.ASCII.GetBytes("hello world!")
};
try
{
RegNewUser(ref user);
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
The output is:
GetNetworkInfo returns normal results, but RegNewUser outputs this error:
Error: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.
How do I fix this?

Marshalling a C# structure

I am trying to serialize the following c# structure:
[Serializable]
[StructLayout(LayoutKind.Sequential, Size = 70, CharSet = CharSet.Ansi)]
public struct USSDContinueModel
{
[MarshalAs(UnmanagedType.U4)]
public uint Command_Length;
[MarshalAs(UnmanagedType.U4)]
public uint Command_ID;
[MarshalAs(UnmanagedType.U4)]
public uint Command_Status;
[MarshalAs(UnmanagedType.U4)]
public uint Sender_ID;
[MarshalAs(UnmanagedType.U4)]
public uint Receiver_ID;
[MarshalAs(UnmanagedType.U1)]
public uint Ussd_Version;
[MarshalAs(UnmanagedType.U1)]
public uint Ussd_Op_Type;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string MsIsdn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string Service_Code;
[MarshalAs(UnmanagedType.U1)]
public uint Code_Scheme;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 182)]
public string Ussd_Content;
// Calling this method will return a byte array with the contents
// of the struct ready to be sent via the tcp socket.
public byte[] Serialize()
{
// allocate a byte array for the struct data
var buffer = new byte[Marshal.SizeOf(typeof(USSDContinueModel))];
// Allocate a GCHandle and get the array pointer
var gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);
var pBuffer = gch.AddrOfPinnedObject();
// copy data from struct to array and unpin the gc pointer
Marshal.StructureToPtr(this, pBuffer, false);
gch.Free();
return buffer;
}
// this method will deserialize a byte array into the struct.
public void Deserialize(ref byte[] data)
{
var gch = GCHandle.Alloc(data, GCHandleType.Pinned);
this = (USSDContinueModel)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(USSDContinueModel));
gch.Free();
}
}
when i try to serialize an instance of the structure say:
public USSDContinueModel continueModel;
continueModel.Command_Length = 174;
continueModel.Command_ID = 0x00000070;
continueModel.Command_Status = 0;
continueModel.Sender_ID = 0x01000005;
continueModel.Receiver_ID = 0x2900AB12;
continueModel.Ussd_Version = 0x20;
continueModel.Ussd_Op_Type = 0x01;
continueModel.MsIsdn = "08098765476";
continueModel.Service_Code = "*308";
continueModel.Code_Scheme = 0x44;
continueModel.Ussd_Content = "1. Continue if you are 18+ 2. Exit i";
I keep getting the error "Type: USSDcontinueModel cannot be marshalled as an unmanaged structure; no meaningful size or offset can be computed".
I noticed this happens when i set the Ussd_Version, Ussd_Op_Type and Code_Scheme as [MarshalAs(UnmanagedType.U1)] but it works fine with [MarshalAs(UnmanagedType.U4)].
Is [MarshalAs(UnmanagedType.U1)] unmarshallable? what do i do?
The problem is that the definition:
[MarshalAs(UnmanagedType.U1)]
public uint Ussd_Op_Type;
is ambiguous as to what size the members could be. If the interop marshal uses the member's size, it will calculate 4 bytes, whereas if it uses the MarshalAs attribute it will calculate 1 byte. Changing the member to use the correct size type,
[MarshalAs(UnmanagedType.U1)]
public byte Ussd_Op_Type;
should fix the issue.
So after taking #theB's recommendation of declaring the U1 data types as bytes. The problem was resolved. The updated structure definition follows:
[Serializable]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack=1)]
public struct USSDContinueModel
{
[MarshalAs(UnmanagedType.U4)]
public uint Command_Length;
[MarshalAs(UnmanagedType.U4)]
public uint Command_ID;
[MarshalAs(UnmanagedType.U4)]
public uint Command_Status;
[MarshalAs(UnmanagedType.U4)]
public uint Sender_ID;
[MarshalAs(UnmanagedType.U4)]
public uint Receiver_ID;
[MarshalAs(UnmanagedType.U1)]
public byte Ussd_Version;
[MarshalAs(UnmanagedType.U1)]
public byte Ussd_Op_Type;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string MsIsdn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string Service_Code;
[MarshalAs(UnmanagedType.U1)]
public byte Code_Scheme;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 182)]
public string Ussd_Content;
// Calling this method will return a byte array with the contents
// of the struct ready to be sent via the tcp socket.
public byte[] Serialize()
{
// allocate a byte array for the struct data
var buffer = new byte[Marshal.SizeOf(typeof(USSDContinueModel))];
// Allocate a GCHandle and get the array pointer
var gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);
var pBuffer = gch.AddrOfPinnedObject();
// copy data from struct to array and unpin the gc pointer
Marshal.StructureToPtr(this, pBuffer, false);
gch.Free();
return buffer;
}
// this method will deserialize a byte array into the struct.
public void Deserialize(ref byte[] data)
{
var gch = GCHandle.Alloc(data, GCHandleType.Pinned);
this = (USSDContinueModel)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(USSDContinueModel));
gch.Free();
}
}

Mapped c++ struct to c# struct

I am having trouble mapping the following c++ struct in c# for memory mapping purposes.
typedef struct
{
DWORD Data1;
DWORD Data2;
double AverageData[2];
DWORD NumData[2];
} DIRECTIONAL_STATS;
typedef struct
{
DIRECTIONAL_STATS DirectionStats[2];
char Name[100];
int StatLength;
} OTHER_STATS;
typedef struct
{
OTHER_STATS SystemStat[64][2];
long LastUpdate;
}STATS;
Can someone please shed some lights on how to achieve the mapping? Mapping from c++ type to c# type is fine for me.
However, I don't have any clue how to map nested struct and the explicit array size required.
Update 1:
Managed to map to following c# code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct STATS
{
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 128)]
public OTHER_STATS[] SystemStat; //64x2
public long LastUpdate;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct OTHER_STATS
{
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 2)]
public DIRECTIONAL_STATS[] DirectionStats;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public char[] Name;
public int StatLength;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DIRECTIONAL_STATS
{
public UInt16 Data1;
public UInt16 Data2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public double[] AverageData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public UInt16[] NumColdRes;
}
Unsure about the following mapping since it is a multidimensional arrays.
public SENTRY_PERFORMANCE_STATS[] Sentry; //64x2
Using the above mapping, it is able to run without any exception, however, the data mapped into OTHER_STATS are all wrong.
Can anyone see what I have done wrong?
Thanks.

Categories

Resources