MichaSchumann
MichaSchumann

Reputation: 1483

Delphi mapping for Wine function wine_nt_to_unix_file_name

How can I correctly call wine_nt_to_unix_file_name from WINE's ntdll.dll in Delphi (10.4)?

In the web I found the definition to be like this:

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

Disposition changes the return result for non existent last path part and check_case is self explanatory.

I would like to use this function to display real unix paths of my application to the user when running in WINE. This should make it more easy for a medium user to find a folder to share data between native apps and the WINE environment.

What I tried:

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;

Retrieving the WINE version works great but I cannot get the path conversion up and running as I don't know how to handle the returned Pointer to ANSI_STRING what seems to be a Windows structure like this:

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

I tried to approach the problem this way:

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

But the function is returning total garbage in the buffer when output byte by byte.

Update Following the hint to the current Wine source and the hint with the structure I tried this version, unfortunately delivering garbage. The first parameter is a UNICODE STRING structure, the second a simple ansistring. The third parameter receives the length of the returned buffer.

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;

Based on Zeds great answer i created this function that automatically tries the new API call if the old one fails

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;

Upvotes: 4

Views: 551

Answers (1)

zed
zed

Reputation: 877

Try this type for MyBuffer:

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

Also you can't pass PChar as input string because it isn't a UNICODE_STRING as defined in wine:

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

You should use this equivalent:

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

Update: This function has changed its API 6 months ago, so depending on wine version you should use one of two ways: define USE_WINE_STABLE if you are on stable wine v5.0 or undefine it if you use newer version:

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.

Output (tested on 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

Upvotes: 4

Related Questions