在C#中調用C++Dll函數的方法

 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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章