0

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 how to fix this?

mghie
  • 32,028
  • 6
  • 87
  • 129
Crudler
  • 2,194
  • 3
  • 30
  • 57

2 Answers2

1

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?).

Rafael Colucci
  • 6,018
  • 4
  • 52
  • 121
  • You can't use ShareMem to solve problems between D2006 and D2010 related to Unicode/Ansi. They use totally different forms of the memory manager. – Ken White Mar 17 '11 at 02:53
  • @Ken White you are right, my mistake. I forgot that embarcadero changed the memory manager also (now it uses fastmm). – Rafael Colucci Mar 17 '11 at 03:00
0

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.

TridenT
  • 4,879
  • 1
  • 32
  • 56