Solved

Problem with DLL parameters

Posted on 2004-04-25
4
255 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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