Solved

Help with Unicode-ANSI Supported func

Posted on 2010-09-10
4
315 Views
Last Modified: 2012-05-10
Hey there!

Here is me again =)

In my API, i'm keeping support for Unicode/ANSI like Windows does, casting a "W' and "A" version for respectives versions (W = Wide, A = ANSI) of course hehe

Ok, example

function SomethingW(lpBuffer: PWideChar): BOOL; StdCall;
begin
  //do something with buffer
  Result := True;
end;

function SomethingA(lpBuffer: PAnsiChar): BOOL; StdCall;
begin
  Result := SomethingW(PWideChar(UnicodeString(lpBuffer)));
end;

Perfect!

but, when using structs/record? i need to make a copy of  the function instead of casting the W version like above?

example

TSomethingW = record
  chFoo: WideChar;
end;
PSomethingW = ^TSomethingW;

TSomethingA = record
  chFoo: AnsiChar;
end;
PSomethingA = ^TSomethingA;

function SomethingW(lpBuffer: PWideChar; lpStruct: PSomethingW): BOOL; StdCall;
begin
  //do something here with params
  Result := True;
end;

function SomethingA(lpBuffer: PAnsiChar; lpStruct: PSomethingA): BOOL; StdCall;
begin
  //here is the problem
  Result := SomethingW(PWideChar(UnicodeString(lpBuffer)), OPS..AND ABOUT THE STRUCT?!?! hehe);
end;

Some idea?

Regards,
Carlos
0
Comment
Question by:cebasso
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33645408
You have to convert your structure and string fields
function SomethingA(lpBuffer: PAnsiChar; lpStruct: PSomethingA): BOOL; StdCall;

Var

 StructW:SomethingW;

begin

// if lpStruct is an IN parameter : convert before 

  StructW.chFoo:=lpStruct^.chFoo;

  Result := SomethingW(PWideChar(UnicodeString(lpBuffer)), @StructW );

// if lpStruct is an OUT parameter : convert after

// if IN/OUT : convert both

   lpStruct^.chFoo:=StructW.chFoo; 

end;

Open in new window

0
 
LVL 4

Author Comment

by:cebasso
ID: 33648936
Hey epasquier thanks! :D
But, if the Struct has a array example
type
  TMyArrayW = record
    chArray: array of WideChar;
  end;

  TMyArrayA = record
    chArray: array of AnsiChar;
  end;
TSomethingW = record
  chFoo: WideChar;
  MyArray: TMyArrayW;
end;
PSomethingW = ^TSomethingW;

TSomethingA = record
  chFoo: AnsiChar;
  MyArray: TMyArrayA;
end;
PSomethingA = ^TSomethingA;
Then, i need to walk for every field in the array, copying to the W struct? example

function SomethingA(lpBuffer: PAnsiChar; lpStruct: PSomethingA): BOOL; StdCall;
var
  StructW: TSomethingW;
  i: Integer;
begin
  FillChar(StructW, SizeOf(TSomethingW), 0);
  StructW.chFoo  := PWideChar(UnicodeString(lpStruct^.chFoo));
  for i := Low(lpStruct^.MyArray) to High(lpStruct^.MyArray) do
  StructW.MyArray[i] := PWideChar(UnicodeString(lpStruct^.MyArray[i]));
  Result := SomethingW(PWideChar(UnicodeString(lpBuffer)), @StructW);
end;
Something like this?

Thanks!
0
 
LVL 25

Assisted Solution

by:epasquier
epasquier earned 500 total points
ID: 33650582
yes, something like this, you just forgot to set the array size

SetLength(StructW.MyArray,Length(lpStruct^.MyArray));
for i:=....
0
 
LVL 4

Author Closing Comment

by:cebasso
ID: 33657256
:D
Thank you again!

Cheers!
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 convert wav to mp3 in delphi 9 174
When I am typing into a TEdit last word repeats 7 119
Correct Component for Shopping Cart. 2 96
Print Graphic and Text to Epson TM-T88v 12 188
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

23 Experts available now in Live!

Get 1:1 Help Now