This site is moving to The Zen Source Library
Please Update your bookmarks


Unicode Parameters and Wrapper Functions


Delphi 3

Delphi 3 makes UniCode <--> Ansi string conversions trivial.
Thanks to the WideString variable there is no need for any API conversion routines.

Ansi --> UNICODE


   var
      s : string;
      ws : WideString;
   begin
      s := 'abc';
      ws := s;
   end;

When you use the OLE functions you normaly need a PWideChar. Just cast a WideSting to a PWideChar to achieve this.


   var
      ws : WideString;
   begin
      ws := 'abc';
      SomeOLEFunction(  PWideChar(ws)  );
   end;


UNICODE --> Ansi


   var
      s : string;
      ws : WideString;
   begin
      ws := 'abc';
      s := ws;
   end;
Easy!


Delphi 2

All the structured storage routines that use strings use unicode strings, actually all 32bit OLE string are UNICODE. So every time you want to use one of these routines you need to convert from unicode-->ANSI or ANSI-->unicode. As you can imagine coding this gets extremely messy, not to mention a tad boring.

For this reason its a good idea to use wrapper functions. These wrapper functions can do all the ANSI<-->unicode conversion. Additionally they can do error checking, to make your app more robust.


ANSI --> Unicode Conversion

In Delphi a PWideChar is used as a pointer to a unicode string, in the same way as a PChar is used as a pointer to a ANSI string. Converting a string from ANSI to Unicode is done with the MultiByteToWide API call. Before you can use this function you'll need to allocate memory for the new unicode string.

The amount of memory a unicode string needs is exactly double that used by the ANSI string. The reason for this is that in UNICODE a character is 16 bytes and in ANSI a character is 8 bytes.

Here is a utility function that does the ANSI --> Unicode conversion


   function StringToPWide(  sStr: string;  var iNewSize: integer  ): PWideChar;
   var
      pw : PWideChar;
      iSize : integer;
   begin
         {String to PWideChar, NB memory allocated must be freed by caller}
      iSize := Length(  sStr  ) + 1;
      iNewSize := iSize * 2;
      
         {Allocate memory}
      pw := AllocMem(  iNewSize  );

         {Do conversion}
      MultiByteToWideChar(  CP_ACP,  0,  PChar(sStr),  iSize,  pw,  iNewSize  );

      Result := pw;
   end;

The caller must remember to free the memory allocated by this function after you are finished using the Unicode string. FreeMem is used to accomplish this.

Here is a short example showing how this function would be used


   procedure TestConversion;
   var
      pw : PWideChar;
      iSize : integer;
      sANSIString : string;
   begin
         {The ANSI string}
      sANSIString := 'Testing...';

         {Get a unicode string from the ANSI string}
      pw := StringToPWide(  sANSIString,  iSize  );
  

      (* Do somthing with the unicode string *)


         {Free memory used by the unicode string}
      FreeMem(  pw  );
   end;


Unicode --> ANSI

Converting a unicode string to a ANSI string is considerably easier, than ANSI to unicode.

Here is the function


   function PWideToString(  pw : PWideChar  ) : string;
   var
      p : PChar;
      iLen : integer;
   begin
         {Get memory for the string}
      iLen := lstrlenw(  pw  ) + 1;
      GetMem(  p,  iLen  );

         {Convert a unicode (PWideChar) to a string}
      WideCharToMultiByte(  CP_ACP,  0,  pw,  iLen,  p,  iLen * 2,  nil,  nil  );

      Result := p;
      FreeMem(  p,  iLen  );
   end;

As you can see there is no memory to free afterwards either.






All information on these www pages is copyright (©) 1997 Andre .v.d. Merwe And may not be copied or mirrored without my permission.