C++生成DLL给C#调用
1、添加C++动态链接库(DLL)
2、添加头文件 test.h
#ifndef TEST_H #define TEST_H // 添加要在此处预编译的标头 #include "framework.h" #endif //TEST_H extern "C" _declspec(dllexport) int GetTxtStr(char* in_str, char* out_str);
2、添加cpp文件 test.cpp
int GetTextStr(char* in_str,char* out_str){ char buff[1024]; memset(buff, 0, sizeof(buff)); char *test = "test" strcpy(instr,buff); strcat(buff,test2); strcpy(out_str,buff); return TRUE }
3、在C#项目里如下
[DllImport("test.dll", EntryPoint = "GetTextStr", CallingConvention = CallingConvention.Cdecl)]
static extern int GetTextStr(string in_str, [MarshalAs(UnmanagedType.LPStr)] StringBuilder out_str);
static void Main(string[] args)
{
StringBuilder str = StringBuilder();
GetTextStr("123456", str);
Console.WriteLine(str);
}

浙公网安备 33010602011771号