怎样用程序将一个unicode big endian格式的文件转换为ansi格式?

zzh26 2006-08-15 11:17:35
如题
...全文
334 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lrq_123 2006-09-08
  • 打赏
  • 举报
回复
没看完,回去继续看
zzh26 2006-08-16
  • 打赏
  • 举报
回复
好吧,还是我自己解决了,调用的时候少写了个stdcall;

调用这么写就可以了

procedure saveaswstr(f1:string);stdcall;external 'wstr.dll';

procedure saveastxt(f2:string);stdcall;external 'wstr.dll';

斑竹帮我结束吧。
zzh26 2006-08-15
  • 打赏
  • 举报
回复
自己顶一下
wudi_1982 2006-08-15
  • 打赏
  • 举报
回复
做个记号,晚上回来看看。
zzh26 2006-08-15
  • 打赏
  • 举报
回复
我是这么写的

library wstr;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
ShareMem,
SysUtils,
Classes;


{$R *.res}


procedure saveaswstr(f1:string);stdcall; //将文件f1转换为unicode编码
var
t1:TStringList; FileStream: TFileStream;
P: pchar;
W: WideString;
begin
t1:=TStringList.Create;
t1.LoadFromFile(f1);

w := widestring(t1.Text);
p := pchar(w);
FileStream := TFileStream.Create(f1, fmCreate);
FileStream.Position := 0;
FileStream.Write(l, 2);
FileStream.Write(p^, Length(w) * 2); //Length(w)<>Length(t1.Text) ;
FileStream.Free;

t1.Clear;
t1.Free;


end;



procedure WordLoHiExchange(var w:Word);
var
b:Byte;
begin
b:=WordRec(w).Lo;
WordRec(w).Lo:=WordRec(w).Hi;
WordRec(w).Hi:=b;
end;

procedure saveastxt(f2:string);stdcall; //将unicode big endian转换为ansi格式存储
var
buffer: WideChar;
f: file of WideChar;
str: string;
w:word;
t1:TstringList;
begin
t1:=TstringList.Create;

assignfile(f, f2);
reset(f);
read(f, buffer); // 跳过unicode开头的标记
while not eof(f) do
begin
read(f, buffer);
w:=word(buffer);
WordLoHiExchange(w);
buffer:=widechar(w);

str:= str+buffer;
end;
closefile(f);

t1.Text:=str;
t1.SaveToFile(f2);
t1.Clear;
t1.Free;

end;


exports
saveaswstr,
saveastxt;

begin
end.


仍然会出错
调用是这么写的

procedure saveaswstr(f1:string);external 'wstr.dll';

procedure saveastxt(f2:string);external 'wstr.dll';
wudi_1982 2006-08-15
  • 打赏
  • 举报
回复
那咬看你DLL是如何调用了得??如果你传递了字符串,那么要uses sharemem
zzh26 2006-08-15
  • 打赏
  • 举报
回复
我自己解决了

procedure WordLoHiExchange(var w:Word);
var
b:Byte;
begin
b:=WordRec(w).Lo;
WordRec(w).Lo:=WordRec(w).Hi;
WordRec(w).Hi:=b;
end;

procedure saveastxt(f2:string);stdcall; //将unicode big endian转换为ansi格式存储
var
buffer: WideChar;
f: file of WideChar;
str: string;
w:word;
t1:TstringList;
begin
t1:=TstringList.Create;

assignfile(f, f2);
reset(f);
read(f, buffer); // 跳过unicode开头的标记

while not eof(f) do
begin
read(f, buffer);
w:=word(buffer);
WordLoHiExchange(w);
buffer:=widechar(w);
str:= str+buffer;
end;
closefile(f);

t1.Text:=str;
t1.SaveToFile(f2);
t1.Clear;
t1.Free;

end;

但是在应用程序中执行没有问题,写到dll中用其他程序调用的时候就会出问题,这是为什么啊

828

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧