delphi调用vc写的动态链接库问题(30分)

A

Adnil

Unregistered / Unconfirmed
GUEST, unregistred user!
用VC建立一个动态链接库,只有一个导出函数
头文件如下:
#ifdef CHARSETEXP_EXPORTS
#define CHARSETEXP_API __declspec(dllexport)
#else
#define CHARSETEXP_API __declspec(dllimport)
#endif
// This class is exported from the CharsetExp.dll
/*class CHARSETEXP_API CCharsetExp {
public:
CCharsetExp(void);
// TODO: add your methods here.
};
extern CHARSETEXP_API int nCharsetExp;
*/
CHARSETEXP_API int fnCharsetExp(int a);

cpp如下:
// CharsetExp.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "CharsetExp.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
//CHARSETEXP_API int nCharsetExp=0;
// This is an example of an exported function.
CHARSETEXP_API int fnCharsetExp(int a)
{
return ++a;
}

delphi调用如下:
type
Tc = function(a: integer): integer;
stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
h: thandle;
a: Tc;
begin
h := loadlibrary('C:/Temp/CharsetExp/Release/CharsetExp.dll');
a := Tc(GetProcAddress(h, 'fnCharsetExp'));
showmessage(inttostr(a(100)));
FreeLibrary(h);
end;

发现无法获取fnCharsetExp函数地址,请教大虾如何解决。
 
我也不清楚,你看一下《Delphi中如何调用VC++创建的动态链接库》这篇文章吧,或许有用
http://www.ccw.com.cn/htm/app/aprog/01_10_29_2.asp
 
部分问题已经解决
加上extern "C",防止函数名按照C++方式链接。
但现在在显示"101"之后会发生非法访问错误。
 
试试修改VC工程目录下的*.def文件:
....
EXPORTS
fnCharsetExp @1;
Explicit exports can go here
 
改成:
delphi调用如下:
type
Tc = function(a: integer): integer;
cdecl;
 
接受答案了。
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
553
import
I
I
回复
0
查看
598
import
I
顶部