gpt4 book ai didi

delphi - 获取程序可执行文件的名称(如 paramstr(0) 中),但在 Delphi 7 中将其作为 Unicode 字符串获取?

转载 作者:行者123 更新时间:2023-12-01 19:44:04 27 4
gpt4 key购买 nike

我想将文件从自定位复制到另一个位置,如下所示:

var
NewFile : WideString;
MyOwnLocation : WideString;
begin
NewFile := 'C:\mycopy.exe';
// CopyFileW (PWideChar(paramstr(0)), PWideChar(NewFile), false); // ===> doesn't work
MyOwnLocation := paramstr(0);
CopyFileW (PWideChar(MyOwnLocation), PWideChar(NewFile), false); // ===> works but not sure if Unicode supported...
end;

当我将 paramstr(0) 复制到 WideString 时它可以工作,但我仍然不确定 paramstr(0) 是否已经是 UNICODE。是否有 WindowsAPI 以 WideString 形式返回我的文件的当前位置?

感谢您的帮助:)

最佳答案

当然。您可以使用 GetModuleFileNameW,这是 ParamStr(0) 内部使用的 api 的 unicode 版本:

var
NewFile: WideString;
MyOwnLocation: WideString;
Len: DWORD;
begin
NewFile := 'C:\mycopy.exe';
SetLength(MyOwnLocation, 260);
Len := GetModuleFileNameW(0, PWideChar(MyOwnLocation), Length(MyOwnLocation));
Win32Check(Bool(Len));
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then begin
SetLength(MyOwnLocation, Len);
CopyFileW (PWideChar(MyOwnLocation), PWideChar(NewFile), false);
end else
// handle fail due to insufficient buffer

关于delphi - 获取程序可执行文件的名称(如 paramstr(0) 中),但在 Delphi 7 中将其作为 Unicode 字符串获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10763243/

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