delphi - 从 Delphi 代码调用 Visual C++ DLL 的函数

标签 delphi visual-c++ dll

在 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 );

德尔福有什么?

我改变了:

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 后的括号)

最佳答案

您需要像这样声明XInt:

XInt: function(DC: HDC; Text: PAnsiChar; SomeInt: Integer): Integer; stdcall;
  1. 您需要声明一个与 C++ 代码中的定义相匹配的参数列表。
  2. 我不知道参数的有意义的名称,但我确信您可以提供它们。
  3. C++ 代码指定与 Delphi 中的 stdcall 匹配的 WINAPI 调用约定。

然后你可以这样调用它:

retval := XInt(GetDC(hWnd), 'C:\Programs\ExtProg', 1);

关于delphi - 从 Delphi 代码调用 Visual C++ DLL 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10754282/

相关文章:

delphi - TDBEdit PopupMenu 默认行为

c++ - 如何在 C++ 中将 "new"用于多边形

c# - 如何将函数从 C++ .dll 导入到 C Sharp

c# - 在运行时不是特定于版本的 Visual Studio 项目引用

c# - native C++ dll 的 C++/CLI 包装器

database - 一个细节可以有两个主人吗?

delphi - 在Delphi 2005中出现错误“找不到属性”

delphi - 就 ADO 而言,nvarchar(max) 有多大?

C++ 删除运算符

c++ - 如果两个静态库使用相同的另一个静态库,如何避免 "LNK2005 Already Defined error"?