开发者

Delphi 2010 variant to unicode problem

I am working on a DLL in Delphi 2010. It exports a procedure that receives an array of variants. I want to be able to take one of these variants, and convert it into a string, but I keep getting ?????

I cannot change the input variable - it HAS to be an array of variants. The host app that calls the DLL cannot be changed. It is written in Delphi 2006.

Sample DLL code:

Procedure TestArr(ArrUID : array of variant); stdcall;  
var 
  i: integer;  
  s: string;
begin  
  s:= string(String(Arruid[0]));  
  showmessage(s);  
end;  

Using D2006 my DLL works fine. I have tried using VartoStr - no luck. When I check the VarType I am getting a varString. Any suggestions h开发者_Go百科ow to fix this?


You host application is sending an AnsiString and you dll is expecting a UnicodeString.
Unicode strings was introduced in Delphi 2009, it does not exist in Delphi 2006. How to fix it? Try [untested]:

Procedure TestArr(ArrUID : array of variant); stdcall;  
var 
  i: integer;  
  s: AnsiString;
begin  
  s:= Ansistring(VarToStr(Arruid[0]));  
  showmessage(s);  
end;  

or maybe [also untested]:

Procedure TestArr(ArrUID : array of variant); stdcall;  
var 
  i: integer;  
  s: AnsiString;
begin  
  s:= Ansistring(AnsiString(Arruid[0]));  
  showmessage(s);  
end;  

You can also check if theres is a function Like VarToStr that accepts AnsiStrings (maybe in the AnsiStrings unit?).


1/ How have you call the VarToStr() function ? VarToString(Arruid[0]) ?

2/ Does your Delphi2006 Application send AnsiString or WideString to the DLL ? If so, and if (1) is not working, try to cast to AnsiString instead of string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜