简体   繁体   中英

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.

IE:

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:

在此处输入图片说明

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.

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.

Char is Unicode in XE2, but you are importing the Ansi version of SHGetFolderPath() and passing a Unicode output buffer to it. That is why your MessageBox() is displaying garbage - it is trying to display Ansi data in a Unicode dialog box. You need to import the Unicode version of SHGetFolderPath() instead:

uses
  Windows;

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

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

BTW, XE2 already imports both Ansi and Unicode versions of SHGetFolderPath() for you, as well as defined the CSIDL values, so you do not need to do it manually:

uses
  Windows, ShlObj, SHFolder;

var
  path: array[0..MAX_PATH] of Char;
begin
  SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, path);
  MessageBox(0, path, 'a', 0);
end.

In your code I see two mistakes one mistake:

- "Path" is declared as local variable (ie on the Stack) so it contains "random"/stale data. It is recommended to initialize it with zeroes: fillChar(path[0], sizeOf(path), #0)

  • SHGetFolderPath is a function. It returns a value. You never test function's result to see if it secceeded or not - you just output the contents of the "path" variable. But are the contents meaningful if SHGetFolderPath had failed for some reason?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM