使用c++;c#项目中的dll

使用c++;c#项目中的dll,c#,c++,dll,C#,C++,Dll,我希望有人能帮助我。我有一个名为LC4_Comm_Lib.dll的dll文件,它允许与设备通信,但我试图在c#项目中使用它。老实说,我不知道这是否会在所有的工作,dll文件来了一个h文件 /* ======================================================================================================================= INFO ========================

我希望有人能帮助我。我有一个名为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)]
我不确定我是否在浪费时间,所以我想我会仔细检查

< p>你可以在C++代码中使用C++ dll而不使用.h文件。 首先你要做的是DLL导入。在这里,我创建了一个类MFCdllConnector来执行该操作

class MFCdllConnector
{ 
[DllImport(“LC4_Comm_Lib.dll”,CallingConvention=CallingConvention.Cdecl)]
公共静态外部程序//请查找以下示例
public static extern int MyExampleExportFunction([MarshalAsAttribute(UnmanagedType.LPStr)]字符串para1[MarshalAsAttribute(UnmanagedType.LPStr)]字符串para2[MarshalAsAttribute(UnmanagedType.LPWStr)]参考StringBuilder输出;
}
//您可以像下面那样调用此函数
int return=MFCdllConnector.MyExampleExportFunction(para1、para2、ref输出);

希望这有助于

<代码> dLimPurt//Cord>是一个使用C++语言的C++项目的好方法。您看到的错误是什么?此外,您尝试使用的函数是否导出为
extern“C”
?@rhughs这些函数都使用宏条件标记为
extern“C”
。您是否调用由种族灭绝独裁者编写的DLL???另外,你是C#methods
static extern
?*你的。我试着像这样导入函数,
[DllImport(“LC4_Comm_Lib.dll”,CallingConvention=CallingConvention.Cdecl,EntryPoint=“Open_USB_Comm”)]静态外部长开_USB_Comm()
然后像这样在我的程序中调用它
Open\u USB\u Comm()
但是没有工作,我得到一个错误,说System.BadImageFormatException
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);