Browse Source

* merged my own Windows specific fixes, and reworked the sysutils part a bit.

git-svn-id: branches/unicode@24741 -
marco 12 years ago
parent
commit
15d86a5489

+ 2 - 2
compiler/globals.pas

@@ -874,7 +874,7 @@ implementation
       {$endif}
       {$ifdef mswindows}
         GetEnvPchar:=nil;
-        p:=GetEnvironmentStrings;
+        p:=GetEnvironmentStringsA;
         hp:=p;
         while hp^<>#0 do
          begin
@@ -891,7 +891,7 @@ implementation
            { next string entry}
            hp:=hp+len+1;
          end;
-        FreeEnvironmentStrings(p);
+        FreeEnvironmentStringsA(p);
         {$define GETENVOK}
       {$endif}
       {$ifdef os2}

+ 6 - 6
rtl/inc/exeinfo.pp

@@ -71,11 +71,11 @@ uses
 
   var
     Tmm: TMemoryBasicInformation;
-{$ifdef wince}
+{$ifdef FPC_OS_UNICODE}
     TST: array[0..Max_Path] of WideChar;
-{$else wince}
+{$else}
     TST: array[0..Max_Path] of Char;
-{$endif wince}
+{$endif FPC_OS_UNICODE}
   procedure GetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: string);
     begin
       baseaddr:=nil;
@@ -86,11 +86,11 @@ uses
           baseaddr:=Tmm.AllocationBase;
           TST[0]:= #0;
           GetModuleFileName(THandle(Tmm.AllocationBase), TST, Length(TST));
-{$ifdef wince}
+{$ifdef FPC_OS_UNICODE}
           filename:= String(PWideChar(@TST));
-{$else wince}
+{$else}
           filename:= String(PChar(@TST));
-{$endif wince}
+{$endif FPC_OS_UNICODE}
         end;
     end;
 

+ 2 - 6
rtl/inc/systemh.inc

@@ -1161,7 +1161,7 @@ Procedure mkdir(const s:rawbytestring); overload;
 Procedure rmdir(const s:rawbytestring); overload;
 
 Procedure getdir(drivenr:byte;var dir: unicodestring);
-Procedure getdir(drivenr:byte;var dir: rawbytestring);rtlproc; // defaultrtlfilesystemcodepage is used here
+Procedure getdir(drivenr:byte;var dir: rawbytestring);{$ifndef VER2_6}rtlproc; {$endif}// defaultrtlfilesystemcodepage is used here
 {$ENDIF}
 {$endif FPC_HAS_FEATURE_FILEIO}
 
@@ -1266,11 +1266,7 @@ Procedure Error(RunTimeError : TRunTimeError);
 {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
 Function  Paramcount:Longint;
 
-{$IFDEF FPC_UNICODE_RTL}
-Function  ParamStr(l:Longint):unicodestring;
-{$ELSE}
-Function  ParamStr(l:Longint):string;
-{$ENDIF}
+Function  ParamStr(l:Longint):RawByteString;
 {$endif FPC_HAS_FEATURE_COMMANDARGS}
 
 Procedure Dump_Stack(var f : text;bp:pointer;addr : pointer = nil);

+ 2 - 5
rtl/linux/system.pp

@@ -121,11 +121,8 @@ end;}
 var
  execpathstr : shortstring;
 
-{$IFDEF FPC_UNICODE_RTL}
-function paramstr(l: longint) : unicodestring;
-{$ELSE}
-function paramstr(l: longint) : string;
-{$ENDIF}
+function paramstr(l: longint) : RawbyteString;
+
  begin
    { stricly conforming POSIX applications  }
    { have the executing filename as argv[0] }

+ 4 - 4
rtl/objpas/objpas.pp

@@ -96,9 +96,9 @@ Var
      {$endif}
 {$endif FPC_HAS_FEATURE_FILEIO}
 
-{$if defined(FPC_HAS_FEATURE_COMMANDARGS) and not defined(FPC_UNICODE_RTL)}
+{$if defined(FPC_HAS_FEATURE_COMMANDARGS) }
      { ParamStr should return also an ansistring }
-     Function ParamStr(Param : Integer) : Ansistring;
+     Function ParamStr(Param : Integer) : UnicodeString;
 {$endif FPC_HAS_FEATURE_COMMANDARGS}
 
 {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS) and not defined(FPC_UNICODE_RTL)}
@@ -262,8 +262,8 @@ end;
 {$endif}
 {$endif FPC_HAS_FEATURE_FILEIO}
 
-{$if defined(FPC_HAS_FEATURE_COMMANDARGS) and not defined(FPC_UNICODE_RTL)}
-Function ParamStr(Param : Integer) : Ansistring;
+{$if defined(FPC_HAS_FEATURE_COMMANDARGS) }
+Function ParamStr(Param : Integer) : unicodestring;
 
 Var Len : longint;
 

+ 85 - 103
rtl/objpas/sysutils/filutil.inc

@@ -79,17 +79,21 @@ begin
                      ToSingleByteFileSystemEncodedFileName(NewName));
 end;
 
+{$ifdef OS_FILEISREADONLY}
 Function FileIsReadOnly(const FileName: UnicodeString): Boolean;
 
 begin
   Result:=FileIsReadOnly(ToSingleByteFileSystemEncodedFileName(FileName));
 end;
+{$endif}
 
+{$ifdef OS_FILESETDATEBYNAME}
 Function FileSetDate (Const FileName : UnicodeString;Age : Longint) : Longint;
 
 begin
   Result:=FileSetDate(ToSingleByteFileSystemEncodedFileName(FileName),Age);
 end;
+{$endif}
 
 function FileAge(const FileName: RawByteString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
    
@@ -139,9 +143,76 @@ begin
 end;
 
 
+
+Function FileSearch (Const Name, DirList : RawByteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
+Var
+  I : longint;
+  Temp : RawByteString;
+
+begin
+  Result:=Name;
+  temp:=SetDirSeparators(DirList);
+  // Start with checking the file in the current directory
+  If (sfoImplicitCurrentDir in Options) and (Result <> '') and FileExists(Result) Then
+    exit;
+  while True do begin
+    If Temp = '' then
+      Break; // No more directories to search - fail
+    I:=pos(PathSeparator,Temp);
+    If I<>0 then
+      begin
+        Result:=Copy (Temp,1,i-1);
+        system.Delete(Temp,1,I);
+      end
+    else
+      begin
+        Result:=Temp;
+        Temp:='';
+      end;
+    If Result<>'' then
+      begin
+      If (sfoStripQuotes in Options) and (Result[1]='"') and (Result[Length(Result)]='"') then
+        Result:=Copy(Result,2,Length(Result)-2);
+      if (Result<>'') then
+        Result:=IncludeTrailingPathDelimiter(Result)+name;
+      end;
+    If (Result <> '') and FileExists(Result) Then
+      exit;
+  end;
+  result:='';
+end;
+
+Function FileSearch (Const Name, DirList : RawByteString; ImplicitCurrentDir : Boolean) : RawByteString;
+begin
+  if ImplicitCurrentDir then
+    Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
+  else
+    Result:=FileSearch(Name,DirList,[]);
+end;
+
+
+Function ExeSearch (Const Name : RawByteString; Const DirList : RawByteString ='' ) : RawByteString;
+
+Var
+  D : RawByteString;
+  O : TFileSearchOptions;
+begin
+  D:=DirList;
+  if (D='') then
+    D:=GetEnvironmentVariable('PATH');
+{$ifdef unix}
+  O:=[];
+{$else unix}
+  O:=[sfoImplicitCurrentDir,sfoStripQuotes];
+{$endif unix}
+  Result := FileSearch(Name, D, O);
+end;
+
+
 {$endif}
 
 {$ifndef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
 begin
   Result:=FileOpen(UnicodeString(FileName),Mode);
@@ -181,12 +252,6 @@ begin
   Result:=DirectoryExists(UnicodeString(Directory));
 end;
 
-Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TSearchRec) : Longint;
-
-begin
-  Result:=FindFirst(unicodestring(path),attr,rslt);
-end;
-
 Function FileGetAttr (Const FileName : RawByteString) : Longint;
 begin
   Result:=FileGetAttr(unicodestring(FileName));  
@@ -208,17 +273,21 @@ begin
   Result:=RenameFile(UnicodeString(OldName),UnicodeString(NewName));
 end;
 
+{$ifdef OS_FILEISREADONLY}
 Function FileIsReadOnly(const FileName: RawByteString): Boolean;
 
 begin
   Result:=FileIsReadOnly(UnicodeString(FileName));
 end;
+{$endif}
 
+{$ifdef OS_FILESETDATEBYNAME}
 Function FileSetDate (Const FileName : RawByteString;Age : Longint) : Longint;
 
 begin
   Result:=FileSetDate(UnicodeString(FileName),Age);
 end;
+{$endif}
 
 function FileAge(const FileName: UnicodeString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
    
@@ -245,12 +314,6 @@ begin
 end;
 
 
-Function FileSearch (Const Name, DirList : RawByteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
-
-begin
-  Result:=ToSingleByteFileSystemEncodedFileName(FileSearch(UnicodeString(Name),UnicodeString(Dirlist),Options));
-end;
-
 Function FileSearch (Const Name, DirList : UnicodeString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : UnicodeString;
 Var
   I : longint;
@@ -289,26 +352,23 @@ begin
   result:='';
 end;
 
-Function FileSearch (Const Name, DirList : RawByteString; ImplicitCurrentDir : Boolean) : RawByteString;
+Function FileSearch (Const Name, DirList : RawbyteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
+begin
+   result:=ToSingleByteFileSystemEncodedFileName(FileSearch(unicodestring(name),unicodestring(dirlist),options));
+end;
 
+Function FileSearch (Const Name, DirList : RawbyteString; ImplicitCurrentDir : Boolean) : RawByteString;
 begin
-  Result:=ToSingleByteFileSystemEncodedFileName(FileSearch(UnicodeString(Name),UnicodeString(DirList),ImplicitCurrentDir));
+     result:=ToSingleByteFileSystemEncodedFileName(FileSearch(unicodestring(name),unicodestring(dirlist),ImplicitCurrentDir));
 end;
 
 Function FileSearch (Const Name, DirList : UnicodeString; ImplicitCurrentDir : Boolean) : UnicodeString;
-
 begin
   if ImplicitCurrentDir then
     Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
-  else  
+  else
     Result:=FileSearch(Name,DirList,[]);
 end;
-    
-Function ExeSearch (Const Name : RawByteString; Const DirList : RawByteString ='' ) : RawByteString;
-
-begin
-  Result:=ToSingleByteFileSystemEncodedFileName(ExeSearch(UnicodeString(Name),UnicodeString(Dirlist)));
-end;
 
 Function ExeSearch (Const Name : UnicodeString; Const DirList : UnicodeString ='' ) : UnicodeString;
 
@@ -327,87 +387,9 @@ begin
   Result := FileSearch(Name, D, O);
 end;
 
-{$endif}
-
-Function FileSearch (Const Name, DirList : RawByteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
-Var
-  I : longint;
-  Temp : RawByteString;
-
+Function ExeSearch (Const Name : RawbyteString; Const DirList : RawbyteString ='' ) : RawByteString;
 begin
-  Result:=Name;
-  temp:=SetDirSeparators(DirList);
-  // Start with checking the file in the current directory
-  If (sfoImplicitCurrentDir in Options) and (Result <> '') and FileExists(Result) Then
-    exit;
-  while True do begin
-    If Temp = '' then
-      Break; // No more directories to search - fail
-    I:=pos(PathSeparator,Temp);
-    If I<>0 then
-      begin
-        Result:=Copy (Temp,1,i-1);
-        system.Delete(Temp,1,I);
-      end
-    else
-      begin
-        Result:=Temp;
-        Temp:='';
-      end;
-    If Result<>'' then
-      begin
-      If (sfoStripQuotes in Options) and (Result[1]='"') and (Result[Length(Result)]='"') then
-        Result:=Copy(Result,2,Length(Result)-2);
-      if (Result<>'') then
-        Result:=IncludeTrailingPathDelimiter(Result)+name;
-      end;
-    If (Result <> '') and FileExists(Result) Then
-      exit;
-  end;
-  result:='';
+  result:=ToSingleByteFileSystemEncodedFileName(ExeSearch(unicodestring(name),unicodestring(dirlist)));
 end;
-Function FileSearch (Const Name, DirList : RawByteString; ImplicitCurrentDir : Boolean) : RawByteString;
-
-begin
-  if ImplicitCurrentDir then
-    Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
-  else  
-    Result:=FileSearch(Name,DirList,[]);
-end;
-Function ExeSearch (Const Name : RawByteString; Const DirList : RawByteString ='' ) : RawByteString;
-
-Var
-  D : RawByteString;
-  O : TFileSearchOptions;
-begin
-  D:=DirList;
-  if (D='') then
-    D:=GetEnvironmentVariable('PATH');
-{$ifdef unix}
-  O:=[];
-{$else unix}
-  O:=[sfoImplicitCurrentDir,sfoStripQuotes];
-{$endif unix}
-  Result := FileSearch(Name, D, O);
-end;
-
-{$IFNDEF FPC_UNICODE_RTL}
-function FileAge(const FileName: RawByteString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
+{$endif}
 
-Var
-  Info : TSearchRec;
-  A : Integer;
-   
-begin
-  for A:=1 to Length(FileName) do
-    If (FileName[A] in ['?','*']) then
-      Exit(False);
-  A:=0;
-  if Not FollowLink then
-    A:=A or faSymLink;
-  Result:=FindFirst(FileName,A,Info)=0;
-  If Result then 
-    FileDateTime:=FileDatetoDateTime (Info.Time);
-  FindClose(Info);
-end;
-{$ENDIF}

+ 0 - 9
rtl/objpas/sysutils/filutilh.inc

@@ -15,7 +15,6 @@
 
 Type
 
-{$IFDEF FPC_UNICODE_RTL}
 
   // Some operating systems need extra find data.
 {$if defined(Win32) or defined(WinCE) or defined(Win64)}
@@ -34,9 +33,6 @@ Type
     TUnicodeSearchFindData = TNativeNTFindData;
 {$endif}
 
-
-{$ENDIF FPC_UNICODE_RTL}
-
 // USEFINDDATA is only defined here.
 
 {$if defined(Win32) or defined(Win64)}
@@ -64,7 +60,6 @@ Type
     {$DEFINE USEFINDDATA}
 {$endif}
 
-{$IFDEF FPC_UNICODE_RTL}
   // The actual unicode search record
   TUnicodeSearchRec = Record
     Time : Longint;
@@ -82,7 +77,6 @@ Type
     FindData : TUnicodeSearchFindData;
 {$ENDIF}    
   end;
-{$ENDIF}
 
   TRawbyteSearchRec = Record
     Time : Longint;
@@ -141,8 +135,6 @@ Type
   TFileSearchOption = (sfoImplicitCurrentDir,sfoStripQuotes);
   TFileSearchOptions = set of TFileSearchOption;
 
-{$IFDEF FPC_UNICODE_RTL}
-
 Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
 Function FileCreate (Const FileName : UnicodeString) : THandle;
 Function FileCreate (Const FileName : UnicodeString; Rights : Integer) : THandle;
@@ -165,7 +157,6 @@ Function FileSearch (Const Name, DirList : UnicodeString; ImplicitCurrentDir : B
 Function ExeSearch  (Const Name : UnicodeString; Const DirList : UnicodeString = '') : UnicodeString;
 Function FileIsReadOnly(const FileName : UnicodeString): Boolean;
 function FileAge(const FileName: UnicodeString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
-{$ENDIF UNICODERTL}
 
 Function FileOpen (Const FileName : RawByteString; Mode : Integer) : THandle;
 Function FileCreate (Const FileName : RawByteString) : THandle;

+ 1 - 1
rtl/objpas/sysutils/fina.inc

@@ -143,7 +143,7 @@ end;
 {$I fexpand.inc}
 
 {$ENDIF}
-{$UNDEFINE PathStr}
+{$UNDEF PathStr}
 
 
 function ExpandFileName (Const FileName : string): String;

+ 0 - 12
rtl/objpas/sysutils/sysutils.inc

@@ -23,30 +23,20 @@
 
 
   {$ifndef OS_FILEISREADONLY}
-{$ifdef FPC_UNICODE_RTL}
   Function FileIsReadOnly(const FileName: RawByteString): Boolean;
-{$else}
-  Function FileIsReadOnly(const FileName: String): Boolean;
-{$endif}
   begin
     Result := (FileGetAttr(FileName) and faReadOnly) <> 0;
   end;
-  {$ifdef  FPC_UNICODE_RTL}
   Function FileIsReadOnly(const FileName: UnicodeString): Boolean;
 
   begin
     Result := (FileGetAttr(FileName) and faReadOnly) <> 0;
   end;
-  {$endif}
   {$endif OS_FILEISREADONLY}
 
 
   {$ifndef OS_FILESETDATEBYNAME}
-  {$ifdef FPC_UNICODE_RTL}
   Function FileSetDate (Const FileName : RawByteString;Age : Longint) : Longint;
-  {$else}
-  Function FileSetDate (Const FileName : String;Age : Longint) : Longint;
-  {$endif}
   Var
     fd : THandle;
   begin
@@ -65,7 +55,6 @@
       Result:=-1;
   {$endif}
   end;
-  {$ifdef FPC_UNICODE_RTL}
   Function FileSetDate (Const FileName : UnicodeString;Age : Longint) : Longint;
   Var
     fd : THandle;
@@ -86,7 +75,6 @@
   {$endif}
   end;
   {$endif}
-  {$endif}
 
   { Read String Handling functions implementation }
   {$i sysstr.inc}

+ 1 - 14
rtl/unix/sysutils.pp

@@ -1,4 +1,4 @@
-{
+{
     This file is part of the Free Pascal run time library.
     Copyright (c) 1999-2000 by Florian Klaempfl
     member of the Free Pascal development team
@@ -267,10 +267,6 @@ procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
 
 {$DEFINE SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
 
-{$IFNDEF FPC_UNICODE_RTL}
-{$DEFINE SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
-{$ENDIF}
-
 { Include platform independent implementation part }
 {$i sysutils.inc}
 
@@ -703,13 +699,11 @@ Begin
   f.FindHandle:=nil;
 End;
 
-{$IFDEF FPC_UNICODE_RTL}
 Procedure FindClose(Var f: TUnicodeSearchRec);
 Begin
   Do_findClose(PUnixFindData(f.FindHandle));
   f.FindHandle:=nil;
 End;
-{$ENDIF}
 
 Function Do_FindGetFileInfo(const s:RawByteString; D:PUnixFindData; 
                             out st : baseunix.stat; out WinAttr : longint):boolean;
@@ -747,7 +741,6 @@ begin
     End;
 end;
 
-{$IFDEF FPC_UNICODE_RTL}
 
 Type
   PUnicodeSearchRec = ^TUnicodeSearchRec;
@@ -771,7 +764,6 @@ begin
     f^.Time:=st.st_mtime;
     End;
 end;
-{$ENDIF}
 
 // Returns the FOUND filename. Empty if no result is found.
 // Uses CB to return file info
@@ -836,13 +828,11 @@ begin
   FindNext:=Do_findNext(PUnixFindData(Rslt.FindHandle),@FindGetFileInfoR,@Rslt);
 end;
 
-{$IFDEF FPC_UNICODE_RTL}
 Function FindNext (Var Rslt : TUnicodeSearchRec) : Longint;
 
 begin
   FindNext:=Do_findNext(PUnixFindData(Rslt.FindHandle),@FindGetFileInfoU,@Rslt);
 end;
-{$ENDIF}
 
 Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TRawByteSearchRec) : Longint;
 
@@ -881,7 +871,6 @@ Begin
     FindClose(Rslt); 
 End;
 
-{$IFDEF FPC_UNICODE_RTL}
 Function FindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TUnicodeSearchRec) : Longint;
 
 {
@@ -921,8 +910,6 @@ Begin
   If (Result<>0) then
     FindClose(Rslt); 
 End;
-{$ENDIF}
-
 
 Function FileGetDate (Handle : Longint) : Longint;
 

+ 11 - 5
rtl/win/dos.pp

@@ -612,9 +612,11 @@ end;
 procedure getfattr(var f;var attr : word);
 var
    l : longint;
+   s : ansistring;
 begin
   doserror:=0;
-  l:=GetFileAttributes(filerec(f).name);
+  s:=filerec(f).name;
+  l:=GetFileAttributes(pchar(s));
   if l=longint($ffffffff) then
    begin
      doserror:=getlasterror;
@@ -626,15 +628,19 @@ end;
 
 
 procedure setfattr(var f;attr : word);
+var s : ansistring;
 begin
   { Fail for setting VolumeId }
   if (attr and VolumeID)<>0 then
     doserror:=5
   else
-   if SetFileAttributes(filerec(f).name,attr) then
-    doserror:=0
-  else
-    doserror:=getlasterror;
+     begin
+       s:=filerec(f).name;
+       if SetFileAttributes(pchar(s),attr) then
+        doserror:=0
+      else
+        doserror:=getlasterror;
+     end;
 end;
 
 { change to short filename if successful win32 call PM }

+ 1 - 1
rtl/win/dynlibs.inc

@@ -39,7 +39,7 @@ Uses windows;
 Function LoadLibrary(const Name : AnsiString) : TlibHandle;
 
 begin
-  Result:=Windows.LoadLibrary(PChar(Name));
+  Result:=Windows.LoadLibraryA(PChar(Name));
 end;
 
 Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;

+ 121 - 3
rtl/win/sysdir.inc

@@ -20,6 +20,123 @@
 type
  TDirFnType=function(name:pointer):longbool;stdcall;
 
+{$ifdef FPC_UNICODE_RTL}
+procedure dirfn(afunc : TDirFnType;s:unicodestring);
+begin
+  DoDirSeparators(s);
+  if not aFunc(punicodechar(s)) then
+    begin
+      errno:=GetLastError;
+      Errno2InoutRes;
+    end;
+end;
+
+function CreateDirectoryTrunc(name:pointer):longbool;stdcall;
+begin
+  CreateDirectoryTrunc:=CreateDirectory(name,nil);
+end;
+
+Procedure do_MkDir(const s: UnicodeString);[IOCheck];
+begin
+  If (length(s)=0) or (InOutRes <> 0) then
+   exit;
+  dirfn(TDirFnType(@CreateDirectoryTrunc),s);
+end;
+
+Procedure do_MkDir(const s: RawByteString);[IOCheck];
+begin
+  do_mkdir(UnicodeString(s));
+end;
+
+Procedure do_RmDir(const s: UnicodeString);[IOCheck];
+begin
+  if (s ='.') then
+    InOutRes := 16;
+  {$ifdef WINCE}
+  if (s='..') then
+    InOutRes := 5;
+  {$endif WINCE}
+  If (s='') or (InOutRes <> 0) then
+   exit;
+  dirfn(TDirFnType(@RemoveDirectory),s);
+{$ifdef WINCE}
+  if (Inoutres=3) and (Pos(DirectorySeparator, s)<2) then
+    Inoutres:=2;
+{$endif WINCE}
+end;
+
+Procedure do_RmDir(const s: RawByteString);[IOCheck];
+begin
+  do_RmDir(UnicodeString(s));
+end;
+
+Procedure do_ChDir(const s: UnicodeString);[IOCheck];
+begin
+{$ifndef WINCE}
+  If (s='') or (InOutRes <> 0) then
+   exit;
+  dirfn(TDirFnType(@SetCurrentDirectory),s);
+  if Inoutres=2 then
+   Inoutres:=3;
+{$else WINCE}
+  InOutRes:=3;
+{$endif WINCE}
+end;
+
+Procedure do_ChDir(const s: RawByteString);[IOCheck];
+begin
+  do_ChDir(UnicodeString(s));
+end;
+
+procedure do_GetDir (DriveNr: byte; var Dir: Unicodestring);
+{$ifndef WINCE}
+var
+  Drive:array[0..3]of char;
+  defaultdrive:boolean;
+  DirBuf,SaveBuf:array[0..259] of WideChar;
+  len : integer;
+{$endif WINCE}
+begin
+{$ifndef WINCE}
+  defaultdrive:=drivenr=0;
+  if not defaultdrive then
+   begin
+    Drive[0]:=widechar(Drivenr+64);
+    Drive[1]:=':';
+    Drive[2]:=#0;
+    Drive[3]:=#0;
+    GetCurrentDirectory(high(SaveBuf)+1,SaveBuf); // in TChar
+    if not SetCurrentDirectory(@Drive) then
+     begin
+      errno := word (GetLastError);
+      Errno2InoutRes;
+      Dir := widechar (DriveNr + 64) + ':\';
+      SetCurrentDirectory(@SaveBuf);
+      Exit;
+     end;
+   end;
+
+  len:=GetCurrentDirectory(0,nil);
+  setlength(dir,len-1); // -1 because len is #0 inclusive
+  GetCurrentDirectory(len,punicodechar(dir));
+  if not defaultdrive then
+   SetCurrentDirectory(@SaveBuf);
+  if not FileNameCasePreserving then
+   dir:=upcase(dir);
+    {todo: massive loss of encoding and number of chars}
+{$else WINCE}
+  Dir:='\';
+{$endif WINCE}
+end;
+
+procedure do_GetDir (DriveNr: byte; var Dir: RawByteString);
+
+var ldir : Unicodestring;
+begin
+  do_GetDir(DriveNr,ldir);
+  dir:=ToSingleByteFileSystemEncodedFileName(ldir);
+end;
+{$else}
 procedure dirfn(afunc : TDirFnType;s:pchar;len:integer);
 begin
   DoDirSeparators(s);
@@ -35,14 +152,14 @@ begin
   CreateDirectoryTrunc:=CreateDirectory(name,nil);
 end;
 
-Procedure MkDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_MKDIR'];
+Procedure do_MkDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_MKDIR'];
 begin
   If not assigned(s) or (len=0) or (InOutRes <> 0) then
    exit;
   dirfn(TDirFnType(@CreateDirectoryTrunc),s,len);
 end;
 
-Procedure RmDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_RMDIR'];
+Procedure do_RmDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_RMDIR'];
 
 begin
   if (len=1) and (s^ ='.') then
@@ -60,7 +177,7 @@ begin
 {$endif WINCE}
 end;
 
-Procedure ChDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_CHDIR'];
+Procedure do_ChDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_CHDIR'];
 
 begin
 {$ifndef WINCE}
@@ -110,3 +227,4 @@ begin
   Dir:='\';
 {$endif WINCE}
 end;
+{$endif}

+ 66 - 2
rtl/win/sysfile.inc

@@ -31,7 +31,47 @@ begin
   CloseHandle(h);
 end;
 
+{$ifdef FPC_UNICODE_RTL}
+procedure do_erase(const p : unicodestring);
+var lp:unicodestring;
+begin
+   lp:=p;
+   DoDirSeparators(lp);
+   if DeleteFile(punicodechar(lp))=0 then
+    Begin
+      errno:=GetLastError;
+      if errno=5 then
+       begin
+         if ((GetFileAttributes(punicodechar(lp)) and FILE_ATTRIBUTE_DIRECTORY)=FILE_ATTRIBUTE_DIRECTORY) then
+          errno:=2;
+       end;
+      Errno2InoutRes;
+    end;
+end;
 
+procedure do_erase(const p : rawbytestring);
+begin
+   Do_Erase(Unicodestring(p));
+end;
+
+procedure do_rename(const p1:unicodestring;const p2 : unicodestring);
+var lp1,lp2:unicodestring;
+begin
+  lp1:=p1; lp2:=p2;
+  DoDirSeparators(lp1);
+  DoDirSeparators(lp2);
+  if MoveFile(punicodechar(lp1),punicodechar(lp2))=0 then
+   Begin
+      errno:=GetLastError;
+      Errno2InoutRes;
+   end;
+end;
+
+procedure do_rename(const p1:unicodestring;const p2 : rawbytestring);
+begin
+  do_rename(p1,unicodestring(p2));
+end;
+{$else}
 procedure do_erase(p : pchar);
 begin
    DoDirSeparators(p);
@@ -59,7 +99,7 @@ begin
    end;
 end;
 
-
+{$endif}
 function do_write(h:thandle;addr:pointer;len : longint) : longint;
 var
    size:longint;
@@ -191,7 +231,11 @@ begin
 end;
 
 
+{$ifdef FPC_UNICODE_RTL}
+procedure do_open(var f;const op:unicodestring;flags:longint);
+{$else}
 procedure do_open(var f;p:pchar;flags:longint);
+{$endif}
 {
   filerec and textrec have both handle and mode as the first items so
   they could use the same routine for opening/creating.
@@ -207,7 +251,13 @@ Var
   shflags,
   oflags,cd : longint;
   security : TSecurityAttributes;
+{$ifdef FPC_UNICODE_RTL}
+  p : unicodestring;
+{$endif}
 begin
+{$ifdef FPC_UNICODE_RTL}
+  p:=op;
+{$endif}
   DoDirSeparators(p);
   { close first if opened }
   if ((flags and $10000)=0) then
@@ -266,7 +316,11 @@ begin
   else
     cd:=OPEN_EXISTING;
   { empty name is special }
+{$ifdef FPC_UNICODE_RTL}
+  if p='' then
+{$else}
   if p[0]=#0 then
+{$endif}
    begin
      case FileRec(f).mode of
        fminput :
@@ -285,8 +339,11 @@ begin
   security.nLength := Sizeof(TSecurityAttributes);
   security.bInheritHandle:=true;
   security.lpSecurityDescriptor:=nil;
+  {$ifdef FPC_UNICODE_RTL}
+  filerec(f).handle:=CreateFile(punicodechar(p),oflags,shflags,@security,cd,FILE_ATTRIBUTE_NORMAL,0);
+  {$else}
   filerec(f).handle:=CreateFile(p,oflags,shflags,@security,cd,FILE_ATTRIBUTE_NORMAL,0);
-
+  {$endif}
   { append mode }
   if ((flags and $100)<>0) and
      (filerec(f).handle<>0) and
@@ -304,3 +361,10 @@ begin
       Errno2InoutRes;
     end;
 end;
+
+{$ifdef FPC_UNICODE_RTL}
+procedure do_open(var f;const p:rawbytestring;flags:longint);
+begin
+ do_open(f,UnicodeString(p),flags);  
+end;
+{$endif}

+ 3 - 4
rtl/win/sysos.inc

@@ -218,7 +218,7 @@ threadvar
    function GetStdHandle(nStdHandle:DWORD):THANDLE;
      stdcall;external KernelDLL name 'GetStdHandle';
 
-   {$ifdef FPC_UNICODE_RTL}
+   {$ifdef FPC_UNICODE_RTLx}
    { command line/environment functions }
 
    function GetCommandLine : pwidechar;
@@ -281,7 +281,7 @@ threadvar
    function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
 
    {$ifdef FPC_UNICODE_RTL}
-    function GetFileAttributes(p : punicodechar) : dword;
+   function GetFileAttributes(p : punicodechar) : dword;
      stdcall;external KernelDLL name 'GetFileAttributesW';
    function DeleteFile(p : punicodechar) : longint;
      stdcall;external KernelDLL name 'DeleteFileW';
@@ -298,9 +298,8 @@ threadvar
      stdcall;external KernelDLL name 'RemoveDirectoryW';
    function SetCurrentDirectory(name : pointer) : longbool;
      stdcall;external KernelDLL name 'SetCurrentDirectoryW';
-   function GetCurrentDirectory(bufsize : longint;name : punicodechar) : longbool;
+   function GetCurrentDirectory(bufsize : longint;name : punicodechar) : Dword;
      stdcall;external KernelDLL name 'GetCurrentDirectoryW';
-
    {$else}
    function GetFileAttributes(p : pchar) : dword;
      stdcall;external KernelDLL name 'GetFileAttributesA';

+ 256 - 53
rtl/win/sysutils.pp

@@ -124,12 +124,12 @@ function GetFileVersion(const AFileName:string):Cardinal;
     result:=$fffffff;
     fn:=AFileName;
     UniqueString(fn);
-    size:=GetFileVersionInfoSize(pchar(fn),@h);
+    size:=GetFileVersionInfoSizeA(pchar(fn),@h);
     if size>sizeof(buf) then
       begin
         getmem(bufp,size);
         try
-          if GetFileVersionInfo(pchar(fn),h,size,bufp) then
+          if GetFileVersionInfoA(pchar(fn),h,size,bufp) then
             if VerQueryValue(bufp,'\',valrec,valsize) then
               result:=valrec^.dwFileVersionMS;
         finally
@@ -138,7 +138,7 @@ function GetFileVersion(const AFileName:string):Cardinal;
       end
     else
       begin
-        if GetFileVersionInfo(pchar(fn),h,size,@buf) then
+        if GetFileVersionInfoA(pchar(fn),h,size,@buf) then
           if VerQueryValue(@buf,'\',valrec,valsize) then
             result:=valrec^.dwFileVersionMS;
       end;
@@ -154,6 +154,9 @@ function GetFileVersion(const AFileName:string):Cardinal;
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 
 
+{define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+{$define SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
+
 function ConvertEraYearString(Count ,Year,Month,Day : integer) : string; forward;
 function ConvertEraString(Count ,Year,Month,Day : integer) : string; forward;
 { Include platform independent implementation part }
@@ -192,12 +195,12 @@ begin
   getmem(buf,size);
 
   try
-    rc := WNetGetUniversalName (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
+    rc := WNetGetUniversalNameA (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
 
     if rc=ERROR_MORE_DATA then
       begin
         buf:=reallocmem(buf,size);
-        rc := WNetGetUniversalName (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
+        rc := WNetGetUniversalNameA (pchar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
       end;
     if rc = NO_ERROR then
       Result := PRemoteNameInfo(buf)^.lpUniversalName
@@ -210,6 +213,39 @@ begin
   end;
 end;
 
+function ExpandUNCFileName (const filename:unicodestring) : string;
+{ returns empty string on errors }
+var
+  s    : unicodestring;
+  size : dword;
+  rc   : dword;
+  buf : pwidechar;
+begin
+  s := ExpandFileName (filename);
+
+  s := s + #0;
+
+  size := max_path;
+  getmem(buf,size);
+
+  try
+    rc := WNetGetUniversalNameW (pwidechar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
+
+    if rc=ERROR_MORE_DATA then
+      begin
+        buf:=reallocmem(buf,size);
+        rc := WNetGetUniversalNameW (pwidechar(s), UNIVERSAL_NAME_INFO_LEVEL, buf, @size);
+      end;
+    if rc = NO_ERROR then
+      Result := PRemoteNameInfo(buf)^.lpUniversalName
+    else if rc = ERROR_NOT_CONNECTED then
+      Result := filename
+    else
+      Result := '';
+  finally
+    freemem(buf);
+  end;
+end;
 
 {****************************************************************************
                               File Functions
@@ -227,27 +263,53 @@ const
                FILE_SHARE_WRITE,
                FILE_SHARE_READ or FILE_SHARE_WRITE);
 
+
+Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
+begin
+  result := CreateFileW(PWideChar(FileName), dword(AccessMode[Mode and 3]),
+                       dword(ShareModes[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
+                       FILE_ATTRIBUTE_NORMAL, 0);
+  //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
+end;
+{$ifdef unused}
 Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
 begin
-  result := CreateFile(PChar(FileName), dword(AccessMode[Mode and 3]),
+  result := CreateFileA(PChar(FileName), dword(AccessMode[Mode and 3]),
                        dword(ShareModes[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL, 0);
   //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
 end;
 
-Function FileCreate (Const FileName : String) : THandle;
+Function FileCreate (Const FileName : RawbyteString) : THandle;
+begin
+  FileCreate:=FileCreate(FileName, fmShareExclusive, 0);
+end;
+
+Function FileCreate (Const FileName : RawbyteString; Rights:longint) : THandle;
+begin
+  FileCreate:=FileCreate(FileName, fmShareExclusive, Rights);
+end;
+
+Function FileCreate (Const FileName : RawbyteString; ShareMode : Integer; Rights : Integer) : THandle;
+begin
+  Result := CreateFileA(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
+                       dword(ShareModes[(ShareMode and $F0) shr 4]), nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
+end;
+{$endif}
+
+Function FileCreate (Const FileName : UnicodeString) : THandle;
 begin
   FileCreate:=FileCreate(FileName, fmShareExclusive, 0);
 end;
 
-Function FileCreate (Const FileName : String; Rights:longint) : THandle;
+Function FileCreate (Const FileName : UnicodeString; Rights:longint) : THandle;
 begin
   FileCreate:=FileCreate(FileName, fmShareExclusive, Rights);
 end;
 
-Function FileCreate (Const FileName : String; ShareMode : Integer; Rights : Integer) : THandle;
+Function FileCreate (Const FileName : UnicodeString; ShareMode : Integer; Rights : Integer) : THandle;
 begin
-  Result := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
+  Result := CreateFileW(PwideChar(FileName), GENERIC_READ or GENERIC_WRITE,
                        dword(ShareModes[(ShareMode and $F0) shr 4]), nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
 end;
 
@@ -327,7 +389,7 @@ begin
                 FileTimeToDosDateTime(lft,Longrec(Dtime).Hi,LongRec(DTIME).lo);
 end;
 
-
+{$ifdef unused}
 Function FileAge (Const FileName : String): Longint;
 var
   Handle: THandle;
@@ -343,38 +405,80 @@ begin
     end;
   Result := -1;
 end;
+{$endif}
+
+Function FileAge (Const FileName : UnicodeString): Longint;
+var
+  Handle: THandle;
+  FindData: TWin32FindDataW;
+begin
+  Handle := FindFirstFileW(Pwidechar(FileName), FindData);
+  if Handle <> INVALID_HANDLE_VALUE then
+    begin
+      Windows.FindClose(Handle);
+      if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
+        If WinToDosTime(FindData.ftLastWriteTime,Result) then
+          exit;
+    end;
+  Result := -1;
+end;
 
+{$ifdef unused}
 
 Function FileExists (Const FileName : String) : Boolean;
 var
   Attr:Dword;
 begin
-  Attr:=GetFileAttributes(PChar(FileName));
+  Attr:=GetFileAttributesA(PChar(FileName));
   if Attr <> $ffffffff then
     Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) = 0
   else
     Result:=False;
 end;
+{$endif}
 
+Function FileExists (Const FileName : UnicodeString) : Boolean;
+var
+  Attr:Dword;
+begin
+  Attr:=GetFileAttributesW(PWideChar(FileName));
+  if Attr <> $ffffffff then
+    Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) = 0
+  else
+    Result:=False;
+end;
 
+{$ifdef unused}
 Function DirectoryExists (Const Directory : String) : Boolean;
 var
   Attr:Dword;
 begin
-  Attr:=GetFileAttributes(PChar(Directory));
+  Attr:=GetFileAttributesA(PChar(Directory));
   if Attr <> $ffffffff then
     Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) > 0
   else
     Result:=False;
 end;
+{$endif}
+
 
+Function DirectoryExists (Const Directory : UnicodeString) : Boolean;
+var
+  Attr:Dword;
+begin
+  Attr:=GetFileAttributesW(PWideChar(Directory));
+  if Attr <> $ffffffff then
+    Result:= (Attr and FILE_ATTRIBUTE_DIRECTORY) > 0
+  else
+    Result:=False;
+end;
 
-Function FindMatch(var f: TSearchRec) : Longint;
+Function FindMatch(var f: TRawByteSearchRec) : Longint;
 begin
   { Find file with correct attribute }
   While (F.FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
    begin
-     if not FindNextFile (F.FindHandle,F.FindData) then
+     if not FindNextFileA (F.FindHandle,F.FindData) then
       begin
         Result:=GetLastError;
         exit;
@@ -388,15 +492,14 @@ begin
   Result:=0;
 end;
 
-
-Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
+Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TRawByteSearchRec) : Longint;
 begin
   Rslt.Name:=Path;
   Rslt.Attr:=attr;
   Rslt.ExcludeAttr:=(not Attr) and ($1e);
                  { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
   { FindFirstFile is a Win32 Call }
-  Rslt.FindHandle:=FindFirstFile (PChar(Path),Rslt.FindData);
+  Rslt.FindHandle:=FindFirstFileA (PChar(Path),Rslt.FindData);
   If Rslt.FindHandle=Invalid_Handle_value then
    begin
      Result:=GetLastError;
@@ -406,8 +509,7 @@ begin
   Result:=FindMatch(Rslt);
 end;
 
-
-Function FindNext (Var Rslt : TSearchRec) : Longint;
+Function FindNext (Var Rslt : TRawByteSearchRec) : Longint;
 begin
   if FindNextFile(Rslt.FindHandle, Rslt.FindData) then
     Result := FindMatch(Rslt)
@@ -416,12 +518,63 @@ begin
 end;
 
 
-Procedure FindClose (Var F : TSearchrec);
+Procedure FindClose (Var F : TRawByteSearchRec);
 begin
    if F.FindHandle <> INVALID_HANDLE_VALUE then
     Windows.FindClose(F.FindHandle);
 end;
 
+Function FindMatch(var f: TUnicodeSearchRec) : Longint;
+begin
+  { Find file with correct attribute }
+  While (F.FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
+   begin
+     if not FindNextFileW (F.FindHandle,F.FindData) then
+      begin
+        Result:=GetLastError;
+        exit;
+      end;
+   end;
+  { Convert some attributes back }
+  WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
+  f.size:=F.FindData.NFileSizeLow+(qword(maxdword)+1)*F.FindData.NFileSizeHigh;
+  f.attr:=F.FindData.dwFileAttributes;
+  f.Name:=StrPas(@F.FindData.cFileName[0]);
+  Result:=0;
+end;
+
+
+Function FindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TUnicodeSearchRec) : Longint;
+begin
+  Rslt.Name:=Path;
+  Rslt.Attr:=attr;
+  Rslt.ExcludeAttr:=(not Attr) and ($1e);
+                 { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
+  { FindFirstFile is a Win32 Call }
+  Rslt.FindHandle:=FindFirstFileW (PWideChar(Path),Rslt.FindData);
+  If Rslt.FindHandle=Invalid_Handle_value then
+   begin
+     Result:=GetLastError;
+     exit;
+   end;
+  { Find file with correct attribute }
+  Result:=FindMatch(Rslt);
+end;
+
+Function FindNext (Var Rslt : TUnicodeSearchRec) : Longint;
+begin
+  if FindNextFileW(Rslt.FindHandle, Rslt.FindData) then
+    Result := FindMatch(Rslt)
+  else
+    Result := GetLastError;
+end;
+
+
+Procedure FindClose (Var F : TUnicodeSearchrec);
+begin
+   if F.FindHandle <> INVALID_HANDLE_VALUE then
+    Windows.FindClose(F.FindHandle);
+end;
 
 Function FileGetDate (Handle : THandle) : Longint;
 Var
@@ -446,15 +599,15 @@ begin
 end;
 
 
-Function FileGetAttr (Const FileName : String) : Longint;
+{Function FileGetAttr (Const FileName : String) : Longint;
 begin
-  Result:=Longint(GetFileAttributes(PChar(FileName)));
+  Result:=Longint(GetFileAttributesA(PChar(FileName)));
 end;
 
 
 Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
 begin
-  if SetFileAttributes(PChar(FileName), Attr) then
+  if SetFileAttributesA(PChar(FileName), Attr) then
     Result:=0
   else
     Result := GetLastError;
@@ -463,16 +616,41 @@ end;
 
 Function DeleteFile (Const FileName : String) : Boolean;
 begin
-  Result:=Windows.DeleteFile(Pchar(FileName));
+  Result:=Windows.DeleteFileA(Pchar(FileName));
 end;
 
 
 Function RenameFile (Const OldName, NewName : String) : Boolean;
 begin
-  Result := MoveFile(PChar(OldName), PChar(NewName));
+  Result := MoveFileA(PChar(OldName), PChar(NewName));
+end;
+}
+Function FileGetAttr (Const FileName : UnicodeString) : Longint;
+begin
+  Result:=Longint(GetFileAttributesW(PWideChar(FileName)));
+end;
+
+
+Function FileSetAttr (Const Filename : UnicodeString; Attr: longint) : Longint;
+begin
+  if SetFileAttributesW(PWideChar(FileName), Attr) then
+    Result:=0
+  else
+    Result := GetLastError;
+end;
+
+
+Function DeleteFile (Const FileName : UnicodeString) : Boolean;
+begin
+  Result:=Windows.DeleteFileW(PWidechar(FileName));
 end;
 
 
+Function RenameFile (Const OldName, NewName : UnicodeString) : Boolean;
+begin
+  Result := MoveFileW(PWideChar(OldName), PWideChar(NewName));
+end;
+
 {****************************************************************************
                               Disk Functions
 ****************************************************************************}
@@ -563,19 +741,19 @@ end;
 
 Function SetCurrentDir (Const NewDir : String) : Boolean;
 begin
-  Result:=SetCurrentDirectory(PChar(NewDir));
+  Result:=SetCurrentDirectoryA(PChar(NewDir));
 end;
 
 
 Function CreateDir (Const NewDir : String) : Boolean;
 begin
-  Result:=CreateDirectory(PChar(NewDir),nil);
+  Result:=CreateDirectoryA(PChar(NewDir),nil);
 end;
 
 
 Function RemoveDir (Const Dir : String) : Boolean;
 begin
-  Result:=RemoveDirectory(PChar(Dir));
+  Result:=RemoveDirectoryA(PChar(Dir));
 end;
 
 
@@ -662,7 +840,7 @@ var
   L: Integer;
   Buf: array[0..255] of Char;
 begin
-  L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf));
+  L := GetLocaleInfoA(LID, LT, Buf, SizeOf(Buf));
   if L > 0 then
     SetString(Result, @Buf[0], L - 1)
   else
@@ -674,7 +852,7 @@ function GetLocaleChar(LID, LT: Longint; Def: Char): Char;
 var
   Buf: array[0..3] of Char; // sdate allows 4 chars.
 begin
-  if GetLocaleInfo(LID, LT, Buf, sizeof(buf)) > 0 then
+  if GetLocaleInfoA(LID, LT, Buf, sizeof(buf)) > 0 then
     Result := Buf[0]
   else
     Result := Def;
@@ -693,7 +871,7 @@ begin
 
   ALCID := GetThreadLocale;
 //  ALCID := SysLocale.DefaultLCID;
-  if GetDateFormat(ALCID , DATE_USE_ALT_CALENDAR
+  if GetDateFormatA(ALCID , DATE_USE_ALT_CALENDAR
       , @ASystemTime, PChar('gg')
       , @buf, SizeOf(buf)) > 0 then
   begin
@@ -736,7 +914,7 @@ begin
   ALCID := GetThreadLocale;
 //  ALCID := SysLocale.DefaultLCID;
 
-  if GetDateFormat(ALCID, DATE_USE_ALT_CALENDAR
+  if GetDateFormatA(ALCID, DATE_USE_ALT_CALENDAR
       , @ASystemTime, PChar(AFormatText)
       , @buf, SizeOf(buf)) > 0 then
   begin
@@ -798,7 +976,7 @@ begin
      EraNames[i] := '';  EraYearOffsets[i] := -1;
    end;
   ALCID := GetThreadLocale;
-  if GetLocaleInfo(ALCID , LOCALE_IOPTIONALCALENDAR, buf, sizeof(buf)) <= 0 then exit;
+  if GetLocaleInfoA(ALCID , LOCALE_IOPTIONALCALENDAR, buf, sizeof(buf)) <= 0 then exit;
   ACALID := StrToIntDef(buf,1);
 
   if ACALID in [3..5] then
@@ -953,7 +1131,7 @@ end;
 { GetEnvironmentStrings cannot be checked by CheckPointer function }
 {$checkpointer off}
 
-Function GetEnvironmentVariable(Const EnvVar : String) : String;
+Function GetEnvironmentVariable(Const EnvVar : Ansistring) : String;
 
 var
    s : string;
@@ -961,7 +1139,7 @@ var
    hp,p : pchar;
 begin
    Result:='';
-   p:=GetEnvironmentStrings;
+   p:=GetEnvironmentStringsA;
    hp:=p;
    while hp^<>#0 do
      begin
@@ -975,7 +1153,32 @@ begin
         { next string entry}
         hp:=hp+strlen(hp)+1;
      end;
-   FreeEnvironmentStrings(p);
+   FreeEnvironmentStringsA(p);
+end;
+
+Function GetEnvironmentVariable(Const EnvVar : UnicodeString) : UnicodeString;
+
+var
+   s : Unicodestring;
+   i : longint;
+   hp,p : pwidechar;
+begin
+   Result:='';
+   p:=GetEnvironmentStringsW;
+   hp:=p;
+   while hp^<>#0 do
+     begin
+        s:=strpas(hp);
+        i:=pos('=',s);
+        if uppercase(copy(s,1,i-1))=uppercase(envvar) then
+          begin
+             Result:=copy(s,i+1,length(s)-i);
+             break;
+          end;
+        { next string entry}
+        hp:=hp+strlen(hp)+1;
+     end;
+   FreeEnvironmentStringsW(p);
 end;
 
 Function GetEnvironmentVariableCount : Integer;
@@ -984,7 +1187,7 @@ var
   hp,p : pchar;
 begin
   Result:=0;
-  p:=GetEnvironmentStrings;
+  p:=GetEnvironmentStringsA;
   hp:=p;
   If (Hp<>Nil) then
     while hp^<>#0 do
@@ -992,7 +1195,7 @@ begin
       Inc(Result);
       hp:=hp+strlen(hp)+1;
       end;
-  FreeEnvironmentStrings(p);
+  FreeEnvironmentStringsA(p);
 end;
 
 Function GetEnvironmentString(Index : Integer) : String;
@@ -1001,7 +1204,7 @@ var
   hp,p : pchar;
 begin
   Result:='';
-  p:=GetEnvironmentStrings;
+  p:=GetEnvironmentStringsA;
   hp:=p;
   If (Hp<>Nil) then
     begin
@@ -1013,7 +1216,7 @@ begin
     If (hp^<>#0) then
       Result:=StrPas(HP);
     end;
-  FreeEnvironmentStrings(p);
+  FreeEnvironmentStringsA(p);
 end;
 
 {$pop}
@@ -1048,7 +1251,7 @@ begin
 
   ExecInherits:=ExecInheritsHandles in Flags;
 
-  if not CreateProcess(nil, pchar(CommandLine),
+  if not CreateProcessA(nil, pchar(CommandLine),
     Nil, Nil, ExecInherits,$20, Nil, Nil, SI, PI) then
     begin
       e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,GetLastError]);
@@ -1159,7 +1362,7 @@ Procedure InitSysConfigDir;
 
 begin
   SetLength(SysConfigDir, MAX_PATH);
-  SetLength(SysConfigDir, GetWindowsDirectory(PChar(SysConfigDir), MAX_PATH));
+  SetLength(SysConfigDir, GetWindowsDirectoryA(PChar(SysConfigDir), MAX_PATH));
 end;
 
 {****************************************************************************
@@ -1211,7 +1414,7 @@ function Win32AnsiUpperCase(const s: string): string;
       begin
         result:=s;
         UniqueString(result);
-        CharUpperBuff(pchar(result),length(result));
+        CharUpperBuffA(pchar(result),length(result));
       end
     else
       result:='';
@@ -1224,7 +1427,7 @@ function Win32AnsiLowerCase(const s: string): string;
       begin
         result:=s;
         UniqueString(result);
-        CharLowerBuff(pchar(result),length(result));
+        CharLowerBuffA(pchar(result),length(result));
       end
     else
       result:='';
@@ -1233,52 +1436,52 @@ function Win32AnsiLowerCase(const s: string): string;
 
 function Win32AnsiCompareStr(const S1, S2: string): PtrInt;
   begin
-    result:=CompareString(LOCALE_USER_DEFAULT,0,pchar(s1),length(s1),
+    result:=CompareStringA(LOCALE_USER_DEFAULT,0,pchar(s1),length(s1),
       pchar(s2),length(s2))-2;
   end;
 
 
 function Win32AnsiCompareText(const S1, S2: string): PtrInt;
   begin
-    result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pchar(s1),length(s1),
+    result:=CompareStringA(LOCALE_USER_DEFAULT,NORM_IGNORECASE,pchar(s1),length(s1),
       pchar(s2),length(s2))-2;
   end;
 
 
 function Win32AnsiStrComp(S1, S2: PChar): PtrInt;
   begin
-    result:=CompareString(LOCALE_USER_DEFAULT,0,s1,-1,s2,-1)-2;
+    result:=CompareStringA(LOCALE_USER_DEFAULT,0,s1,-1,s2,-1)-2;
   end;
 
 
 function Win32AnsiStrIComp(S1, S2: PChar): PtrInt;
   begin
-    result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,-1,s2,-1)-2;
+    result:=CompareStringA(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,-1,s2,-1)-2;
   end;
 
 
 function Win32AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
   begin
-    result:=CompareString(LOCALE_USER_DEFAULT,0,s1,maxlen,s2,maxlen)-2;
+    result:=CompareStringA(LOCALE_USER_DEFAULT,0,s1,maxlen,s2,maxlen)-2;
   end;
 
 
 function Win32AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
   begin
-    result:=CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,maxlen,s2,maxlen)-2;
+    result:=CompareStringA(LOCALE_USER_DEFAULT,NORM_IGNORECASE,s1,maxlen,s2,maxlen)-2;
   end;
 
 
 function Win32AnsiStrLower(Str: PChar): PChar;
   begin
-    CharLower(str);
+    CharLowerA(str);
     result:=str;
   end;
 
 
 function Win32AnsiStrUpper(Str: PChar): PChar;
   begin
-    CharUpper(str);
+    CharUpperA(str);
     result:=str;
   end;
 

+ 9 - 2
rtl/win/syswin.inc

@@ -230,15 +230,22 @@ const
     paramcount := argc - 1;
   end;
 
-  function paramstr(l : longint) : string;
+{$ifdef xFPC_UNICODE_RTL}
+  function paramstr(l : longint) : unicodestring;
+{$else}
+  function paramstr(l : longint) : rawbytestring;
+{$endif}
   begin
     if (l>=0) and (l<argc) then
+{$ifdef FPC_UNICODE_RTL}
+      paramstr:=(argv[l])
+{$else}
       paramstr:=strpas(argv[l])
+{$endif}
     else
       paramstr:='';
   end;
 
-
   procedure randomize;
   begin
     randseed:=GetTickCount;

+ 6 - 3
rtl/win/windirs.pp

@@ -59,8 +59,11 @@ uses
   sysutils;
 
 Type
+{$ifdef FPC_UNICODE_RTL}
+  PFNSHGetFolderPath = Function(Ahwnd: HWND; Csidl: Integer; Token: THandle; Flags: DWord; Path: {$ifdef FPC_UNICODE_RTL}PWideChar{$ELSE}PChar{$ENDIF}): HRESULT; stdcall;
+{$else}
   PFNSHGetFolderPath = Function(Ahwnd: HWND; Csidl: Integer; Token: THandle; Flags: DWord; Path: PChar): HRESULT; stdcall;
-
+{$endif}
 
 var
   SHGetFolderPath : PFNSHGetFolderPath = Nil;
@@ -69,7 +72,7 @@ var
 Procedure InitDLL;
 
 Var
-  pathBuf: array[0..MAX_PATH-1] of char;
+  pathBuf: array[0..MAX_PATH-1] of {$ifdef FPC_UNICODE_RTL}WideChar{$else}char{$endif};
   pathLength: Integer;
 begin
   { Load shfolder.dll using a full path, in order to prevent spoofing (Mantis #18185)
@@ -83,7 +86,7 @@ begin
 
     if (CFGDLLHandle<>0) then
     begin
-      Pointer(ShGetFolderPath):=GetProcAddress(CFGDLLHandle,'SHGetFolderPathA');
+      Pointer(ShGetFolderPath):=GetProcAddress(CFGDLLHandle,{$ifdef FPC_UNICODE_RTL}'SHGetFolderPathW'{$else}'SHGetFolderPathA'{$endif});
       If @ShGetFolderPath=nil then
       begin
         FreeLibrary(CFGDLLHandle);