本文介绍了获取程序可执行文件的名称(如paramstr(0)一样),但在Delphi 7中将其作为Unicode字符串获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件从自身位置复制到这样的另一个位置:
I want to copy a file from the selflocation to another location like that:
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。
it works when I copy paramstr(0) to a WideString, but I'm still not sure if paramstr(0) is already UNICODE. Is there maybe a WindowsAPI that returns the current location of my file in a wideString?
感谢帮助:)
推荐答案
可以。您可以使用 GetModuleFileNameW
, ParamStr(0)
内部使用的api的Unicode版本:
Sure. You can use GetModuleFileNameW
, the unicode version of the api that ParamStr(0)
internally use:
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
这篇关于获取程序可执行文件的名称(如paramstr(0)一样),但在Delphi 7中将其作为Unicode字符串获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!