Solved

Problem with DLL parameters

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
Error E2158 compiling with Delphi XE10 Seattle 2 100
CheckListBox usage 3 59
Firemonkey DbLookupComboBox equivalent ? 2 48
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

810 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