[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

passing strings to dll

Posted on 2001-07-12
10
Medium Priority
?
305 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
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!

 
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

Technology Partners: 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

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

872 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