gpt4 book ai didi

Wine 函数 wine_nt_to_unix_file_name 的 Delphi 映射

转载 作者:行者123 更新时间:2023-12-03 23:01:19 39 4
gpt4 key购买 nike

如何在 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)
处置更改不存在的最后一个路径部分的返回结果,并且 check_case 是不言自明的。
我想使用这个函数在 WINE 中运行时向用户显示我的应用程序的真实 unix 路径。这应该会让中等用户更容易找到一个文件夹来在 native 应用程序和 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 的指针,这似乎是这样的 Windows 结构:
typedef struct _STRING {
USHORT Length;
USHORT MaximumLength;
PCHAR Buffer;
} STRING;
我试图以这种方式解决问题:
MyBuffer: array [0 .. 2048] of AnsiChar;
LWineNTToUnixFileName(PChar(aWinPath), @MyBuffer, 0, true);
但是该函数在逐字节输出时返回缓冲区中的总垃圾。
更新
按照对当前 Wine 源代码的提示和结构的提示,我尝试了这个版本,不幸的是提供了垃圾。第一个参数是一个 UNICODE STRING 结构,第二个参数是一个简单的 ansistring。第三个参数接收返回缓冲区的长度。
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 调用失败,它会自动尝试新的 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作为输入字符串,因为它不是 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;

更新 : 这个函数有 changed它的 API 是 6 个月前的,因此根据葡萄酒版本,您应该使用以下两种方式之一:定义 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

关于Wine 函数 wine_nt_to_unix_file_name 的 Delphi 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65549969/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com