Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to use DLL built by Delphi in VB?

Posted on 1999-01-26
6
196 Views
Last Modified: 2010-04-06
I need to know how I can implement a DLL that has been compiled by Delphi 3.0 in a 32 bit VB application.

Can anyone tell me what datatypes are compatible (Delphi versus VB) or how I can modify a parameter's contents when passing it to a DLL function?

For example, when passing a string to a Delphi DLL function from VB: how do I do that?
Does Delphi support the Variant datatype or an equivalent, like VB does?
0
Comment
Question by:kulikuli
  • 3
  • 2
6 Comments
 

Expert Comment

by:lorenkoss
ID: 1363401
I have done these many times.  Basically, I only pass PChars, integers, and pointers.  Be sure also to specify either CDECL or STDCALL in your delphi app so that it puts your variables in the right order.  In your VB app, you will prototype the function with the appropriate CDECL or STDCALL so that the variable go in the right order (pascal type is right to left (i think) and cdecl is left to right) based on the call stack.

I wouldn't play with the Variant type.  I never properly got it work.  As long as you know what type to expect, use pointers to objects.
0
 
LVL 5

Author Comment

by:kulikuli
ID: 1363402
lorenkoss, I have created a function in Delphi that is compiled in a DLL, which expects a PChar like you said.I have specified STDCALL in the DLL-file.
Unfotunately, it keeps crashing......

The dll's source code in Delphi:
----------------------------
library docver;

uses
  SysUtils,
  Classes;

function valid(fname : pchar) : integer; stdcall;
var fs : TFilestream;
    l  : longint;
begin
  fs := TFileStream.create(fname,fmOpenRead);
  fs.Seek(512,soFromBeginning);
  fs.Read(l,3);
  if l = 12690924 then
    valid := 1
  else
    valid := 0;
  fs.destroy;
end;

exports
 valid index 1;

begin
end.
-------------


My module in VB:
--------------Declarations section-----------------
Private Declare Function valid2 Lib "docver.dll" (ByVal strName as String) as Integer
-------------------------------------------------------------
Function Test()

Dim intReturn as Integer
intReturn = Valid2( "Hello" & vbNull ) 'call dll-function with null-terminated string

End Function

0
 
LVL 5

Author Comment

by:kulikuli
ID: 1363403
the VB declaration of course shouldn't be Valid2, but Valid. Sorry.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Author Comment

by:kulikuli
ID: 1363404
The question is reopened, because I need a solution fast!
0
 

Expert Comment

by:lorenkoss
ID: 1363405
Make sure the valid function is being called by running the DLL from delphi and setting the run parameters in Delphi to point to the "host application" (the VB app).  I have a feeling the error may be happening in your filestream implementation.  Put a break point on the create line and step through..  It should get at least to that line.
0
 
LVL 15

Accepted Solution

by:
simonet earned 30 total points
ID: 1363406
In Delphi 3, use the ShortString type rather than pChar.

You should only use pchar if you're going to change it's value. Otherwise, if it's going to behave as a "const", make it a ShortString.

That said, your Delphi declaration should look like:

function valid(fname : ShortString) : integer; stdcall;
var
  fs : TFilestream;
  l  : longint;
begin
  fs := TFileStream.create(AnsiString(fname), fmOpenRead);
  ...
  ...
  ...

end;

See if that works. If not, drop me a line.

yours,

Alex

   



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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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