2012-07-03 45 views
1

由於我已經從delphi 5升級到XE,我正努力使用前面編譯的特定Dll。我的堵點似乎與爲Unicode/ANSI字符,但我還沒有找到如何解決這個問題使用delphiXE的pchar參數調用delphi 2006 dll

下面是過程的例子:

procedure GetFilename(Buffer: PChar; BufSize: Integer); stdcall; 

在我的代碼我打電話這一點,方式

implementation  
procedure GetFilename; external 'myDll.dll' name 'GetFilename'; 
procedure myproc 
var 
    buffer : Array [0..255] of Char; 
begin 
    GetFilename(buffer, length(buffer)-1); 
    Showmessage(buffer); //This gives me chinese character 
end; 

緩衝區包含此:

byte((@buffer[0])^); // 67 which is the ASCII for C 
byte((@buffer[1])^); // 92 which is the ASCII for \ 

什麼我期待正常是海峽ing「C:\」

有沒有人遇到同樣的問題?

回答

5

因爲DLL使用德爾福的非Unicode版本,讓你必須更改

procedure GetFilename(Buffer: PChar; BufSize: Integer); stdcall; 

procedure GetFilename(Buffer: PAnsiChar; BufSize: Integer); stdcall; 

的聲明,並從

buffer : Array [0..255] of Char; 

緩衝變量到

buffer : Array [0..255] of AnsiChar; 

此外,要了解Delphi Unicode支持,請查看MarcoCantú的Delphi and Unicode白皮書。

+1

無需在實現部分中更改'GetFilename'的'external'聲明。只有接口部分的聲明必須從'PChar'更改爲'PAnsiChar'。 –