Solved

Problem with DLL parameters

Posted on 2004-04-25
4
251 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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…

744 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

14 Experts available now in Live!

Get 1:1 Help Now