2

In a Delphi program I'm using a DLL to control an external executable. The DLL is written in Visual C++. In C++ the code to use DLL is:

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

I have an equivalent code in 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;

It seems to work correctly, but I'm not able to find the Delphi code for the last instruction, to launch external program...

pfStart -> ( GetDC ( hWnd ), “C:\\Programs\\ExtProg”, 1 );

what in Delphi ?

I have changed:

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;

but now I have on last instruction : retval := XInt... the following error: DCC Error : '(' expected but ')' found (parenthesis after hWnd)

1
  • It looks like you've retyped some of this rather than using copy/paste. pfStart -> ( GetDC ( hWnd ), “C:\\Programs\\ExtProg”, 1 ) is a syntax error I think and some of your quotes are messed up. I think I know what you mean, but using the clipboard helps us be sure we can see the exact same thing that you can see. Commented May 25, 2012 at 12:27

1 Answer 1

3

You need to declare XInt like this:

XInt: function(DC: HDC; Text: PAnsiChar; SomeInt: Integer): Integer; stdcall;
  1. You need to declare a parameter list that matches the definition in the C++ code.
  2. I don't know meaningful names for the parameters but I'm sure you can supply them.
  3. The C++ code specifies the WINAPI calling convention which matches stdcall in Delphi.

And then you can call it like this:

retval := XInt(GetDC(hWnd), 'C:\Programs\ExtProg', 1);
Sign up to request clarification or add additional context in comments.

3 Comments

I have changed as You suggested. Delphi gives an error on instruction retval := XInt... GetDC(hWnd) isn't recognized ...
You didn't show all the code. So I can't see what you are looking at. What is not recognized?
You need to get a window handle, hwnd, say. I can't work out where you'll get that from. I think I answered the question you asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.