我自己解决了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中用其他程序调用的时候就会出问题,这是为什么啊
那咬看你DLL是如何调用了得??如果你传递了字符串,那么要uses sharemem
我是这么写的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';
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中用其他程序调用的时候就会出问题,这是为什么啊
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';
procedure saveastxt(f2:string);stdcall;external 'wstr.dll';斑竹帮我结束吧。