I am getting "Catastrophic Failure" error in following scenario.

type
TGetDoubleValue = function (var ID: Integer): Double; safecall;
....
....
var
GetDoubleValue: TGetDoubleValue;
.....
.....
LibHandle := LoadLibrary('GetDoubleValue.dll');
@GetDoubleValue := GetProcAddress(LibHandle, 'getDoubleValue');
if not (@GetDoubleValue = nil) then
begin
  myDouble := GetDoubleValue(ID);
end
else
  RaiseLastOSError;

I am getting error on the "myDouble := GetDoubleValue(ID);" line while calling this function.

有帮助吗?

解决方案

Almost certainly this error is because of a calling convention or parameter list mis-match.

It is highly unlikely that the function you import from the DLL really is safecall. That calling convention is used with COM methods to perform HRESULT parameter list re-writing. Much more plausible is that the calling convention is stdcall, but you must check. You should also double-check that the parameters and return type are an exact match.

If you cannot work it out for yourself, then please just add to the question the function's declaration from the DLL source code (or documentation).

I am also not a fan of using @ with function pointers. It tends to lead to errors that could have been found by the compiler. I would write your code like this:

LibHandle := LoadLibrary('GetDoubleValue.dll');
Win32Check(LibHandle<>0);
GetDoubleValue := GetProcAddress(LibHandle, 'getDoubleValue');
Win32Check(Assigned(GetDoubleValue));
myDouble := GetDoubleValue(ID);

FWIW, I'm a huge fan of Win32Check because it removes branching from the high level code which makes it much easier to read.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top