English 中文(简体)
获取程序可执行文件的名称( 如 paramstr( 0 ) ), 但能否在 Delphi 7 中将其作为 Unicode 字符串?
原标题:Get the name of the program executable (as in paramstr(0)) but get it as a Unicode string in Delphi 7?

我想复制一个文件 从自我定位到另一个地点 类似:

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;

当我把Pramstr(0)复制到宽链条时,它有效,但我仍然不确定paramstr(0)是否已经是UNICODE。也许有一个 WindowsAPI 返回我文件在宽链条中的当前位置吗?

感谢帮助:)

最佳回答

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

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) 直接调用 CopyFileW ParamStr(0) 不起作用,因为在德尔菲7中 ParamStr(0) 返回 AnsiString ( string 的默认类型),因此它不符合预期的第一个参数类型( PWideChar )。

唯一的方法就是使用您的方式 - 将 ParamStr(0) 的内容指派给 WideString 变量,然后将它用作 CopyFileW 的参数。

在使用非统一代码 API ( CopyFileA , 由德尔斐 7 s Windows 单位地图 CopyFile ) 来使用非统一代码 API ( CopyFileA ) 时, Windows 以透明方式在 Unicode 和 ANSI 之间进行大多数转换,而无需你任何努力,所以你应该代之使用它。





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签