Search code examples
delphiwinapiunicodedelphi-xe2ansi

winapinameA vs winapinameW - Unicode vs Ansi - Delphi XE2


I am using Delphi XE2 and importing SHGetFolderPath from Shell32.dll. I am running windows vista x64. When running SHGetFolderPathA The result is unclear.

I.E.:

uses
  Windows;

function SHGetFolderPath(hwnd: Cardinal; csidl: Integer; hToken: Cardinal; dwFlags: Cardinal; pszPath: PChar): Cardinal; stdcall;external 'Shell32.dll' name 'SHGetFolderPathA';

Var
  Path:Array [0..MAX_PATH] of Char;
  AppData:String;

begin
  SHGetFolderPath(0,$001A,0,0,@path[0]);
  MessageBox(0,Path,'a',0);
end.

The result is:

enter image description here

Versus using SHGetFolderPathW:

uses Windows;

function SHGetFolderPath(hwnd: Cardinal; csidl: Integer; hToken: Cardinal; dwFlags: Cardinal; pszPath: PChar): Cardinal; stdcall;external 'Shell32.dll' name 'SHGetFolderPathW';

Var
  Path:Array [0..MAX_PATH] of Char;
  AppData:String;

begin
  SHGetFolderPath(0,$001A,0,0,@path[0]);
  MessageBox(0,Path,'a',0);
end.

The result clearly states the path to my AppData folder with no issue.

When running this same code on x32 Vista when using SHGetFolderPathA works just fine.

If someone could shine some light on why this is? I was under the impression the "W" api was usually for use on Unicode machines...?


Edit:

I am now using the following code, with the same error:

uses
  Windows;

function SHGetFolderPath(hwnd: HWND; csidl: Integer; hToken: THandle; dwFlags: DWord; pszPath: PAnsiChar): HRESULT; stdcall; external 'SHFolder.dll' name 'SHGetFolderPathA';

var
  path: array[0..MAX_PATH] of char;
begin
  SHGetFolderPath(0,$001A,0,0, @path[0]);
  MessageBox(0,path,'a',0);
end.

Final Edit:

Thank you all for the responses. The above declaration of SHGetFolderPath is fine. After looking through all responses, and taking in the information in each, I have come up with the following result:

uses
  Windows;

function SHGetFolderPath(hwnd: HWND; csidl: Integer; hToken: THandle; dwFlags: DWord; pszPath: PAnsiChar): HRESULT; stdcall; external 'shell32.dll' name 'SHGetFolderPathA';

var
  path: array[0..MAX_PATH] of ansichar;
  xRes:String;
begin
  If SHGetFolderPath(0,$001A,0,0, @path[0]) = S_OK Then Begin
    xRes := Path;
    MessageBox(0,PWideChar(xRes),'Result',0);
  End Else
    MessageBox(0,'An error has occurred.','Result',0);
end.

The resulting messagebox correctly displays the path to my AppData path.

Thanks again for all of the responses.


Solution

  • You declared the final parameter of SHGetFolderPath as having type PChar. In Delphi 2009 and later (which includes your version, Delphi XE2), PChar is an alias for PWideChar, yet you told Delphi to link your declared function to the "A" version, which expects AnsiChar characters.

    When declaring functions that are sensitive to the character type, I recommend not using PChar at all. Explicitly use either PWideChar or PAnsiChar.

    The "A" and "W" suffixes aren't about which kind of machine the program runs on. All supported versions of Windows are Unicode now — the last non-Unicode version was Windows ME. The "A" and "W" refer to the character type of the arguments.

    As for why your code worked on the 32-bit version of Vista when it fails on the 64-bit version, I have no explanation. Maybe you're just lucky and the OS did some conversions that happened to work out OK in your case.