Solved

C DLL in Delphi 2

Posted on 1998-11-14
4
293 Views
Last Modified: 2010-04-04
I wrote a DLL in MS Visual C++, and I want to call it from a Delphi 2 app. How do I do this? Here is my declaration of the function in C++:

BOOL UnpackFile(const char* Resource, const char* InFile)
0
Comment
Question by:mmarvi
  • 2
4 Comments
 

Accepted Solution

by:
kashif063098 earned 50 total points
ID: 1346804
first you need to declare this function in delphi in following way
function UnpackFile(Resource,InFile:PChar):boolean;cdecl;
external 'C:\YourCdll.dll' name 'UnpackFile';

then simply call UnpackFile() with appropriate parameters.

Any question please ask!

regards

0
 

Author Comment

by:mmarvi
ID: 1346805
Dear kashif:

I put your code into a test program, and at build time, I encountered an error "field defintion not allowed after methods or procedures." I included the unit's code below, please find my error and correct it.

Thanx!

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure OnExtract(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function UnpackFile(Resource,InFile:PChar):boolean; cdecl; external 'WPLANE.DLL';
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.OnExtract(Sender: TObject);
var
     h: boolean;
begin
     h := UnpackFile('WINGS.WAD', 'JEX.EXE');
     if h = true then ShowMessage('Success!');
     else ShowMessage('Fail!');
end;

end.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1346806
Don't place UnpackFile in the form class.  Just place it by itself in the module.
0
 

Expert Comment

by:kashif063098
ID: 1346807
scrapdog is right put the following declaration after the uses clause
>>uses
>>  Windows, Messages, SysUtils, Classes, Graphics, Controls, >>  Forms, Dialogs,StdCtrls;

function UnpackFile(Resource,InFile:PChar):boolean;cdecl;
external 'C:\YourCdll.dll' name 'UnpackFile';
After this change your exmaple should look like following

unit Unit1;

interface

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

/*Remember to include the full path to your dll*/
function UnpackFile(Resource,InFile:PChar):boolean; cdecl; external 'WPLANE.DLL' name 'UnpackFile';


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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.OnExtract(Sender: TObject);
var
     h: boolean;
begin
     h := UnpackFile('WINGS.WAD', 'JEX.EXE');
     if h = true then ShowMessage('Success!');
     else ShowMessage('Fail!');
end;

end.


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show Listview image from database (String field) 5 143
How to load 2 images in same column in Delphi 2 67
MS Access from Delphi 31 77
migrate this code to work on android 1 27
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

680 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