Builder C ++调用VC ++类 [英] Builder C++ calling VC++ class

查看:93
本文介绍了Builder C ++调用VC ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用VC ++编译了一个Hunspell DLL,其中包含一个类...
现在我想在Builder C ++ 2006中调用该DLL以使用其功能...我该怎么做?

I have compiled a Hunspell DLL with VC++ which contains a class... Now I want to call that DLL in Builder C++ 2006 to use its functions...how I can do that?

我尝试过:

typedef Hunspell * (CALLBACK *fpoint)(char *aff_file, char *dict_file);
fp pHunspell = (fp)GetProcAddress(handle_Hunspell, "hunspell_initialize");
if (pHunspell) {
  Hunspell* obj = (Hunspell *)pHunspell("..\hunspelldic\en_US.aff", "..\hunspelldic\en_US.dic");
  obj->add_dic("..\hunspelldic\it_IT.aff", "..\hunspelldic\it_IT.dic");
}

问题是,如果在BuilderC ++ 2006中我可以在按shift-space后显示函数

the matter is if in BuilderC++ 2006 I can show functions after pressing shift-space after obj-> but looks like it doesn't really recognize class functions and it keeps giving out Unresolved external '__fastcall Hunspell::add_dic(...);' referenced from....

将VC ++ dll调用到Builder C ++中的确切方法是什么?预先感谢大家...

What would be the exact way to call a VC++ dll into Builder C++? Thanks in advance to everyone...

干杯,
Luigino

Cheers, Luigino

推荐答案

首先,您必须 #include 您的 DLL 的导入头文件(包括类,const定义...)

first you must #include a import header file for your DLL (including classes, consts defines...)

之后,有2种方法:


  1. 静态DLL链接

很简单,但有时不能与 MSVC ++ DLL 一起使用。您需要为 DLL 添加一个项目 LIB ,您可以使用 implib.exe 在Borland bin文件夹中创建该文件,但是您可能必须使用命令行切换以更正转换已使用的转换,直到构建器可以解决所有外部问题为止。例如:

is easy but sometimes do not work with MSVC++ DLLs. You need Add to project a LIB file for your DLL which you can create with implib.exe in Borland bin folder but you probably must play with command line switches to correct convert used mangling until builder can resolve all externals. for example:

implib.exe -c -f -a glut32.lib glut32.dll 

如果您的lib文件不正确,则编译器将添加您未解决的外部错误。另外,来自 MSVC ++ obj / lib 文件与Borland / Intel不兼容,因为MS使用其非标准格式。在这种情况下,Borland将大喊错误的OMF 之类的东西,因此 implib 实用工具

if you have incorrect lib file then compiler will add your unresolved external errors. Also obj/lib files from MSVC++ are incompatible with Borland/Intel because MS use their nonstandard format. In such case Borland will shout something like wrong OMF, hence the implib utility

动态DLL链接

在下面的示例中,我从 DLL 链接了2个函数。对于链接类,必须链接导入的所有类的所有使用的方法。另外,您可能会遇到 DLL中的名称修改(类似于静态链接)的问题,因此使用一些工具来探索DLL (我使用DLL Export Viewer),它向您展示了真实的功能 DLL 中的名称,必须在 GetProcAddress 中使用。下面是一个示例:

in example bellow I link 2 functions from a DLL. For linking class you must link all used methods of all classes you import. Also you might have problems with name mangling in DLL (similar to static link) so use some tool to explore dll (I use DLL Export Viewer ) which shows you the real function names in DLL which you must use in GetProcAddress. Here an example:

HANDLE hdll;
typedef BOOL(__stdcall *_InitRemoteCtrl)(HWND); _InitRemoteCtrl InitRemoteCtrl;
typedef DWORD(__stdcall *_ReadRemoteData)();    _ReadRemoteData ReadRemoteData;

__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
    {
    hdll=LoadLibrary("./RemtCtrl.dll");
    if (hdll==0) Application->Terminate();
    InitRemoteCtrl=(_InitRemoteCtrl)GetProcAddress(hdll,"InitRemoteCtrl");
    ReadRemoteData=(_ReadRemoteData)GetProcAddress(hdll,"ReadRemoteData");
    }

void __fastcall TForm1::FormDestroy(TObject *Sender)
    {
    FreeLibrary(hdll);
    }


使用第一个选项如果可以的话,因为它更安全。另外,不要忘记在 DLL 头文件中为 DLL 源导出/导入类,以及为 DLL 使用而导入/导出。

Use the first option if you can because it is safer. Also do not forget to export/import classes in the DLL header file export for DLL source and import for DLL usage.

class __declspec(dllimport/dllexport) myClass
    {
    ...
    };

PS

MSVC ++ Borland 之间构建 API 时,请注意导出/导入内容,避免使用未在Booth平台中使用的非标准数据类型(或类似 AnsiString ,...)的方式。

While constructing API between MSVC++ and Borland be careful what to export/import avoid the use of non standard data types not used in booth platforms (or in the same way like AnsiString,...).

这篇关于Builder C ++调用VC ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆