开发者

Win VS2008 calling convention: Delphi dll from c

From a c app (VS2008, Win), I call a function in a dll written 开发者_高级运维in Delphi in Borland. The function works, but after each call I get this error: “The value of ESP was not properly saved across a function call[…]” which means my calling convention is wrong. I don't know Delphi and I don't have the full dll code, but I believe this is the Delphi function:

function translate(file1, file2: PChar):PChar; stdcall;
    ...
    Result:=PChar(c);
end;
exports
    translate; 

The relevant part in c:

typedef char*(__stdcall *translate)(char*, char*);
translate MyTranslate;
...
MyTranslate = (translate)GetProcAddress(dll, "translate");
char* result = (*MyTranslate)(file1, file2);

Instead of __stdcall in c above I've tried __cdecl and __fastcall, but I always get the ESP message. Also, in the Dephi function code the function seems to return char*, but the dll doc says it returns "true" or "false" (?). So in c instead of "typedef char*..." I've tried "typedef BOOL...": still, I get the ESP message. I know I can suppress the message under "Basic Runtime Checks" (see here), but I'd rather get the calling syntax right. The dll is compressed with UPX, but I'm not sure if it's relevant (like I said, the function itself works).


I believe the problem is that your Delphi function description isn't correct. All you could do is rightly mark the 'translate' function with __stdcall.

This kind of error occures when you try to invoke a COM object according to a one description and it actually has a different one. With COM objects it happends because there can be more that one version of the COM object on your machine and incorrect version is loaded. So, this is a dll-hell-like problem.

But in your case I believe that you know perfectly well where your Delphi DLL is loaded from. So I think it's just incorrect doc for that particular version of the DLL.


If the dll is using Borland fastcall(EAX, EDX, ECX) and the compiler is using Microsoft fastcall(EAX, EDX), that could cause the ESP register to lose sync quickly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜