c#调用c/c++ dll方法

985 阅读3分钟

方法

  1. swig c# 示例

Example for SWIG to wrap C++ library in .Net 6 (iamsorush.com)

  1. 官方文档:

本机互操作性 - .NET | Microsoft Docs

  1. p/invok处理union类型时:

P/invoke with unions in C# | yizhang82’s blog

P/Invoke各种总结(八、在C#中使用Union联合体) - 编程猎人 (programminghunter.com)

C# 中的并集 - 与非对象字段错误地对齐或重叠|易学教程 (e-learn.cn)

  1. p/invok处理指针类型时:

P/Invoke各种总结(五、在C#中使用指针类型) - zhaotianff - 博客园 (cnblogs.com)

各种绑定工具对比

SWIG vs djinni | LibHunt

参考

cppsharp和swig哪个好用? - 知乎 (zhihu.com)

windows api 绑定

dahall/Vanara: A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers. (github.com)

dotnet/pinvoke: A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS. (github.com)

GitHub - microsoft/CsWin32: A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.

dll

dll export 函数查看

  1. vs developer cmd
dumpbin /exports *.dll

如何查看.dll文件函数接口?(DLL动态链接库)(查看动态链接库、查看接口、查看函数)(Visual Studio的dumpbin工具)(Dependency Walker)_dll查看器-CSDN博客

c/c++ 导出函数

导出 C 函数以用于 C 或 C++ 语言可执行文件 | Microsoft Learn

示例:

exportl_dll.h

#pragma once
#include "./Cnc.h"
#ifdef __cplusplus
extern "C" {
#endif
	__declspec(dllexport) CncConnectionInterface* new_cnc(char* hostName, char* hostAddress, int hostPort);

	__declspec(dllexport) void del_cnc(CncConnectionInterface* cnc);

	__declspec(dllexport) bool isConnect(CncConnectionInterface* cnc);

	__declspec(dllexport) bool connect(CncConnectionInterface* cnc);

	__declspec(dllexport) bool dis_connect(CncConnectionInterface* cnc);

	__declspec(dllexport) char* read(CncConnectionInterface* cnc, char* param, char* sub_param);

	__declspec(dllexport) void del_read(char* v);

#ifdef __cplusplus
}
#endif

exportl_dll.cpp

#include "exportl_dll.h"

CncConnectionInterface* new_cnc(char* hostName, char* hostAddress, int hostPort) {
	return Cnc::CreateConnectionInterface(hostName, hostAddress, hostPort);
}

void del_cnc(CncConnectionInterface* cnc) {
	delete cnc;
	cnc = nullptr;
}

bool isConnect(CncConnectionInterface* cnc) {
	return cnc->CncConnectionEstabilished();
}

bool connect(CncConnectionInterface* cnc) {
	return cnc->ConnectCnc();
}

bool dis_connect(CncConnectionInterface* cnc) {
	return cnc->DisconnectCnc();
}

char* read(CncConnectionInterface* cnc, char* param, char* sub_param) {
	std::string v = cnc->ReadCncStringParameter(param, sub_param);
	char* vv = new char[v.length() + 1];
	std::strcpy(vv, v.c_str());
	return vv;
}

void del_read(char* v) {
	delete[] v;
	v = nullptr;
}

dumpbin 查看函数暴露情况:

Dump of file lib_fidia.dll

File Type: DLL

  Section contains the following exports for lib_fidia.dll

    00000000 characteristics
    FFFFFFFF time date stamp
        0.00 version
           1 ordinal base
          13 number of functions
          13 number of names

    ordinal hint RVA      name
    
          1    6 00024555 connect = @ILT+1360(_connect)
          2    7 00025B0D del_cnc = @ILT+6920(_del_cnc)
          3    8 00025E19 del_read = @ILT+7700(_del_read)
          4    9 000251CB dis_connect = @ILT+4550(_dis_connect)
          5    A 00025B35 isConnect = @ILT+6960(_isConnect)
          6    B 00024672 new_cnc = @ILT+1645(_new_cnc)
          7    C 00025702 read = @ILT+5885(_read)

  Summary

        1000 .00cfg
        2000 .data
        4000 .idata
        1000 .msvcjmc
        C000 .rdata
        4000 .reloc
        1000 .rsrc
       4F000 .text
       23000 .textbss
        1000 .tls