用bcb5建立dll文件,内容如下:
//---------------------------------------------------------------------------#include "vcl.h"
#include <windows.h>#pragma argsused
extern "C" __declspec(dllexport) void test(int i);
extern "C" __declspec(dllexport) void test1(char *str);int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
void test(int i)
{
MessageBox(NULL,"test function in the dll begin!","",MB_OK);
};void test1(char *str)
{
MessageBox(NULL,str ,"",MB_OK);
}编译成test.dll,成功。在delphi6中调用其中的test1函数,采用静态调用的方式:
声明如下:
procedure _test(str:pchar);far; external 'test.dll';调用的语句如下:
_test('okok');
可是运行的结果,test1函数中的信息窗口显示的是随机的字符串!!并不是'okok' 采用动态调用的方式结果也一样。曾想用bcb中采用动态调用的方式调用test1函数,但不知道怎么声明,我测试调用一个不带参数的dll中的函数,成功了,但带参数带用怎么写呢?
以下是调用不带参数的dll中的函数。结果是可以的。
HANDLE hLibrary;
FARPROC lpFunc;
int i; hLibrary=LoadLibrary("test.dll"); //加载DLL
if((int)hLibrary>31) //加载成功
{
lpFunc=GetProcAddress(hLibrary,"_test"); //检取函数地址
if(lpFunc!=(FARPROC)NULL) //检取成功则调用
i=(*lpFunc)();
Label1->Caption =IntToStr(i);
FreeLibrary(hLibrary); //释放占用的内存
}
//---------------------------------------------------------------------------#include "vcl.h"
#include <windows.h>#pragma argsused
extern "C" __declspec(dllexport) void test(int i);
extern "C" __declspec(dllexport) void test1(char *str);int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
void test(int i)
{
MessageBox(NULL,"test function in the dll begin!","",MB_OK);
};void test1(char *str)
{
MessageBox(NULL,str ,"",MB_OK);
}编译成test.dll,成功。在delphi6中调用其中的test1函数,采用静态调用的方式:
声明如下:
procedure _test(str:pchar);far; external 'test.dll';调用的语句如下:
_test('okok');
可是运行的结果,test1函数中的信息窗口显示的是随机的字符串!!并不是'okok' 采用动态调用的方式结果也一样。曾想用bcb中采用动态调用的方式调用test1函数,但不知道怎么声明,我测试调用一个不带参数的dll中的函数,成功了,但带参数带用怎么写呢?
以下是调用不带参数的dll中的函数。结果是可以的。
HANDLE hLibrary;
FARPROC lpFunc;
int i; hLibrary=LoadLibrary("test.dll"); //加载DLL
if((int)hLibrary>31) //加载成功
{
lpFunc=GetProcAddress(hLibrary,"_test"); //检取函数地址
if(lpFunc!=(FARPROC)NULL) //检取成功则调用
i=(*lpFunc)();
Label1->Caption =IntToStr(i);
FreeLibrary(hLibrary); //释放占用的内存
}
extern "C" void __declspec(dllexport) __stdcall test(int i);
extern "C" void __declspec(dllexport) __stdcall test1(char *str);
在delphi中也用stdcall方式;
然后test(pchar('okok'));
肯定可以的。
你这样不行,是因为声明的方式不同造成的。
如果不改动态库,你调用时用cdecl可以试一下。
哎!这些天忙的头都晕了,这么基础的知识都不敢确定了。
extern "C" void __declspec(dllexport) __stdcall test(int i);
extern "C" void __declspec(dllexport) __stdcall test1(char *str);