Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with DLL parameters

Posted on 2004-04-25
4
Medium Priority
?
259 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 200 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: 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.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

715 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