C++ DLL設計 & C#呼叫使用(可以要C++動態配置記憶體)
C++ DLL設計 &
C#
呼叫使用(可以要C++動態配置記憶體)
//C++
typedef struct _CardReaderState
{
char
strAutoRead[10];
char
strWithBeep[10];
char
strWithLED[10];
char
strSameCardDetection[10];
char
strGreenMode[10];
}CardReaderState;
extern “C”{
__declspec(dllexport)
int SYRIS_readStateCardReader(CardReaderState*& res);
__declspec(dllexport)
int SYRIS_finish(CardReaderInfo* res,CardReaderState* res1,CardInfo* res2);
}
int
SYRIS_readStateCardReader(CardReaderState*& res)
{
res=new
CardReaderState[1];
return
1;
}
int SYRIS_finish(CardReaderInfo*
res,CardReaderState* res1,CardInfo* res2)
{
if(res)
{
delete
[] res;
}
if(res1)
{
delete
[] res1;
}
if(res2)
{
delete
[] res2;
}
return
1;
}
//C#
using
System.Runtime.InteropServices;//USE DLL
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CardReaderState
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string strAutoRead;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= 10)]
public string strWithBeep;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string strWithLED;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string strSameCardDetection;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string strGreenMode;
}
[DllImport(“./SYRIS_CS_LIB.dll”, CallingConvention =
CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint =
“SYRIS_readStateCardReader”)]
public static extern int SYRIS_readStateCardReader(ref IntPtr ptr);
[DllImport(“./SYRIS_CS_LIB.dll”,
CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint
= “SYRIS_finish”)]
public
static extern int SYRIS_finish(IntPtr ptr, IntPtr ptr1, IntPtr ptr2);