Solved

Call a C++ DLL from Delphi

Posted on 1997-05-13
6
680 Views
Last Modified: 2010-04-04
Hi,
I'm working with Delphi(2.0) and right now I'm stuck in a very stupid problem. The problem deals with the loading of a DLL. This DLL is built with Microsoft C++ 1.52.

It is impossible to load this DLL and I tried it in two ways:

1) First, (Proof #1 below) is the implicit way and when I
   execute it from the Delphi environment the returned is:
   "Debugger Kernel Error. Error Code: 1."

2) Second, (Proof #2) is the explicit way. When I execute
   the routine "LoadLibrary(...)" it returns always zero.
   This error means:
   "System was out of memory, executable file was corrupt,
   or relocations were invalid". (?!?)

For your information, the DLL in question (TEKDCI.DLL) contains some functions to control an image acquisition board.

I will be very grateful if you could help me in this trouble. Thank you in advance and don't hesitate to contact me for further information or anything else.

Sincerely yours,

Jesus Galceran

------------------------------------------------------------
   Proof #1 (Implicit Load)
------------------------------------------------------------

   unit Init2;

   interface

   uses
     Windows, Messages, SysUtils, Classes, Graphics,
     Controls, Forms, Dialogs, StdCtrls, Buttons;

   type
     TForm1 = class(TForm)
       Button1: TButton;
       procedure Button1Click(Sender: TObject);
     private
       { Private declarations }
     public
       { Public declarations }
     end;

   var
     Form1: TForm1;
     BoardID : Integer;

   implementation

   {$R *.DFM}

   function TEK_Init: Integer; far pascal; external
   'TEKDCI.DLL';


   //
   //  Loading & Calling the DLL
   //
   procedure TForm1.Button1Click(Sender: TObject);
   begin
      BoardID:=TEK_Init;
      if BoardID > 0 then ShowMessage ('Board Initialized')
      else ShowMessage ('Initialization Failure');
   end;

   end.




------------------------------------------------------------
   Proof #2 (Explicit Load)
------------------------------------------------------------

   unit Init3;

   interface

   uses
     Windows, Messages, SysUtils, Classes, Graphics,
     Controls, Forms, Dialogs, StdCtrls, Buttons;


   type
     TInit = function: Integer;

     TForm1 = class(TForm)
       Button1: TButton;
       procedure Button1Click(Sender: TObject);
     private
       { Private declarations }
     public
       { Public declarations }
     end;

   var
     Form1  : TForm1;
     hTekDCI: THandle;
     Init   : TInit;
     FuncPtr: TFarProc;
     hDLL   : THandle;
     BoardID: Integer;

   implementation

   {$R *.DFM}

   //
   //  Loading & Calling the DLL
   //
   procedure TForm1.Button1Click(Sender: TObject);
   begin
      hTekDCI:=LoadLibrary('tekdci.DLL');
      if hTekDCI < HINSTANCE_ERROR then  
      begin
         ShowMessage ('      Error loading DLL      ');
         exit;
      end
      else begin
         FuncPtr:=GetProcAddress(hTekDCI, 'TEK_Init');
         @Init:=FuncPtr;
         if @Init <> nil then
         begin
            BoardID:=Init;
            if BoardID > 0 then ShowMessage ('Board Init')
            else ShowMessage ('Initialization Failure');
         end
         else ShowMessage ('Can not Load Routine');
      end;
      FuncPtr:=nil;
      FreeLibrary(hTekDCI);
   end;

   end.




------------------------------------------------------------
   Notes PERHAPS important:
------------------------------------------------------------

- In file DLL.H (included by the manufacturer) there is the   following definition:
     #define DLLCALL  __far __pascal __export

- Next, there is the TEK_Init() definition

    int DLLCALL TEK_Init()

    * Parameters:
      -------------
         None

    * Return Value:
      -------------
         The function returns <0 to indicate failure. If
         initialization is successful it returns the Board
         ID. The return ID can be 0, 1 or 2.

         Error Code Definition: 1: No hardware installed
                                2: Fail to initial hardware
                                3: INI file not found
                                4: DMA vxd not installed
                                5: No DCI Driver
                                6: Create DCI Primary Fail
                                7: Lock Memory Error

    * Description:
      -------------
         This function initializes the board using the               information specified in the INI file.

0
Comment
Question by:galceran
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:sperling
ID: 1336204
The problem is simply that the DLL is 16-bit...

You need to use thunking to make this work, or you need to write the app in Delphi 1.

If you need help with thunking, leave me a comment and I'll look into it.

Regards,

Erik.
0
 
LVL 3

Accepted Solution

by:
mheacock earned 25 total points
ID: 1336205
Exactly...32 bit to 16 bit not possible without a thunking
layer.  I've written some thunking.  I could forward you
a good article from the Delphi Magazine and some example code
of mine.

Just include your e-mail address.
0
 
LVL 3

Expert Comment

by:mheacock
ID: 1336206
The only problem is that this thunking makes use of an
undocumented function in Kernel32 call QT_Thunk.  Since it
is undocumented, there is no guarantee that Microsoft will
keep the function in future Windows versions.  It will also
not work in Windows NT.  As I understand it, no 32-bit app
can thunk to a 16-bit DLL in WinNT.

But QT_Thunk is really the only method to thunk in Delphi
down to 16 bits.  The other methods work mainly with C/C++
(the MS version)...to get those methods to work with Delphi
(from what I hear) is so much trouble as not to be worth it.

Anyhow, I've done it with QT_Thunk and it works pretty good.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Expert Comment

by:mheacock
ID: 1336207
Your other option would be to contact the makers of the DLL
and see if they have a 32 bit version...or if you have the
source code...it shouldn't be too difficult to recompile as
32 bit.
0
 

Author Comment

by:galceran
ID: 1336208
I'm interested in the mentioned Delphi Magazine article.
My e-mail address is galceran@esaii.upc.es

Thanks in advance
0
 

Author Comment

by:galceran
ID: 1336209
I'm interested in the mentioned Delphi Magazine article.
My e-mail address is galceran@esaii.upc.es

Thanks in advance
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 249
delphi exception 7 58
Delphi cmd execution 6 40
Delphi Dbf export problem to a Visual Foxpro application 6 122
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now