在 c# 项目中使用 c++ dll
Using a c++ dll within a c# project
我希望有人能帮助我。 我有一个名为 LC4_Comm_Lib.dll 的 DLL 文件,它允许与设备通信,但是我试图在 C# 项目中使用它。老实说,我不知道这是否有效,dll 文件附带一个 h 文件
/*
=======================================================================================================================
INFO
=======================================================================================================================
Title : LC4_Comm_Lib - Function that are writen for LC4
Author : Slobodan Milosevic
Revision : 1.0.0.1
Date : 02.11.2015.
=======================================================================================================================
CHANGE LOG
=======================================================================================================================
1.0.0.0 : Initial Release
1.0.0.1 : Brasil update2
=======================================================================================================================
*/
#pragma once
/// Added to enforce __cdecl calling convetion
#define CALL __cdecl
#ifdef LC4_COMM_LIB_EXPORTS
#define COMMLIB_LC4_API __declspec(dllexport)
#else
#define COMMLIB_LC4_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct LC4SystemInformation{
char CalibrationDate[64];
char Calibrationvalidity;
char BSMInumber[64];
char LC4_fw[64];
char PL4_fw[64];
char PL4_checksum[64];
}LC4SystemInformation, *ptrLC4SystemInformation;
/* open usb communication */
long COMMLIB_LC4_API Open_USB_Comm();
/* open wifi communication */
long COMMLIB_LC4_API Open_WiFi_Comm(const char* ipAddress);
/* set encryption key */
long COMMLIB_LC4_API Set_Encryption_Key(const char* key);
/* decrypt encrypted file */
long COMMLIB_LC4_API Decrypt_File(const char* encryptedFilePath, const char* _decryptedFilePath);
/* create list of files */
long COMMLIB_LC4_API Create_Available_File_List(int type, const char* time, const char* listPath);
/* get file from device */
long COMMLIB_LC4_API Get_File(const char* Lc4_Path, const char* pc_Path, bool encFlag);
/* close communication whith LC4 device */
void COMMLIB_LC4_API Close_Comm();
/* power off LC4 device */
long COMMLIB_LC4_API Power_Down();
/* get system information */
long COMMLIB_LC4_API Get_SysInfo(ptrLC4SystemInformation sysInfo);
/* set settings path */
void COMMLIB_LC4_API Set_Settings_Path(const char* errorFilePath);
/* get error code*/
long COMMLIB_LC4_API Get_Error_Code();
/* get error string*/
size_t COMMLIB_LC4_API Get_Error_String(char* errStr);
/* get current file progres - for downloaded file */
size_t COMMLIB_LC4_API Get_File_Progress();
/* get size of file */
size_t COMMLIB_LC4_API Get_File_Size();
/* Serial Number */
int COMMLIB_LC4_API cmd_GetLidarSerialNo(char *);
#ifdef __cplusplus
}
#endif
我已经让通信在一个C++项目中工作,但是我想知道是否有可能让它在我的 c# 项目中工作(在不使用 h 文件的情况下猜测(。我已经尝试了很多使用 DLLImport 的东西,甚至通过引用添加它,也
[DllImport("LC4_Comm_Lib.dll", CallingConvention = CallingConvention.Cdecl)]
我不确定我是否在浪费时间,所以我想我会仔细检查
可以在C# 代码中使用C++ DLL,而无需使用 .h 文件。 首先,您必须执行DLL导入。在这里,我正在创建一个类 MFCdllConnector 来执行该操作。
class MFCdllConnector { [DllImport( "LC4_Comm_Lib.dll", CallingConvention = CallingConvention.Cdecl ) ] public static extern <your exporting function> //please find below example public static extern int MyExampleExportFunction([MarshalAsAttribute(UnmanagedType.LPStr)]string para1, [MarshalAsAttribute(UnmanagedType.LPStr)] string para2,[MarshalAsAttribute(UnmanagedType.LPWStr)] ref StringBuilder output); } //You can call this function like below int return = MFCdllConnector.MyExampleExportFunction(para1, para2, ref OUTPUT);
希望这会有所帮助
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- 从C++dll访问C#中的一行主要参数
- 链接到自行创建的dll失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- C++:将外部库链接到dll库
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 不同的Visual Studio版本中缺少.dll
- 从DLL中删除类的实例
- 如何包装第三方DLL在R中使用
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Python ctypes:不会按预期加载 dll
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL