一、環境介紹
VS版本: VS2017
程式設計語言: C++
二、功能介紹
使用VS2017項目模闆建立C++動态庫生成工程,生成動态庫。然後再建立一個C++工程調用動态庫完成測試。
三、建立動态庫
#ifdef DLLLIBRARY_EXPORTS
#define DLLLIBRARY_API __declspec(dllexport)
#else
#define DLLLIBRARY_API __declspec(dllimport)
#endif
EXTERN_C DLLLIBRARY_API char* Version(void);
EXTERN_C DLLLIBRARY_API int sum(int a, int b);
static char version_str[] = "v20210524";
// 擷取版本号
DLLLIBRARY_API char* Version(void)
{
return version_str;
}
// 求和
DLLLIBRARY_API int sum(int a, int b)
{
int c = a + b;
return c;
}
寫好之後,編譯生成動态庫。
生成的庫檔案在解決方案目錄下:
四、建立C++工程調用動态庫
将第一步生成的庫檔案xxx.lib拷貝到目前測試工程目錄下:
編寫調用庫的測試代碼:
#include <iostream>
#pragma comment(lib, "DLLLIBRARY.lib")
extern "C"
{
extern __declspec(dllexport) char* Version(void);
extern __declspec(dllexport) int sum(int a, int b);
};
using namespace std;
int main()
{
int c = sum(12, 34);
cout << "求和:" << c << endl;
char *p = Version();
cout << "版本号:" << p;
}
寫完代碼後,直接編譯運作,會報錯提示缺少xxx.dll,接下來把xxx.dll拷貝到程式運作目錄下即可。
拷貝xxx.dll到程式運作目錄下:
再次編譯運作:
五、C#調用動态庫測試
建立一個C#控制台工程,準備調用DLL庫測試。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
[DllImport("DLLLIBRARY.dll", EntryPoint = "sum", CallingConvention = CallingConvention.Cdecl)]
extern static int sum(int a, int b);
[DllImport("DLLLIBRARY.dll", EntryPoint = "Version",CharSet =CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
extern static IntPtr Version();
static void Main(string[] args)
{
Console.WriteLine(sum(100,200));
Console.WriteLine(Marshal.PtrToStringAnsi(Version()));
Console.ReadKey();
}
}
}
編譯運作,提示無法加載xxx.dll,接下來将xxx.dll拷貝到程式運作目錄下即可。
拷貝xxx.dll到目前程式運作目錄下:
再次運作:
六、C#傳入C指針字元串參數
如果C的函數需要傳入字元串,C#可以這樣傳遞:
C函數原型:
static char version_str[] = "v20210524";
//擷取版本号
ECRSTOOLLIBRARY_API char* Version(void)
{
return version_str;
}
void GetApplicationDirPath(char * buff)
{
printf("傳入的參數:%s\n", buff);
}
C#代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace CSharp_LoadLibrary
{
class Program
{
[DllImport("ECRSTOOLLIBRARY.dll", EntryPoint = "Version", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
extern static IntPtr Version();
[DllImport("ECRSTOOLLIBRARY.dll", EntryPoint = "GetApplicationDirPath", CallingConvention = CallingConvention.Cdecl)]
extern static void GetApplicationDirPath(IntPtr text);
static void Main(string[] args)
{
//申請空間
IntPtr pBuff = Marshal.AllocHGlobal(100);
//将string轉為IntPtr類型
string str = "我是傳遞給C++的字元串";
pBuff = Marshal.StringToHGlobalAnsi(str);
//調用C++方法
GetApplicationDirPath(pBuff);
//釋放空間
Marshal.FreeHGlobal(pBuff);
Console.WriteLine(Marshal.PtrToStringAnsi(Version()));
Console.ReadKey();
}
}
}