Solved

Help with Unicode-ANSI Supported func

Posted on 2010-09-10
4
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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