Solved

Help with Unicode-ANSI Supported func

Posted on 2010-09-10
4
321 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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