简体   繁体   中英

Using a c++ dll within a c# project

I am hoping someone can help me. i have a dll file called LC4_Comm_Lib.dll which allows communication to a device, however im trying to use it in a c# project. Honestly i am unaware if this will work at all, the dll file came with ah file

    /*
=======================================================================================================================
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

I have got the communication to work in a C++ project however i was wondering if it is at all possible to get it to work in my c# project (guessing without the use of the h file). I have tried many things using DLLImport and even adding it through references, also

 [DllImport("LC4_Comm_Lib.dll", CallingConvention = CallingConvention.Cdecl)]

I am not sure if i am wasting my time, so thought i would double check

you can use C++ DLL in C# code without using your.h file. First of all you have to do DLL import. Here i am creating a class MFCdllConnector to do the operation.

 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);

Hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM