[D2010] Passing a unicode string to a DLL

Hi to everybody, I'm writing a custom dll in which I need to write a 
function that receives a string as a parameter. Till now, in this 
scenario I always used PChars to be sure to don't get problems with 
memory managers (I mean don't want to use and ditribute BORLNDMM.DLL).

Now, with D2010, I'm using fastmm as memomory manager; may I have 
problems with memory if I pass directly the parameter as a string type 
(unicode)? Exe and dll are always compiled togheter and both with the 
same fastmm parameters.

Thanks

Davide
0
Davide
7/15/2010 7:58:58 AM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

1 Replies
1298 Views

Similar Articles

[PageSpeed] 14

> Hi to everybody, I'm writing a custom dll in which I need to write a 
> function that receives a string as a parameter. Till now, in this 
> scenario I always used PChars to be sure to don't get problems with 
> memory managers (I mean don't want to use and ditribute BORLNDMM.DLL).

PChar used to be PAnsiChar and is now PWideChar, so you can use it like before, 
except that it points to unicode string data now. However for DLLs I'd 
explicitly use PAnsiChar or PWideChar, just because it's more obvious, 
especially if the DLL is used by other languages. You can also export two 
different functions, just like Windows often does with the A and W version of 
the API calls (like MessageBoxA and MessageBoxW).

If your DLL function looks like this...

{code}
procedure MyProc(P: PChar);
{code}

....you can use it like this on the caller's side, *if* both the DLL and the 
application were compiled with a Delphi version using the same string type:

{code}
// all Delphi versions
var
   S: String;
begin
   S := 'test';
   MyProc(@S[1]);
{code}

If your DLL functions (compiled with Delphi 2009+) look like this...

{code}
procedure MyProcA(P: PAnsiChar);
procedure MyProcW(P: PWideChar);
{code}

....you can use it like this on the caller's side using variuos Delphi versions:

{code}
// D2009+ only
var
   S: UnicodeString; // or just String
begin
   S := 'test';
   MyProcW(@S[1]);
{code}

{code}
// all Delphi versions
var
   S: AnsiString;
begin
   S := 'test';
   MyProcA(@S[1]);
{code}

{code}
// all Delphi versions
var
   S: String;
begin
   S := 'test';
   MyProc(@S[1]);
{code}

The latter only works if MyProc equals MyProcW in Delphi 2009+ and MyProcA 
otherwise.

If you don't want to support ansi strings it gets a lot simpler, but you cannot 
use your DLL with older Delphi versions of course (well, you can, using WideString).

-- 
Jens Gruschel
http://www.pegtop.net
0
Jens
7/15/2010 5:26:47 PM
Reply:

Similar Artilces:

D2010 Passing user defined records as parameters to Delphi DLL and returning records
Hi Friends: I have a simple question... I have a *Huge* account system, made in Delphi 2010. It works ok. I have external MODULES made in Delphi 2010. They are DLL. So, i need to share information between them, using records like those (example) TAnotherRecord = record field1, field3 : widestring; ComplexField: TRecordComplex; end; TPlusRecord = record field1, fieldA, fieldB: widestring; end; TMyRecord = record someField: TAnotherRecord; otherField: TPlusRecord; end; All these type are shared in definitions in main EXE and DLLs FIRST PROBLEM...

Passing unicode strings to prepare method and other unicode questions
Hi, Increasingly I am getting asked unicode questions and being presented with unicode issues that currently don't work in DBD::ODBC. Currenty DBD::ODBC supports the binding of unicode parameters and the returning of unicode result-set data. I would like to change DBD::ODBC to support: a) unicode column names (from NAME attribute, column_info etc) b) unicode connection strings c) unicode SQL d) unicode table names (table_info etc) Although I don't specifically need unicode connection strings I at least need to turn connection strings usually passed to SQLDriverCo...

Passing string array to dll
The PB manuals describe how to declare an external reference with regards to passing an array of strings -> ref string item[]. I have tried numerous variations (LPSTR *x, LPVOID x, LPSTR x[] ...) in the dll to receive the array, but cannot seem to get one to work. I am constantly getting an error code 15 - Error accessing external function. The dll contains numerous other functions that receive simple string items, and they all work successfully. The new function has been placed in the .def file as an export item. Has anyone _ever_ successfully passed a string array to a dll...

Delphi 2009 Unicode String
I have some old (from 2003) units for a freeware component that don't compile in Delphi 2009 for unicode translation error. Can I disable with a directive the unicode and leave the old ansichar? Thanks. Roberto Colpani wrote: > I have some old (from 2003) units for a freeware component that don't > compile in Delphi 2009 for unicode translation error. Can I disable > with a directive the unicode and leave the old ansichar? Thanks. No. Good reasons why this is have been given and discussed already. Can't you simply recompile the code and adjust it thus that...

problems passing string to DLL
Hi there, I've written a DLL function: char* getString(char* in) This works fine when run from a console but from powerbuilder, the DLL only receives the first char. what am I doing wrong? Any help, would be great, I've been trying different things for about 2 hours now! mat mat wrote: > I've written a DLL function: > > char* getString(char* in) > > This works fine when run from a console but from powerbuilder, the DLL > only receives the first char. > > what am I doing wrong? > > Any help, would be great, I'...

Passing strings from a dll to a PB app
Hi all, My PB app is using a dll I wrote in C++ in order to connect to a certain server. Some of the dll functions are used for getting strings, which can vary from few characters to several thousands. I'm looking for an elegant way to pass those strings from the dll to the PB app: Going by the rule of "whoever allocates memory is responsible for freeing it", I should create a long string (i.e. large array of chars, a buffer) and pass it to the dll, which will copy the string to the given buffer. This will work, but creating a huge string of spaces and taking the risk ...

Passing Arrays as Args to Delphi I DLL
I have a PowerBuilder 5.0.3 16 bit app that needs to call a Delphi I DLL procedure that expects a character array as arg 1, an integer as arg 2 and a string as arg 3. Arg 1 & 2 are to be modified by the DLL proc. The following is the PB5 external declaration for the DLL proc and the statement that calls it. Following that is the actual Delphi I DLL proc being called. When I execute the call from the PB app, it bombs every time with an error in PBstb050. If I remove the character array arg from the DLL proc and PB routine that calls it, everything works just fine. Can ...

Delphi 2010 DLL with string parameters
Hi all, I'm new to DLL development, and want to build a DLL with dynamic loading to do some calculations in Delphi 2010. I have read several examples on this subject, but can't get it to work. Using Delphi2010 with FastMM, I would like to call a function in a DLL with a String parameter.In FastMM, I've set the ShareMM (App / DLL) and AttemptToUseShareMM (DLL) parameters to true so I should be able to use dynamic arrays. But I'm getting rubbish from the parameter. Application: TDLLFunction = function DLLFunction(AParam: String); Stdcall; procedure CallDLLFunction; ...

Passing Unicode string from C++ to Javascript
Hi! I'm a newbie XPCOM developer and I've come across several difficulties regarding passing strings back from C++ to Javascript. The task is to populate XUL <listbox/> with values from string array I get from my C+ + component. Right now I'm trying to get it working using JS-array like this: var contacts = contactsInterface.getContacts({}); for (var i = 0; i < contacts.length; i++) { contactList.appendItem(contacts[i]); } And that's C++ code: NS_IMETHODIMP Contacts::GetContacts(PRUint32 *aCount, PRUnichar*** contacts) { PRUint32 itemCount = 1;...

Passing strings ByRef to DLL [Edit]
Hi - Can anyone out there please clarify the correct method to enable passing strings from VBA to a delphi DLL so they can be modified ? I have googled this subject and (depending on the version of Delphi) found several snippets of code - not of which seem to work. Here is the situation. I have a routing in Access (VBA) that needs to call a DLL (written in DELPHI RAD STUdio XE2) and pass some string variables that need to be modified in the DLL so the result can be seen in the called VBA application. Here is the code I am trying at the moment : In Access ==========================...

Delphi COM and passing string in OPC
Hi! I have a COM job, programming OPC. First I need to add my group. This was working good: g := Obj.OPCGroups.Add('MY') // g.Name == MY ! But this is don't: s := 'MY' g := Obj.OPCGroups.Add(s) // g.Name == _Group* ! I tried with every mode and style. I converted to WideString, variant, oleString, etc. Only this working a little: s := 'MY' g := Obj.OPCGroups.Add(PChar(s)^) // g.Name == M , only the first char :-( So the question is: How Delphi can compile OLE call with string constant to it is successful, and how I can do same thing?...

passing a string to unmaned dll
Do I have to use <MarshalAs(UnmanagedType.LPStr)> in my function declaration for a string? Declare Auto Function pk_Extract Lib "CLLib.Dll" Alias "lib_GetAppRegistryValue" _ (<MarshalAs(UnmanagedType.LPStr)> ByVal ProdKey As String, ByRef SystemID As Integer, ByRef CardID As Byte, _ ByRef Products As Integer, ByRef Modules As Long, ByRef Hardware As Byte) As Long ...

Help!
Hi, The typecasting used with non unicode strings 'PChar(s)' doesn't work more correct with unicode strings in D2010. How can I do this? Thanks, John > The typecasting used with non unicode strings 'PChar(s)' doesn't work > more correct with unicode strings in D2010. How can I do this? If your question is 'how do I cast from a string to a PChar', then the answer is 'in exactly the same way as you did in D2-D2007'. Alternatively, if your question is 'how do I cast from an AnsiString to a PChar', then it depends on your unde...

Passing array from delphi app to C++ dll
Hi I need to develop a C++ DLL to use with my Delphi application (The DLL must be in C++ because we are going to use a lot of objects from OpenCV library). In my DLL I need to create a routine that will receive an array of the following delphi record: TMyRecord = record Origin: HRGN; Destination: HRGN; end; I have an array of this record type: var MyArray: array of TMyRecord; begin SetLength(MyArray, 4); // Fill the array .. .. // Pass the data to my dll function MyCPPDll(MyArray, 4); end; This is how it should work, I need to pass My...

Web resources about - [D2010] Passing a unicode string to a DLL - embarcadero.delphi.general

Pascal (programming language) - Wikipedia, the free encyclopedia
Prior to his work on Pascal, Wirth had developed Euler and ALGOL W and later went on to develop the Pascal-like languages Modula-2 and Oberon ...

Quality of Service (Disputes/Issues) (8660): 2010
Part VII Applications - Quality of Service (Disputes/Issues) for 2010

BJP MP's brother on run after crushing school director over land dispute
Jashpur police have lodged FIR on charges against attempt to murder against Vikramaditya.

Commonwealth Games Federation - Commonwealth Games - Introduction
The Commonwealth Games Federation - Uniting the Commonwealth through Sport

Arbitrators Decide 'Pornosquatting' Operator Must Give Up Site
Calling it a case of pornosquatting, arbitrators for the World Intellectual Property Organization have ruled that the operator of CafeInternet.com ...

衛信科技 : SDN Expert
衛信(Xinguard)提供專業的網路技術應用及產品,協助企業面對瞬息萬變的網路需求。衛信服務及產品包含OpenFlow switch、Big data應用及Netflow analysis。

uvc(4)
UVC(4) FreeBSD Kernel Interfaces Manual UVC(4) NAME uvc webcamd driver for UVC compliant USB devices SYNOPSIS uvc DESCRIPTION The uvc driver ...

Index of WIPO UDRP Panel Decisions
World Intellectual Property Organization … Menu IP Services IP Services All IP Services Protecting IP PCT - The International Patent System Madrid ...

Biographies - S
The names listed below are those for whom there is abiography. Many hundreds (thousands?) of other names, with dates of birth,etc, are recorded ...

Pascal (programming language) - Wikipedia, the free encyclopedia
Pascal, named in honor of the French mathematician and philosopher Blaise Pascal , was developed by Niklaus Wirth . Before his work on Pascal, ...

Resources last updated: 12/7/2015 8:54:54 PM