关于GetSystemDirectory的问题,100分相送!(100分)

  • 主题发起人 主题发起人 yan_yi_fei
  • 开始时间 开始时间
Y

yan_yi_fei

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用GetSystemDirectory(sysdir,128)得出系统目录后,<br>然后copyfile('a.txt',sysdir+'a.txt',false)<br>出现 [Warning] &nbsp;Variable 'sysdir' might not have been initialized<br>程序运行时,文件复制成功但出现“程序出现非法操作,请关闭“,请问各位朋友,为何?
 
"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long<br>LpBuffer :String,用于装载系统目录路径名的一个字串缓冲区。<br>它应事先初始化成nSize+1个字符的长度。<br>通常至少要为这个缓冲区分配MAX_PATH个字符的长度
 
var<br>Buf: array[0..MAX_PATH] of char;<br><br><br>GetSystemDirectory(Buf, MAX_PATH);
 
来晚了,补充一句吧:),string是不需要显示的分配内存的,因为是Delphi自动分配和释放的,WINAPI之类的<br>冬冬很多需要一个缓冲区,这需要你去分配的,不管是动态或静态<br>
 
我最烦的就是声明PChar,动态声明要 GetMem、FreeMem什么的,静态声明又要使用数组。<br><br>这样吧<br>声明一个String变量sysdir<br><br>var<br>&nbsp; sysstr: String<br>begin<br>&nbsp; setLength(sysdir, 255) ;<br>&nbsp; GetSystemDirectory(PChar(sysdir),128) ;<br>&nbsp; setLength(sysstr, strLen(PChar(sysstr))) ;<br>&nbsp; ...<br>end;<br><br>
 
不是一样吗,不用的时侯你还是要setlength(sysdir,0);<br>又不简单什么
 
String的内存由Delphi自动管理,不需要setLength为 0 :)<br><br>另外,这样的好处是 String 可以动态修改长度,容易实现变量重复使用。
 
试试这段代码:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var s:array[0..80]of char;<br>begin<br>&nbsp;GetWindowsDirectory(s,SizeOf(s));<br>&nbsp;label1.Caption:=s;<br>end;<br><br>书上的,现学现卖,不好意思:-)
 
GetSystemDirectory一样。
 
GetSystemDirectory<br>The GetSystemDirectory function retrieves the path of the system directory. The system directory contains such files as dynamic-link libraries, drivers, and font files. <br><br>UINT GetSystemDirectory(<br>&nbsp; LPTSTR lpBuffer, &nbsp;// buffer for system directory<br>&nbsp; UINT uSize &nbsp; &nbsp; &nbsp; &nbsp;// size of directory buffer<br>);<br>Parameters<br>lpBuffer <br>[out] Pointer to the buffer to receive the null-terminated string containing the path. This path does not end with a backslash unless the system directory is the root directory. For example, if the system directory is named WINDOWS/SYSTEM on drive C, the path of the system directory retrieved by this function is C:/WINDOWS/SYSTEM. <br>uSize <br>[in] Specifies the maximum size of the buffer, in TCHARs. This value should be set to at least MAX_PATH. <br>Return Values<br>If the function succeeds, the return value is the length, in TCHARs, of the string copied to the buffer, not including the terminating null character. If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path. <br><br>If the function fails, the return value is zero. To get extended error information, call GetLastError. <br><br>Remarks<br>Applications should not create files in the system directory. If the user is running a shared version of the operating system, the application does not have write access to the system directory. Applications should create files only in the directory returned by the GetWindowsDirectory function. <br><br>Requirements <br>&nbsp; Windows NT/2000: Requires Windows NT 3.1 or later.<br>&nbsp; Windows 95/98: Requires Windows 95 or later.<br>&nbsp; Header: Declared in Winbase.h; include Windows.h.<br>&nbsp; Library: Use Kernel32.lib.<br>&nbsp; Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.<br>
 
你这里的<br>sysdir需要是PCHAR型的<br>应该是这样:<br>
代码:
<br>var<br>&nbsp; sysdir:pchar;<br>begin<br>&nbsp; getmem(sysdir,100);<br>&nbsp; try<br>&nbsp; &nbsp; getsystemdirectory(sysdir,100);<br>&nbsp; &nbsp; .....<br>&nbsp; finally<br>&nbsp; &nbsp; freemem(sysdir,100);<br>&nbsp; end;<br>end;<br>
<br>&nbsp;
 
真是谢谢大家,分数太少了,只有抱歉了!
 
咦?iamfly的代码的字体是怎么高出来的?
 
procedure TForm1.Button1Click(Sender: TObject);<br>var s:array[0..MAX_PATH]of char;<br>begin<br>&nbsp;GetWindowsDirectory(s,SizeOf(s));<br>&nbsp;copyfile('a.txt',strPas(sysdir)+'/a.txt',false);<br><br>end
 
<br>哈哈哈!!!<br>正确的代码来也!<br><br>&nbsp;Var<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SysDir &nbsp;: PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PArr &nbsp; : Packed Array [0..MAX_PATH] Of Char;<br>begin<br>&nbsp; &nbsp; SysDir:=@_PArr;<br>&nbsp; &nbsp; GetSystemDirectory(SysDir,128);<br>&nbsp; &nbsp; Windows.CopyFile(<br>&nbsp; &nbsp; &nbsp; PChar('c:/a.txt'),<br>&nbsp; &nbsp; &nbsp; PChar(String(SysDir)+'/'+ // 注意 String 和 '/'<br>&nbsp; &nbsp; &nbsp; 'a.txt'),<br>&nbsp; &nbsp; &nbsp; Bool(false));<br>end;<br><br>看来今天又可以赚$100,好开心!<br>
 
var Names: Pchar;<br>Names := #0;<br>try<br>GetMem( Names, MAX_PATH+1 );<br>GetSystemDirectory(Names, MAX_PATH+1);<br>copyfile('a.txt',Names+'/a.txt',false)<br>finally<br>FreeMem( Names );<br>end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部