Solved

passing strings to dll

Posted on 2001-07-12
10
292 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to add Windows metadata to a text file programmatically with Delphi? 3 169
select query - oracle 16 92
Create a path if not exists 7 69
control image tags in a string ? 12 110
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…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

21 Experts available now in Live!

Get 1:1 Help Now