?
Solved

passing strings to dll

Posted on 2001-07-12
10
Medium Priority
?
300 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

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

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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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