2014-01-10 47 views
0

我有一个第三方C++ .lib,我想在C#应用程序中使用。如何引用没有类/名称空间的C++库?

我的意图是为.lib编写一个托管的C++包装器.dll,然后在我的C#应用​​程序中使用类似于the one outlined at this link的流程包含该包装器dll。

头文件的.LIB不表示任何类声明或命名空间,只是方法名称:

int CheckNamedVariable(const char* _name); 
int RegisterNamedVariable(const char* _name); 
double GetNamedVariableValue(int _id); 
double GetNamedVariableTypedValue(int _id, int _units); 
void SetNamedVariableValue(int _id, double _value); 
... (etc) 

我检查与DUMPBIN /出口的lib,看看我能搜集任何附加信息,并得到如下:

File Type: LIBRARY 

    Exports 

     ordinal name 

        [email protected]@[email protected] (double __cdecl AircraftVarGet(int,int,int)) 
        [email protected]@[email protected] (int __cdecl CheckNamedVariable(char const *)) 
        [email protected]@YGXXZ (void __stdcall DLLStart(void)) 
        [email protected]@YGXXZ (void __stdcall DLLStop(void)) 
        [email protected]@[email protected] (int __cdecl GetAircraftVarEnum(char const *)) 
        [email protected]@[email protected]@@Z (double __cdecl GetModuleVar(enum GAUGE_TOKEN)) 
        [email protected]@[email protected] (char const * __cdecl GetNameOfNamedVariable(int)) 
        [email protected]@[email protected] (double __cdecl GetNamedVariableTypedValue(int,int)) 
        [email protected]@[email protected] (double __cdecl GetNamedVariableValue(int)) 
        [email protected]@[email protected] (int __cdecl GetUnitsEnum(char const *)) 
        [email protected]@[email protected] (int __cdecl IsPanelWindowVisibleIdent(unsigned int)) 
        [email protected]@[email protected]@A (struct PANELS * Panels) 
        [email protected]@[email protected] (int __cdecl RegisterNamedVariable(char const *)) 
        [email protected]@[email protected] (void __cdecl SendKeyEvent(unsigned int,unsigned int)) 
        [email protected]@YA[email protected] (void __cdecl SetNamedVariableTypedValue(int,double,int)) 
        [email protected]@[email protected] (void __cdecl SetNamedVariableValue(int,double)) 
        _ImportTable 
        _Linkage 

    Summary 

      ED .debug$S 
      14 .idata$2 
      14 .idata$3 
      4 .idata$4 
      4 .idata$5 
      1A .idata$6 

在我的托管包装的项目,我可以链接输入指向的.lib,但我不知道如何调用在lib的方法(我挂利用教程命名空间和类)。如果它是C#/ VB项目中普通的旧DLL,我可以查看对象浏览器并了解如何调用库。

但由于我的C++无知,我在黑暗中。任何帮助赞赏。

编辑: 我尝试了创建具有相同名称的方法的方法包头中列出,如:

int VariableExporter::IsPanelWindowVisibleIdent(unsigned int panel_id) 
{ 
    return IsPanelWindowVisibleIdent(panel_id); 
} 

这不是在做什么,我倒是希望,这只是递归调用本身(直到它有 - 等待它 - 堆栈溢出)。

然后我试图重新命名的包装方法是这样的:

int VariableExporter::IsPanelWindowVisibleIdent_New_Name(unsigned int panel_id) 
{ 
    return IsPanelWindowVisibleIdent(panel_id); 
} 

如今,虽然C++ .DLL项目和C#项目都进行编译时,C#项目将引发FileNotFound异常,当它试图加载。 DLL。

+1

使用'extern“C”'输出到C#....(或C)的符号。 –

+0

请原谅我所问的无知,但......这不属于在.lib的源代码?我无法访问该源......只是二进制库和头文件。 – GojiraDeMonstah

回答

0

第一件事就是打电话给全球范围内的功能,你需要使用范围解析操作符:

int VariableExporter::IsPanelWindowVisibleIdent(unsigned int panel_id) 
{ 
    return ::IsPanelWindowVisibleIdent(panel_id); 
} 

没有必要(至少对于这个简单的例子),重命名你的方法。

其次,对于未发现DLL的问题,请检查该DLL是否存在于您的C# 应用程序的bin路径中。还要检查包装器DLL本身是否可加载(使用Dependency Walker)。还可以确定包装DLL和C#应用程序的性能也匹配。 64位应用程序无法加载32位DLL(反之亦然)。

相关问题