Question : How to re-write code using strings to Delphi 2010

Please, I have this code:

Var
 cDestino: PAnsiChar;
 cOrigem: PAnsiChar;
 iProgBar: Integer;
 sFile :Ansistring;
 sPath:Ansistring;
 sDiaSemana:string[20];
and;

  sFile := 'PedItem.db';

  cOrigem:=strtopchar(sPath+'\'+sFile);
   --> Error E2010 Incompatible types: 'Char' and 'AnsiChar'  

  cDestino:= strtopchar( 'C:\Back-up_AcxSir\'+sDiaSemana+sFile);
  --> Error: E2010 Incompatible types: 'Char'  and 'AnsiChar' , AND,  Warning: W1057 Implicit string cast from 'AnsiString' to 'string'  

These two lines cause these errors and warnings when compiling with Delphi 2010.
What changes do I have to do?
Thanks




Answer : How to re-write code using strings to Delphi 2010

I'm guessing you are calling Windows API to copy files or similar, so you need only PChar and string types, and Delphi compiler will decide, according to the version, the correct kind and correct API version it will use.

With Delphi <2009 :
String = AnsiString
(P)Char = (P)AnsiChar
and all windows API called are the ANSI version , ex :
CopyFile = CopyFileA

With Delphi >=2009
String = WideString
Char = WideChar
and all windows API called are the Wide version


so, your code should be made to compile with all versions, without playing too much with other types except when you don't have choice
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Var
 cDestino, cOrigem : PChar;
 sFile, sPath, sDiaSemana :String;
 iProgBar: Integer;
and;

  sFile := 'PedItem.db';
  cOrigem := strtopchar ( sPath+'\'+sFile );
  cDestino:= strtopchar ( 'C:\Back-up_AcxSir\'+sDiaSemana+sFile ); 
  CopyFile( cOrigem , cDestino , False ); 


Note that you can convert String to PChar with just a cast in the way aflarin shown for AnsiString & PAnsiChar. 
So you can even call your function in one line

  CopyFile( PChar( sPath+'\'+sFile ), PChar( 'C:\Back-up_AcxSir\'+sDiaSemana+sFile ) , False ); 
Random Solutions  
 
programming4us programming4us