VC创建Dll,BCB调用中出现的一个问题
1 VC中新建一个dll工程,名为dllabc,在dllabc.h中添加声明:extern "C" _declspec(dllexport) int sum(int a,int b);
在dllabc.cpp中添加代码:#include "windows.h"
#include "DLLABC.h"
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{return TRUE;}
extern "C" _declspec(dllexport)int sum(int a,int b)
{ return a+b; }
2 build这个VC工程,产生dllabc.dll ,用BCB的 implib.exe将dllabc转换成BCB可用的lib:implib dllabc.lib dllabc.dll
3 BCB中新建一个工程,窗体中拖一个BUTTON1和EDIT1,然后加载上面生成的dllabc.lib,在头文件中添加dllabc.h 。给button1
添加代码: void __fastcall TForm1::Button1Click(TObject *Sender)
{ int a;
a=sum(2,4) ;
Edit1->Text=a;}
最后运行,出现了一个错误:【Linker Error】Unresolved external '_sum' referenced from C:\PROGRAMFILES\BORLAND\CBUILDER6\PROJECTS\UNIT1.OBJ
请大大们看看哪一步错了?
[解决办法]
dllabc.h 中声明:
extern "C" _declspec(dllexport) int __stdcall sum(int a,int b);
dllabc.cpp 中定义:
int __stdcall sum(int a,int b)
{
return a + b;
}
编译生成DLL以后, 用C++Builder提供的implib生成.lib文件. 然后在C++Builder中新建一个工程, 将生成的.lib文件添加到工程中, 然后:
extern "C" _declspec(dllimport) int __stdcall sum(int a,int b);void __fastcall TForm1::Button1Click(TObject *Sender){ int a = sum(2, 4); Edit1->Text = a;}