用DLL来处理文件问题?(100)

  • 主题发起人 主题发起人 Eroswar
  • 开始时间 开始时间
E

Eroswar

Unregistered / Unconfirmed
GUEST, unregistred user!
library dell;uses SysUtils, Classes, ShareMem, ShellAPI, Windows;{$R *.res}Function dellxp(FileName: string):string;stdcall;var max,n: LongInt; i: integer; fs: TFileStream; buffer: array[0..4095] of char;begin for i:=Low(buffer) to High(buffer) do buffer:=#0; fs:=TFileStream.Create(FileName,fmOpenReadWrite or fmShareExclusive); try n:=4096; max:=fs.Size; fs.Position:=0; while max>0 do begin if max<n then n:=max; fs.Write(buffer,n); max:=max-n; end; FlushFileBuffers(fs.Handle); finally fs.Free; end; Deletefile(FileName); //提示错误信息:[Error] dell.dpr(35): Incompatible types: 'String' and 'PAnsiChar'end;exports dellxp;beginend.--------------------------------------------------------------------------[Error] dell.dpr(35): Incompatible types: 'String' and 'PAnsiChar'怎么样解决???--------------------------------------------------------------------------Function dellxp(FileName: string):string;stdcall;external 'Dell.dll';procedure TForm1.Button1Click(Sender: TObject);begin//比如我想让程序运行后调用Dell.DLL里的dellxp函数子程来删除指定的某文件如d:/abc/a/atx.txt里的atx.txt文件或者如下的路径varTempDir: array[0..MAX_PATH] of Char;//定义临时目录----------------------------------------------------GetSystemDirectory(TempDir, MAX_PATH);DirTemp := StrPas(TempDir)+'/'+'Avixg';DirTemp路径下的atx.txt文件?FileName:= Edit1.Text ???怎么样把文件名和路径传递给dellxp用来删除它·~如上的DLL代码不知有没有错误?请朋友们帮我看看~最好给个源码,帮我改改end;
 
Deletefile(PChar(FileName));
 
改为 DeleteFile(PChar(FileName)); 试试。建议你的dll里的函数定义为:function dellxp(FileName: PChar):Boolean;stdcall;如果这样就可以直接写:Result := DeleteFile(FileName);调用时要注意传入PChar型的文件名,祝你好运。
 
运行后提示出如错误:应用程序或 DLL C:/TDDownload/ADS/dellxCG.dll 为无效的Windows映像。请再检测一遍您的安装盘。----------------------------------------到底怎么弄啊?怎么样才能调用啊!晕·~
 
to Eroswar 试一下。我测试正常!library dell;uses SysUtils, Classes, ShellAPI, Windows;{$R *.res}Function dellxp(FileName: PChar):boolean;stdcall;var max,n: LongInt; i: integer; fs: TFileStream; buffer: array[0..4095] of char;begin for i:=Low(buffer) to High(buffer) do buffer:=#0; fs:=TFileStream.Create(FileName,fmOpenReadWrite or fmShareExclusive); try n:=4096; max:=fs.Size; fs.Position:=0; while max>0 do begin if max<n then n:=max; fs.Write(buffer,n); max:=max-n; end; FlushFileBuffers(fs.Handle); finally fs.Free; end; Deletefile(FileName); //提示错误信息:[Error] dell.dpr(35): Incompatible types: 'String' and 'PAnsiChar' result:=True;end;exports dellxp;beginend.调用。在D2007中编译通过!unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}function dellxp(aFile:PChar):Boolean;stdcall;external 'dellCx.dll';procedure TForm1.Button1Click(Sender: TObject);begin dellxp('c:/SB1_ForkVerify.SCL');end;end.
 
谢谢·~其实是我把DLL生成方式搞错了·~呵呵
 

Similar threads

后退
顶部