Solved

DLL and Strings parameters

Posted on 1998-12-22
6
259 Views
Last Modified: 2010-04-04
Hi all,

How can I send String parameters to DLL?
I'm observing to put ShareMem in USES clauses in both DLL and Caller and making Delphi3MM.dll available.
What should be wrong?
What about to include ShareMem in USES clause if the program I need to use the .dll(created by Delphi) isn't one Delphi program?

I hope to be clear.
Regards
Paulo
0
Comment
Question by:sergio_2001
6 Comments
 
LVL 3

Expert Comment

by:KE
ID: 1352614
How long are the strings you want to send, and would you need other datatypes to go along ?

0
 
LVL 3

Accepted Solution

by:
williams2 earned 50 total points
ID: 1352615
You actually don't need to use the ShareMem feature if you follow the rules of handling different memory managers.

This can be done by declaring Char pointers in a special way:

The rules is:
1) The client will have to allocate the needed space for the retrieved string. The library method may NOT touch this allocation, since the other memory manager ****s this up.

Example:

library Project1;

uses
  Unit1 in 'Unit1.pas';

  {$R ICON.res}

exports
  getInfo       index 1 name 'GetInfo',

begin
end.
----------------------------------------
unit Unit1;

interface

  Uses SysUtils;

  procedure getInfo(Name: pChar); stdcall; export;

implementation

  procedure getInfo(Name: pChar); stdcall;
  Begin
    //Do NOT use name:= 'Something'
    StrPCopy(Name,'This is dll');
  End;

end.

 At the project using the code, you should allocate as much memory to fit the largest string of them all, but it's fast and it works. The Windows libraries typically do the same.

Regards,
Williams
0
 
LVL 3

Expert Comment

by:williams2
ID: 1352616
..sorry, the {$R ICON.res} is just an example of how to include an Icon ressource to a dll, you should just remove it, or it wll fail to compile :-)

but to other guys:
I do not want to hear any objections on that the functions can be done more easy, because I do not share the opinion that every shared function should be located in the root source, though I have been corrected every time I come up with an example using this. :-)

Cheers and merry X-mas,
Williams


0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Expert Comment

by:philipleighs
ID: 1352617
Just to be on the safe side, you should getInfo should have another parameter which is the buffer size. This is what the WinAPI functions do. You could then prevent StrPCopy causing AVs by using StrPLCopy or whatever it is.

Now willy, before you go getting all defensive, know that I'm not trying to start another war. It's christmas man. I know you said something about the caller having the responsibility of ensuring that the buffer is long enough, so that's cool.

Oh and thanks for the xmas wishes, I'll drink to that and same to you. You know, by the time you have your first bevvy, I'll be (Edited by Computer101) in triplicate because we're 12 hours ahead!

Cheers!


0
 

Author Comment

by:sergio_2001
ID: 1352618
Hi Williams,

Thanks again for your answer.
Yet about my old question (mult-tiered application remember?) traking your tip to look for
Francois Piette I found anything very interesting. Look:
www.dbOvernet.com

Regards
Paulo
0
 
LVL 3

Expert Comment

by:williams2
ID: 1352619
Hi Paulo,

I think I have seen an add pointing to this URL. it sounds well, but I'm not sure I'll recommend anyone to a site whích is not freeware, but if the price is effortable (from 149$) I guess this must be the way to do it.

To Philipleighs:
hahahahaha.. You can (Edited by Computer101) in triplicates all that you want :-) ..I've was just having a little ironic-satiric moment, why prople ALWAYS post 5+ comments on how to export functions more easy than the other, sooo.. I guess your right about the buffer convertion.

I actually tried to allocate the needed memory inside the exported function, but I received a pointer error instead :-(

Cheers and more happy x-mas
Williams
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

20 Experts available now in Live!

Get 1:1 Help Now