Delphi Wine 函数的映射 wine_nt_to_unix_file_name

Delphi mapping for Wine function wine_nt_to_unix_file_name

如何在 Delphi (10.4) 中从 WINE 的 ntdll.dll 正确调用 wine_nt_to_unix_file_name?

我在网上找到的定义是这样的:

NTSTATUS wine_nt_to_unix_file_name(const UNICODE_STRING *nameW, ANSI_STRING *unix_name_ret, UINT disposition, BOOLEAN check_case)

Disposition 改变了不存在的最后路径部分的 return 结果,check_case 是不言自明的。

当 运行 在 WINE 中时,我想使用此功能向用户显示我的应用程序的真实 unix 路径。这应该让中等用户更容易找到一个文件夹来在本机应用程序和 WINE 环境之间共享数据。

我尝试了什么:

type 
TWineGetVersion = function: PAnsiChar; stdcall;   
TWineNTToUnixFileName = procedure(pIn: Pointer; pOut: Pointer; aParam: integer; caseSens: Boolean); stdcall;
...
initialization
try
  LHandle := LoadLibrary('ntdll.dll');
  if LHandle > 32 then
  begin
    LWineGetVersion := GetProcAddress(LHandle, 'wine_get_version');
    LWineNTToUnixFileName := GetProcAddress(LHandle, 'wine_nt_to_unix_file_name');
  end;
except
  LWineGetVersion := nil;
  LWineNTToUnixFileName := nil;
end;

检索 WINE 版本效果很好,但我无法进行路径转换,运行 因为我不知道如何处理指向 ANSI_STRING 的 returned 指针成为这样的 Windows 结构:

typedef struct _STRING {
  USHORT Length;
  USHORT MaximumLength;
  PCHAR  Buffer;
} STRING;

我试着用这种方式解决问题:

MyBuffer: array [0 .. 2048] of AnsiChar;
LWineNTToUnixFileName(PChar(aWinPath), @MyBuffer, 0, true);

但是函数在逐字节输出时return清空缓冲区中的总垃圾。

更新 按照对当前 Wine 源的提示和结构的提示,我尝试了这个版本,不幸的是提供了垃圾。第一个参数是一个 UNICODE STRING 结构,第二个是一个简单的 ansistring。第三个参数接收returned缓冲区的长度。

type
TWineNTToUnixFileName = procedure(pIn: Pointer; pOut: Pointer; aLen: Pointer); stdcall;
  TWineUnicodeString = packed record
    Len: Word;
    MaxLen: Word;
    Buffer: PWideChar;
  end;

function WinePath(const aWinPath: String): String;
var
  inString: TWineUnicodeString;
  MyBuffer: array [0 .. 2048] of AnsiChar;
  aLen,i: integer;
begin
  inString.Buffer := PChar(aWinPath);
  inString.Len := length(aWinPath);
  inString.MaxLen := inString.Len;
  LWineNTToUnixFileName(@inString, @MyBuffer, @aLen);
  result := '';
  for i := 1 to 20 do
    result := result + MyBuffer[i];
end;

基于 Zeds 的出色回答,我创建了这个函数,如果旧调用失败,它会自动尝试新的 API 调用

type
  TWineAnsiString = packed record
    Len: Word;
    MaxLen: Word;
    Buffer: PAnsiChar;
  end;

  PWineAnsiString = ^TWineAnsiString;

  TWineUnicodeString = packed record
    Len: Word;
    MaxLen: Word;
    Buffer: PWideChar;
  end;

  PWineUnicodeString = ^TWineUnicodeString;

var
  wine_get_version: function: PAnsiChar; cdecl;
  // Both are assigned to the function in ntdll.dll to be able to try both alternatives
  wine_nt_to_unix_file_name: function(const nameW: PWineUnicodeString; unix_name_ret: PWineAnsiString; disposition: Cardinal): Cardinal; cdecl;
  wine_nt_to_unix_file_name_1: function(const nameW: PWineUnicodeString; nameA: PAnsiChar; Sz: PCardinal; disposition: Cardinal): Cardinal; cdecl;
  LHandle: THandle;

function WinePath(const aPathIn: String): String;
var
  VSz: Cardinal;
  VNameA: AnsiString;
  VNameW: TWineUnicodeString;
  VUnixNameRet: TWineAnsiString;
  VStatus: Cardinal;
  aPath: String;
  newVersion: Boolean;
begin
  if not assigned(wine_nt_to_unix_file_name) then
  begin
    Result := 'n/a';
    exit;
  end;
  aPath := '\??\' + aPathIn;
  Result := '?';
  newVersion := false;
  VNameW.Len := Length(aPath) * SizeOf(WideChar);
  VNameW.MaxLen := VNameW.Len;
  VNameW.Buffer := PWideChar(aPath);
  VUnixNameRet.Len := 0;
  VUnixNameRet.MaxLen := 0;
  VUnixNameRet.Buffer := nil;
  VStatus := wine_nt_to_unix_file_name(@VNameW, @VUnixNameRet, 0);
  if VStatus <> 0 then
  begin
    VSz := 255;
    SetLength(VNameA, VSz);
    ZeroMemory(Pointer(VNameA), VSz);
    VStatus := wine_nt_to_unix_file_name_1(@VNameW, Pointer(VNameA), @VSz, 0);
    newVersion := true;
  end;
  if VStatus <> 0 then
  begin
    Result := 'Error ' + IntToStr(Status);
    exit;
  end;
  if not newVersion then
  begin
    VSz := VUnixNameRet.Len;
    SetString(VNameA, VUnixNameRet.Buffer, VSz);
    // ToDo: RtlFreeAnsiString(@VUnixNameRet)
  end
  else
    SetLength(VNameA, VSz);
  Result := StringReplace(VNameA, '/dosdevices/c:/', '/drive_c/', [rfIgnoreCase]);
end;

MyBuffer 尝试这种类型:

type
  TWineString = packed record
    Len    : Word;
    MaxLen : Word;
    Buffer : PAnsiChar;
  end;

您也不能将 PChar 作为输入字符串传递,因为它不是 wine 中定义的 UNICODE_STRING:

typedef struct _UNICODE_STRING {
  USHORT Length;        /* bytes */
  USHORT MaximumLength; /* bytes */
  PWSTR  Buffer;
} UNICODE_STRING, *PUNICODE_STRING;

你应该使用这个等价物:

type
  TWineUnicodeString = packed record
    Len    : Word;
    MaxLen : Word;
    Buffer : PWideChar;
  end;

Update:此函数 changed 是 API 6 个月前的,因此根据 wine 版本,您应该使用以下两种方式之一:定义 USE_WINE_STABLE 如果您使用的是稳定的 wine v5.0,或者如果您使用更新的版本则取消定义它:

program WineTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Winapi.Windows,
  System.SysUtils;

{$DEFINE USE_WINE_STABLE}

type
  {$IFDEF USE_WINE_STABLE}
  TWineAnsiString = packed record
    Len    : Word;
    MaxLen : Word;
    Buffer : PAnsiChar;
  end;
  PWineAnsiString = ^TWineAnsiString;
  {$ENDIF}

  TWineUnicodeString = packed record
    Len    : Word;
    MaxLen : Word;
    Buffer : PWideChar;
  end;
  PWineUnicodeString = ^TWineUnicodeString;

var
  wine_get_version: function: PAnsiChar; cdecl;

  {$IFDEF USE_WINE_STABLE}
  wine_nt_to_unix_file_name: function(const nameW: PWineUnicodeString;
    unix_name_ret: PWineAnsiString; disposition: Cardinal): Cardinal; cdecl;
  {$ELSE}
  wine_nt_to_unix_file_name: function(const nameW: PWineUnicodeString;
    nameA: PAnsiChar; Sz: PCardinal; disposition: Cardinal): Cardinal; cdecl;
  {$ENDIF}

procedure TestWinePath(const APath: string);
var
  VSz: Cardinal;
  VNameA: AnsiString;
  VNameW: TWineUnicodeString;
  {$IFDEF USE_WINE_STABLE}
  VUnixNameRet: TWineAnsiString;
  {$ENDIF}
  VStatus: Cardinal;
begin
  VNameW.Len := Length(APath) * SizeOf(WideChar);
  VNameW.MaxLen := VNameW.Len;
  VNameW.Buffer := PWideChar(APath);

  {$IFDEF USE_WINE_STABLE}
  VUnixNameRet.Len := 0;
  VUnixNameRet.MaxLen := 0;
  VUnixNameRet.Buffer := nil;

  VStatus := wine_nt_to_unix_file_name(@VNameW, @VUnixNameRet, 0);
  {$ELSE}
  VSz := 255;
  SetLength(VNameA, VSz);
  ZeroMemory(Pointer(VNameA), VSz);

  VStatus := wine_nt_to_unix_file_name(@VNameW, Pointer(VNameA), @VSz, 0);
  {$ENDIF}

  Writeln('wine_nt_to_unix_file_name:');
  Writeln('status = 0x', IntToHex(VStatus, 8));

  if VStatus <> 0 then begin
    Exit;
  end;

  {$IFDEF USE_WINE_STABLE}
  VSz := VUnixNameRet.Len;
  SetString(VNameA, VUnixNameRet.Buffer, VSz);
  // ToDo: RtlFreeAnsiString(@VUnixNameRet)
  {$ELSE}
  SetLength(VNameA, VSz);
  {$ENDIF}

  Writeln('unix len = ', VSz);
  Writeln('unix: ', VNameA);
  Writeln('nt: ', APath);
end;

function LoadProc(const AHandle: THandle; const AName: string): Pointer;
begin
  Result := GetProcAddress(AHandle, PChar(AName));
  if Result = nil then begin
    raise Exception.CreateFmt('Can''t load function: "%s"', [AName]);
  end;
end;

var
  LHandle: THandle;
  LNtFileName: string;
begin
  try
    LNtFileName := ParamStr(1);
    if LNtFileName = '' then begin
      Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' NtFileName');
      Exit;
    end;

    LHandle := LoadLibrary('ntdll.dll');
    if LHandle > 32 then begin
      wine_get_version := LoadProc(LHandle, 'wine_get_version');
      Writeln('wine version = ', wine_get_version() );

      wine_nt_to_unix_file_name := LoadProc(LHandle, 'wine_nt_to_unix_file_name');
      TestWinePath(LNtFileName);
    end;
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
    end;
  end;
end.

输出(在 Ubuntu 20.04 上测试):

$ wine WineTest.exe "\??\c:\windows\notepad.exe"
wine version = 5.0
wine_nt_to_unix_file_name:
status = 0x00000000
unix len = 49
unix: /home/zed/.wine/dosdevices/c:/windows/notepad.exe
nt: \??\c:\windows\notepad.exe