Solved

passing strings to dll

Posted on 2001-07-12
10
294 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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
 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 64
control image tags in a string ? 12 133
Delphi...Split view - idea? 1 69
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 58
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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