在Delphi程序中,我使用DLL来控制外部可执行文件。 DLL是用Visual C ++编写的。 在C ++中,使用DLL的代码是:
// defining pointer's type to a function compatible with 'launcher' function:
typedef int (WINAPI* PFI_HD_ST_N)(HDC, LPCSTR, int);
…
…
HINSTANCE m_hiDLLCTRL; // handle to DLL instance
PFI_HD_ST_N pfStart; // pointer to function that starts external program
// initialization
m_hiDLLCTRL = NULL;
pfStart = NULL;
// links DLL dynamically
m_hiDLLCTRL = LoadLibrary(“ExtProgCTRL.dll”);
// if DLL has been loaded
If (m_hiDLLCTRL)
// I get the address to funcion that launches ExtProg
pfStart = ( PFI_HD_ST_N) GetProcAddress( m_hiDLLCTRL, “ExtProgStart” );
// launches external program
If ( pfStart )
// hWnd is handle to window where I want do draw
pfStart -> ( GetDC ( hWnd ), “C:\\Programs\\ExtProg”, 1 );
我在Delphi中有一个等效的代码:
private
DLLHandle: THandle;
XInt: function: Integer: cdecl;
...
implementation
...
...
DLLHandle := LoadLibrary('ExtProgCTRL.dll');
if DLLHandle <> 0 then
begin
@XInt := GetProcAddress(DLLHandle, ''ExtProgStart);
if @XInt <> nil then
begin
MessageDlg('Function ExtProgStart loaded !', mtError, mbOKCancel, 0);
...
...
end;
end;
它似乎工作正常,但我无法找到最后一条指令的Delphi代码,启动外部程序......
pfStart -> ( GetDC ( hWnd ), “C:\\Programs\\ExtProg”, 1 );
Delphi中的内容?
我改变了:
private
DLLHandle: THandle;
XInt: function(DC: HDC; Text: PAnsiChar; SomeInt: Integer); Integer: cdecl;
...
implementation
...
retval: Integer;
DLLHandle := LoadLibrary('ExtProgCTRL.dll');
if DLLHandle <> 0 then
begin
@XInt := GetProcAddress(DLLHandle, ''ExtProgStart);
if @XInt <> nil then
begin
MessageDlg('Function ExtProgStart loaded !', mtError, mbOKCancel, 0);
retval := XInt(GetDC(hWnd), 'C:\Programs\ExtProg', 1);
end;
end;
但现在我有最后一条指令:retval:= XInt ......出现以下错误: DCC错误:'('期望但')'找到(hWnd后的括号)
答案 0 :(得分:3)
你需要像这样声明XInt
:
XInt: function(DC: HDC; Text: PAnsiChar; SomeInt: Integer): Integer; stdcall;
WINAPI
匹配的stdcall
调用约定。然后你可以这样称呼它:
retval := XInt(GetDC(hWnd), 'C:\Programs\ExtProg', 1);