Delphi 2009/2010和Windows API调用的Unicode问题 [英] Unicode problems with Delphi 2009 / 2010 and windows API calls

查看:239
本文介绍了Delphi 2009/2010和Windows API调用的Unicode问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Delphi 2006中使用这个功能,但现在用D2010它会抛出一个错误。
我认为这与切换到Unicode有关。

 功能TWinUtils.GetTempFile(Const扩展名:STRING):STRING; 
var
缓冲区:ARRAY [0 .. MAX_PATH] OF char;
开始
重复
GetTempPath(SizeOf(Buffer) - 1,Buffer);
GetTempFileName(Buffer,'~~',0,Buffer);
结果:= ChangeFileExt(Buffer,Extension);
直到不是FileExists(Result);
结束;

我应该怎么做才能使它工作?



编辑



当调用ChangeFileExt时,我得到一个访问冲突

解决方案

Windows.Pas

  function GetTempFileName(lpPathName,lpPrefixString:PWideChar; 
uUnique:UINT; lpTempFileName:PWideChar):UINT;标准

函数GetTempPath(nBufferLength:DWORD; lpBuffer:PWideChar):DWORD;标准

SysUtils.Pas

  function ChangeFileExt(const FileName,Extension:string):string; 

尝试这个

 code>函数TWinUtils.GetTempFile(Const扩展名:STRING):STRING; 
Var
缓冲区:ARRAY [0 .. MAX_PATH] OF WideChar;
开始
重复
GetTempPath(Length(Buffer),Buffer);
GetTempFileName(Buffer,'~~',0,Buffer);
结果:= ChangeFileExt(Buffer,Extension);
直到不是FileExists(Result);
结束;

或这个

 code>函数GetTempFile(Const Extension:String):String; 
Var
缓冲区:String;
开始
SetLength(Buffer,MAX_PATH);
重复
GetTempPath(MAX_PATH,PChar(缓冲区));
GetTempFileName(PChar(缓冲区),'~~',0,PChar(缓冲区));
结果:= ChangeFileExt(Buffer,Extension);
直到不是FileExists(Result);
结束;

对于Delphi,Char和PChar类型分别是WideChar和PWideChar类型。



如果您使用将数据返回到char缓冲区中的任何Windows API,那么这些缓冲区需要重新声明为字节数组或AnsiChar数组。



如果您正在调用这些Windows API并在缓冲区中发送,那么当告诉API您的缓冲区多长时间时,如果使用了sizeof函数。那些调用需要更改为Length函数,因为Windows widechar API需要字符数,而不是字节数。



再见。


Hi I have been using this function in Delphi 2006, but now with D2010 it throws an error. I think it is related to the switch to Unicode.

  Function TWinUtils.GetTempFile(Const Extension: STRING): STRING;
  Var
     Buffer: ARRAY [0 .. MAX_PATH] OF char;
  Begin
    Repeat
      GetTempPath(SizeOf(Buffer) - 1, Buffer);
      GetTempFileName(Buffer, '~~', 0, Buffer);
      Result := ChangeFileExt(Buffer, Extension);
    Until not FileExists(Result);
  End;

What should I do to make it work?

EDIT

I get an 'access violation' when the ChangeFileExt is called

解决方案

Windows.Pas

function GetTempFileName(lpPathName, lpPrefixString: PWideChar;
  uUnique: UINT; lpTempFileName: PWideChar): UINT; stdcall;

function GetTempPath(nBufferLength: DWORD; lpBuffer: PWideChar): DWORD; stdcall;

SysUtils.Pas

function ChangeFileExt(const FileName, Extension: string): string;

Try this

  Function TWinUtils.GetTempFile(Const Extension: STRING): STRING;
  Var
     Buffer: ARRAY [0 .. MAX_PATH] OF WideChar;
  Begin
    Repeat
      GetTempPath(Length(Buffer), Buffer);
      GetTempFileName(Buffer, '~~', 0, Buffer);
      Result := ChangeFileExt(Buffer, Extension);
    Until not FileExists(Result);
  End;

or this

  Function GetTempFile(Const Extension: String): String;
  Var
     Buffer: String;
  Begin
      SetLength(Buffer,MAX_PATH);
    Repeat
      GetTempPath( MAX_PATH, PChar( Buffer) );
      GetTempFileName(PChar( Buffer), '~~', 0, PChar( Buffer));
      Result := ChangeFileExt(Buffer, Extension);
    Until not FileExists(Result);
  End;

For Delphi, Char and PChar types are WideChar and PWideChar types, respectively.

If you use any Windows API’s that return data into char buffers , those buffers need to be redeclared as arrays of bytes or an array of AnsiChar.

If you are calling these Windows API’s and sending in buffers, if have been using the sizeof function when telling the API how long your buffer is. Those calls need to be changed to the Length function, as the Windows widechar API’s require the number of characters, not the number of bytes.

Bye.

这篇关于Delphi 2009/2010和Windows API调用的Unicode问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆