Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with Unicode-ANSI Supported func

Posted on 2010-09-10
4
Medium Priority
?
330 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 2000 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 2000 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

730 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