Solved

Help with Unicode-ANSI Supported func

Posted on 2010-09-10
4
312 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:D
Thank you again!

Cheers!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

12 Experts available now in Live!

Get 1:1 Help Now