Solved

Problem with DLL parameters

Posted on 2004-04-25
4
254 Views
Last Modified: 2012-05-04
Hello experts,

I am having some difficulty making a call to a dll function that I wrote.  I am passing a PChar parameter from my application to the DLL function (to avoid passing strings), but the value is corrupted when it is received by the dll.  This is a snapshot of my code:

**********************
Application code....

...

  try
    Handle := LoadLibrary(test.dll');
    @Func1:= GetProcAddress(Handle, Func1);
    if @Func1= nil then
      exit;
    Func1('Parameter');
  except
  end

...

**********************
DLL code....

...

function Func1(lpszCmdLine :PChar) : Integer; stdcall;
begin
  ShowMessage(Parameter: ' + lpszCmdLine);
end;

...


Am I doing something wrong?
Thanks,

dannyguindi
0
Comment
Question by:dannyguindi
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
ID: 10915569
Hi,

1. What's your function declaration in the app? It should be something like:

type
   TFunc1 = function(lpszCmdLine :PChar) : Integer; stdcall;

var
  Func1: TFunc1;

or:
var
  Func1: function(lpszCmdLine :PChar) : Integer; stdcall;

2. Do not forget to free the library

 try
    Handle := LoadLibrary('test.dll'); // // there should be one more ' character
    if Handle <> 0 then begin
      @Func1:= GetProcAddress(Handle, 'Func1'); // two more ' chars
      if @Func1 <> nil then begin
        Func1('Parameter');
      end;
      FreeLibrary(Handle);
    end;
  except
  end
...

3. In the DLL:

function Func1(lpszCmdLine :PChar) : Integer; stdcall;
begin
  result := 0;
  ShowMessage('Parameter: ' + lpszCmdLine); // there should be one ' before Parameter
end;

Regards, Geo
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10915715
Sometimes, I dont even understand why, Pchar sucks... I always prefer PAnsiChar in this type of parameter  values..

0
 

Author Comment

by:dannyguindi
ID: 10918196
The missing ' were just typos in my snapshot.   What was causing the problem was the missing 'stdcall' in my function declaration.  It works now!

Thanks geobul.
0
 
LVL 17

Expert Comment

by:geobul
ID: 10918411
You are welcome :-)
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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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