使用Delphi中的指针调用DLL [英] Calling DLLs with pointers in Delphi
问题描述
bool __stdcall MyFunction(char * name,int * index)
这个在C ++中调用此DLL函数的代码完美地工作:
typedef void(WINAPI * MyFunction_t)(char *,int *);
void main()
{
HMODULE mydll = LoadLibrary(LC:\\mydll.dll);
MyFunction_t MyFunction =(MyFunction_t)GetProcAddress(mydll,MyFunction);
int index = 0;
MyFunction(MyString,& index);
}
我需要在Delphi中执行相同操作。这是我的代码,它不工作(MyFunction被调用,但索引变量没有收到适当的值)。这是一个代码摘录,所以请忽略无序。任何输入将非常感谢!
type
TMyFunction = function(name:PChar; var index_ptr:Integer):布尔; STDCALL;
var
fMyFunction:TMyFunction;
i:整数;
h:THandle;
begin
结果:= 0;
h:= LoadLibrary('c:\\mydll.dll');
fMyFunction:= GetProcAddress(h,'MyFunction');
如果@fMyFunction<> nil then
begin
fMyFunction('MyString',i);
结果:= i;
结束
FreeLibrary(h);
结束
首先我假设你正在使用C与 externC
的连接,以便在C ++翻译单元中定义此函数。
如果您正在使用Delphi 2009或更高版本,您需要注意,PChar是一个指向空终止的宽字符串的指针。
与您的ANSI C函数需要使用:
键入
TMyFunction = function(name:PAnsiChar; var index:Integer) :布尔值STDCALL;
C bool
类型可能是最佳映射到 LongBool
,因为它与Delphi不一样布尔值
:
type
TMyFunction = function(name:PAnsiChar; var index:Integer):LongBool; STDCALL;
您不需要转义 \
在字符串中,所以你可以写:
h:= LoadLibrary('c:\mydll.dll');
您可能应该检查调用LoadLibrary的错误,从技术上来说, h
是一个 HMODULE
,而不是 THandle
,虽然这不会导致你任何问题。
惯用的Delphi将会写:
if分配(fMyFunction)然后
fMyFunction('MyString',Result);
基本上看起来很合理,但我对字符宽度最为怀疑。
希望有所帮助。
I am new to Delphi. I have a DLL with the following exported function in it:
bool __stdcall MyFunction(char * name, int * index)
This code which calls this DLL function in C++ works perfectly:
typedef void (WINAPI * MyFunction_t)(char *, int *);
void main()
{
HMODULE mydll = LoadLibrary(L"C:\\mydll.dll");
MyFunction_t MyFunction = (MyFunction_t)GetProcAddress(mydll, "MyFunction");
int index = 0;
MyFunction("MyString", &index);
}
I need to do the same in Delphi. Here is my code, which is not working (MyFunction gets called but the index variable doesn't receive the appropriate value). This is a code excerpt so please ignore disorder. Any input would be much appreciated!
type
TMyFunction= function(name: PChar; var index_ptr: Integer): Boolean; stdcall;
var
fMyFunction : TMyFunction;
i : Integer;
h: THandle;
begin
Result := 0;
h := LoadLibrary('c:\\mydll.dll');
fMyFunction := GetProcAddress(h, 'MyFunction');
if @fMyFunction <> nil then
begin
fMyFunction('MyString', i);
Result := i;
end;
FreeLibrary(h);
end;
First of all I am assuming that you are using C linkage with extern "C"
in case this function is defined in a C++ translation unit.
If you are using Delphi 2009 or later, you need to be aware that PChar is a pointer to a null-terminated wide character string.
To interop with your ANSI C function you need to use:
type
TMyFunction= function(name: PAnsiChar; var index: Integer): Boolean; stdcall;
The C bool
type is probably best mapped to LongBool
since it's not quite the same as a Delphi Boolean
:
type
TMyFunction= function(name: PAnsiChar; var index: Integer): LongBool; stdcall;
You don't need to escape \
in strings so you can write:
h := LoadLibrary('c:\mydll.dll');
You probably ought to check for errors on the call to LoadLibrary and, technically, h
is an HMODULE
rather than a THandle
, although that won't cause you any problems.
Idiomatic Delphi would be to write:
if Assigned(fMyFunction) then
fMyFunction('MyString', Result);
Basically it looks reasonable to me but I'm most suspicious of the character width.
Hope that helps.
这篇关于使用Delphi中的指针调用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!