我是Delphi的新手。 我有一个DLL,其中包含以下导出函数:
bool __stdcall MyFunction(char * name, int * index)
这段调用DLL函数的C++代码可以正常运行:
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);
}
我需要在Delphi中完成同样的操作。以下是我的代码,但不起作用(MyFunction被调用了,但索引变量没有得到适当的值)。以下是代码片段,请忽略无序情况。如有任何建议,请提出!
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;