1. 输入数值的情况
1) C++ DLL中的函数定义
// if there is no "extern "C" " , we cannot use the functions in C#
// *****************Input value *************************************
CPPDLL_API
bool Inputbool( bool &bVal, bool * pVal, const bool cbVal);
CPPDLL_API BOOL InputBOOL(BOOL &bVal,BOOL * pVal,
const BOOL cbVal);
CPPDLL_API
short int InputShortInt(short int &iVal,short int * pVal,const short int ciVal);
CPPDLL_API
int InputInt(int &iVal,int * pVal,const int ciVal);
CPPDLL_API
long int InputLongInt(long int &iVal,long int * pVal,const long int ciVal);
CPPDLL_API
long InputLong(long &lVal,long * pVal,const long clVal);
CPPDLL_API
__int64 InputInt64(__int64 &iVal,__int64 * pVal,const __int64 ciVal);
CPPDLL_API
float InputFloat(float &fVal,float * pVal,const float cfVal);
CPPDLL_API
double InputDouble(double &dVal,double * pVal,const double cdVal);
// ***********************************************************************
2)C#中函数的Invoke
[
DllImport("CppDll.dll")]
public static extern bool Inputbool(ref/*out*/ bool bVal, ref/*out*/ bool pVal, bool cbVal);
[
DllImport("CppDll.dll")]
public static extern bool InputBOOL(out/*ref*/ bool bVal, out/*ref*/ bool pVal, bool cbVal);
[
DllImport("CppDll.dll")]
public static extern short InputShortInt(ref short iVal, ref short pVal, short ciVal);
[
DllImport("CppDll.dll")]
public static extern int InputInt(ref int iVal, ref int pVal, int ciVal);
[
DllImport("CppDll.dll")]
public static extern Int32 InputLongInt(ref Int32 iVal, ref Int32 pVal, Int32 ciVal);
[
DllImport("CppDll.dll")]
public static extern Int32 InputLong(ref Int32 lVal, ref Int32 pVal, Int32 clVal);
[
DllImport("CppDll.dll")]
public static extern Int64 InputInt64(ref Int64 lVal, ref Int64 pVal, Int64 clVal);
[
DllImport("CppDll.dll")]
public static extern float InputFloat(ref float lVal, ref float pVal, float clVal);
[
DllImport("CppDll.dll")]
public static extern double InputDouble(ref double lVal, ref double pVal, double clVal);
2. 输入字符串的情况
1)C++DLL中的函数定义
// *********************Input string *************************************
CPPDLL_API
char InputChar(char cc);
CPPDLL_API
wchar_t InputWChar(wchar_t wc);
CPPDLL_API LPCTSTR InputConstString(LPCWSTR cwStr,
const wchar_t* cwc);
CPPDLL_API LPCSTR InputConstChar(
const char* cc);
CPPDLL_API LPSTR InputString(
char* pCC);
CPPDLL_API LPTSTR InputWString(LPWSTR lpWStr,
wchar_t * lpWW);
CPPDLL_API HRESULT StringCopy(LPTSTR pszDest,
int size,LPCTSTR pszSrc);
// ***********************************************************************
2)C#中函数的Invoke
[
DllImport("CppDll.dll", CharSet = CharSet.Ansi)]
public static extern char InputChar(char cc);
[
DllImport("CppDll.dll", CharSet = CharSet.Unicode)]
public static extern char InputWChar(char wc);
[
DllImport("CppDll.dll",CharSet=CharSet.Unicode)]
public static extern string InputConstString(string str1, string str2);
[
DllImport("CppDll.dll", CharSet = CharSet.Ansi)]
public static extern string InputConstChar(string str1);
[
DllImport("CppDll.dll", CharSet = CharSet.Ansi)]
public static extern string InputString(string str1);
[
DllImport("CppDll.dll", CharSet = CharSet.Unicode)]
public static extern string InputWString(string str1, string str2);
[
DllImport("CppDll.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 StringCopy([MarshalAs(UnmanagedType.LPWStr)] string/*StringBuilder*/ str1, int size, string str2);
3.输入自定义结构体的情况
1)C++ DLL中的函数定义
// ************************* Input struct ********************************
enum
COUNTRY
{
USA = 0,
China =1,
Japan = 2
};
typedef
struct tagPerson
{
int Age;
LPCWSTR Name;
COUNTRY coutryName;
}PERSON;
CPPDLL_API
void MergeStruct(RECT rect1,RECT & rect2);
CPPDLL_API LPRECT ReturnStruct(RECT rect1,RECT & rect2);
CPPDLL_API
void PrintStructMessage(PERSON per);
// ***********************************************************************
2)C#中函数的Invoke
[
StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int bottom;
public int left;
public int right;
public int top;
}
public enum COUNTRY
{
USA = 0,
China = 1,
Japan = 2
};
[
StructLayout(LayoutKind.Sequential)]
public struct PERSON
{
public int Age;
[
MarshalAs(UnmanagedType.LPTStr)]
public string Name;
public COUNTRY coutryName;
};
[
DllImport("CppDll.dll")]
public static extern void MergeStruct(RECT rect1, ref RECT rect2);
[
DllImport("CppDll.dll")]
public static extern IntPtr ReturnStruct(RECT rect1, ref RECT rect2);
[
DllImport("CppDll.dll")]
public static extern void PrintStructMessage(PERSON per);
4. 输入回调函数的情况
1)C++DLL中的函数定义
// *************************Callback function ****************************
// If there is no "CALLBACK", Cal function will run failed.
typedef
int (CALLBACK *LPADD)(int a , int b);
CPPDLL_API
int Cal(LPADD lpAdd, int b);
// ***********************************************************************
2)C#中函数的Invoke
public
delegate int CallBackPtr(int a, int b);
class CallbackTest
{
[
DllImport("CppDll.dll")]
public static extern int Cal(CallBackPtr callPtr,int b);
public static int Add(int a, int b)
{
return a + b;
}
}