簡體   English   中英

在 c# 項目中使用 c++ dll

[英]Using a c++ dll within a c# project

我希望有人能幫助我。 我有一個名為 LC4_Comm_Lib.dll 的 dll 文件,它允許與設備通信,但是我試圖在 c# 項目中使用它。 老實說,我不知道這是否可行,dll 文件附帶 ah 文件

    /*
=======================================================================================================================
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 代碼,而無需使用 your.h 文件。 首先你必須做 DLL 導入。 在這里,我正在創建一個 class 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);

希望這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM