Solved

passing strings to dll

Posted on 2001-07-12
10
291 Views
Last Modified: 2010-04-06
I want to pass a pchar parameter to a dll function, but failed to do so. The parameter contains garbage in the dll. code is as follows ....


//********* DLL function ************
function dllTest(fn:pchar):Boolean;
begin
   if fn <> '' then
      // some code here .....
end;

//********* DLL function called from application *****
procedure test;
var
   str:string;
begin
   str:= 'this is a test';
   dllTest(pchar(str));
end;
0
Comment
Question by:m_adil
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 1

Expert Comment

by:Summa
ID: 6276560
Try using a char-array.

procedure test;
var Buffer: array[0..512] of Char;
begin
  StrPCopy(Buffer, 'A nice text for DLL');
  dllTest(Buffer);
end;
0
 
LVL 1

Expert Comment

by:mscatena
ID: 6276667
Are you following the warnings generated by the compiler when you create the DLL? Here it is:

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6276744
Summa's version should give the same result as your test.
mscatena's hint is of no relevance here since you do not hand over a string.
The application side of your test is correct.

I assume that
  if fn <> '' then
fails.
You are now handling C strings aka PChar.
Try the correct test for an empty string in C
  if fn[0] <> #0 then
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6278640
Could you show us a bit more code? Apart from the little thing Robert mentioned already your code looks okay. Show us the line where the garbage comes out. Also show us how you defined the dll function in the application.

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:alx512
ID: 6279840
Include sharemem in uses clause in bouth units.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:m_adil
ID: 6280377
ok Madshi here is a bit more code ...


//******** DLL SIDE *****************************

exports DisplayMAPI;

// in mapiForm.pas file .....

.........
{ Declare export function}
function DisplayMAPI(fn:pchar):Boolean;stdcall;


function DisplayMAPI(fn:pchar):Boolean;
var
    frmMail: TfrmMail;
    flag:boolean;
begin
flag:= True;
try
   try
      frmMail:= TfrmMail.Create(Application);
      MessageBox(0, fn, 'Make a Shortcut?',MB_OKCANCEL);
      if fn <> '' then
         frmMail.LoadAttachments(StrPas(fn));

      frmMail.ShowModal;
   except
       flag:= False;
   end;
finally
    frmMail.Free;
end;
Result:= flag;

end;


now right from the start i get garbage in the fn.
0
 
LVL 1

Author Comment

by:m_adil
ID: 6280480
and calling it like this ...

function DisplayMAPI(fn:pchar):boolean; external 'MAPI.dll';


procedure TForm1.Button2Click(Sender: TObject);
begin
DisplayMAPI(pchar('c:\temp\att.txt'));
end;
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 6280549
function DisplayMAPI(fn:pchar):boolean; external 'MAPI.dll';

You forgot stdcall here.

function DisplayMAPI(fn:pchar):boolean; stdcall;  external 'MAPI.dll';

Without stdcall your calling program assumes register calling convention and places the fn parameter value in a register.
The called function DisplayMAPI expects fn on the stack where it will only find the return address = garbage.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6280563
Exactly...
0
 
LVL 1

Author Comment

by:m_adil
ID: 6304328
ya it worked.
thanks ......
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now