请帮我看看这个函数,可以编译,但运行完会报错。高手解决300分送 ( 积分: 300 )

  • 主题发起人 主题发起人 huguozhi
  • 开始时间 开始时间
H

huguozhi

Unregistered / Unconfirmed
GUEST, unregistred user!
function FilediskMount(DeviceNumber: String; DriveLetter: String; FileName: PChar; CdImage: Boolean): Word;<br>var<br> &nbsp;Device: THandle;<br> &nbsp;DeviceName: String;<br> &nbsp;VolumeName: String;<br> &nbsp;BytesReturned: DWord;<br> &nbsp;OpenFileInformation: OPEN_FILE_INFORMATION;<br> &nbsp;IOCTL_FILE_DISK_OPEN_FILE: DWord;<br>begin<br> &nbsp;VolumeName := '//./'+DriveLetter;<br><br> &nbsp;Device := CreateFile(<br> &nbsp; &nbsp; Pchar(VolumeName),<br> &nbsp; &nbsp; GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; FILE_SHARE_READ or FILE_SHARE_WRITE,<br> &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; OPEN_EXISTING,<br> &nbsp; &nbsp; FILE_FLAG_NO_BUFFERING,<br> &nbsp; &nbsp; 0<br> &nbsp; &nbsp; );<br> &nbsp;if Device &lt;&gt; INVALID_HANDLE_VALUE then<br> &nbsp;begin<br> &nbsp; &nbsp;messagebox(0,'错误1!','出错',48);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br><br> &nbsp;if CDImage then<br> &nbsp; &nbsp; DeviceName := DEVICE_DIR_NAME+DEVICE_BASE_NAME+DEVICE_BASE_NAME+'Cd'+DeviceNumber<br> &nbsp;else<br> &nbsp; &nbsp; DeviceName := DEVICE_DIR_NAME+DEVICE_BASE_NAME+DEVICE_BASE_NAME+DeviceNumber;<br><br><br> &nbsp;if not (DefineDosDevice(DDD_RAW_TARGET_PATH, Pchar(DriveLetter), Pchar(DeviceName))) then<br> &nbsp;begin<br> &nbsp; &nbsp;messagebox(0,'盘符已在使用!','出错',48);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;Device := CreateFile(<br> &nbsp; &nbsp; &nbsp; &nbsp;Pchar(VolumeName),<br> &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp;nil,<br> &nbsp; &nbsp; &nbsp; &nbsp;OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_FLAG_NO_BUFFERING,<br> &nbsp; &nbsp; &nbsp; &nbsp;0<br> &nbsp; &nbsp; &nbsp; &nbsp;);<br> &nbsp;if Device = INVALID_HANDLE_VALUE then<br> &nbsp;begin<br> &nbsp; &nbsp;DefineDosDevice(DDD_REMOVE_DEFINITION, Pchar(DriveLetter), nil);<br> &nbsp; &nbsp;messagebox(0,'错误2!','出错',48);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> &nbsp;//Allocmem(Sizeof(OpenFileInformation)+StrLen(FileName)+4);<br> &nbsp;Zeromemory(@OpenFileInformation,Sizeof(OPEN_FILE_INFORMATION)+Length(FileName) + 4);<br> &nbsp;StrPCopy(OpenFileInformation.FileName,'/??/'+FileName);<br> &nbsp;OpenFileInformation.FileNameLength := StrLen(OpenFileInformation.FileName);<br> &nbsp;OpenFileInformation.Read_Only := False;<br><br> &nbsp;IOCTL_FILE_DISK_OPEN_FILE := CTL_CODE(FILE_DEVICE_FILE_DISK, $800, METHOD_BUFFERED, FILE_READ_ACCESS or FILE_WRITE_ACCESS);<br> &nbsp;if not (DeviceIoControl(<br> &nbsp; &nbsp; Device,<br> &nbsp; &nbsp; IOCTL_FILE_DISK_OPEN_FILE,<br> &nbsp; &nbsp; @OpenFileInformation,<br> &nbsp; &nbsp; Sizeof(OpenFileInformation)+ OpenFileInformation.FileNameLength - 1,<br> &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; BytesReturned,<br> &nbsp; &nbsp; nil<br> &nbsp; &nbsp; ))<br> &nbsp;then<br> &nbsp;begin<br> &nbsp; &nbsp;DefineDosDevice(DDD_REMOVE_DEFINITION, Pchar(DriveLetter), nil);<br> &nbsp; &nbsp;messagebox(0,'错误3!','出错',48);<br> &nbsp; &nbsp;Exit<br> &nbsp;end;<br> &nbsp;//ReallocMem();<br> &nbsp;CloseHandle(Device);<br><br>end;<br><br>以上函数可以编译通过,但执行会报错误,好像是指针没有释放。请高手解决。
 
function FilediskMount(DeviceNumber: String; DriveLetter: String; FileName: PChar; CdImage: Boolean): Word;<br>var<br> &nbsp;Device: THandle;<br> &nbsp;DeviceName: String;<br> &nbsp;VolumeName: String;<br> &nbsp;BytesReturned: DWord;<br> &nbsp;OpenFileInformation: OPEN_FILE_INFORMATION;<br> &nbsp;IOCTL_FILE_DISK_OPEN_FILE: DWord;<br>begin<br> &nbsp;VolumeName := '//./'+DriveLetter;<br><br> &nbsp;Device := CreateFile(<br> &nbsp; &nbsp; Pchar(VolumeName),<br> &nbsp; &nbsp; GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; FILE_SHARE_READ or FILE_SHARE_WRITE,<br> &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; OPEN_EXISTING,<br> &nbsp; &nbsp; FILE_FLAG_NO_BUFFERING,<br> &nbsp; &nbsp; 0<br> &nbsp; &nbsp; );<br> &nbsp;if Device &lt;&gt; INVALID_HANDLE_VALUE then<br> &nbsp;begin<br> &nbsp; &nbsp;messagebox(0,'错误1!','出错',48);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br><br> &nbsp;if CDImage then<br> &nbsp; &nbsp; DeviceName := DEVICE_DIR_NAME+DEVICE_BASE_NAME+DEVICE_BASE_NAME+'Cd'+DeviceNumber<br> &nbsp;else<br> &nbsp; &nbsp; DeviceName := DEVICE_DIR_NAME+DEVICE_BASE_NAME+DEVICE_BASE_NAME+DeviceNumber;<br><br><br> &nbsp;if not (DefineDosDevice(DDD_RAW_TARGET_PATH, Pchar(DriveLetter), Pchar(DeviceName))) then<br> &nbsp;begin<br> &nbsp; &nbsp;messagebox(0,'盘符已在使用!','出错',48);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;Device := CreateFile(<br> &nbsp; &nbsp; &nbsp; &nbsp;Pchar(VolumeName),<br> &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp;nil,<br> &nbsp; &nbsp; &nbsp; &nbsp;OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_FLAG_NO_BUFFERING,<br> &nbsp; &nbsp; &nbsp; &nbsp;0<br> &nbsp; &nbsp; &nbsp; &nbsp;);<br> &nbsp;if Device = INVALID_HANDLE_VALUE then<br> &nbsp;begin<br> &nbsp; &nbsp;DefineDosDevice(DDD_REMOVE_DEFINITION, Pchar(DriveLetter), nil);<br> &nbsp; &nbsp;messagebox(0,'错误2!','出错',48);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> &nbsp;//Allocmem(Sizeof(OpenFileInformation)+StrLen(FileName)+4);<br> &nbsp;Zeromemory(@OpenFileInformation,Sizeof(OPEN_FILE_INFORMATION)+Length(FileName) + 4);<br> &nbsp;StrPCopy(OpenFileInformation.FileName,'/??/'+FileName);<br> &nbsp;OpenFileInformation.FileNameLength := StrLen(OpenFileInformation.FileName);<br> &nbsp;OpenFileInformation.Read_Only := False;<br><br> &nbsp;IOCTL_FILE_DISK_OPEN_FILE := CTL_CODE(FILE_DEVICE_FILE_DISK, $800, METHOD_BUFFERED, FILE_READ_ACCESS or FILE_WRITE_ACCESS);<br> &nbsp;if not (DeviceIoControl(<br> &nbsp; &nbsp; Device,<br> &nbsp; &nbsp; IOCTL_FILE_DISK_OPEN_FILE,<br> &nbsp; &nbsp; @OpenFileInformation,<br> &nbsp; &nbsp; Sizeof(OpenFileInformation)+ OpenFileInformation.FileNameLength - 1,<br> &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; BytesReturned,<br> &nbsp; &nbsp; nil<br> &nbsp; &nbsp; ))<br> &nbsp;then<br> &nbsp;begin<br> &nbsp; &nbsp;DefineDosDevice(DDD_REMOVE_DEFINITION, Pchar(DriveLetter), nil);<br> &nbsp; &nbsp;messagebox(0,'错误3!','出错',48);<br> &nbsp; &nbsp;Exit<br> &nbsp;end;<br> &nbsp;//ReallocMem();<br> &nbsp;CloseHandle(Device);<br><br>end;<br><br>以上函数可以编译通过,但执行会报错误,好像是指针没有释放。请高手解决。
 
typedef struct _OPEN_FILE_INFORMATION {<br> &nbsp; &nbsp;LARGE_INTEGER &nbsp; FileSize;<br> &nbsp; &nbsp;BOOLEAN &nbsp; &nbsp; &nbsp; &nbsp; ReadOnly;<br> &nbsp; &nbsp;USHORT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileNameLength;<br> &nbsp; &nbsp;UCHAR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileName[1];<br>} OPEN_FILE_INFORMATION, *POPEN_FILE_INFORMATION;<br><br>FileName你没有分配足够的内存,就使用啦,当然会出错!<br>使用时应该用OPEN_FILE_INFORMATION指针,给指针分配Sizeof(OPEN_FILE_INFORMATION)+Length(FileName) + 4个字节。<br> &nbsp;Zeromemory(@OpenFileInformation,Sizeof(OPEN_FILE_INFORMATION)+Length(FileName) + 4);<br> &nbsp;StrPCopy(OpenFileInformation.FileName,'/??/'+FileName);<br><br> &nbsp;if not (DeviceIoControl(<br> &nbsp; &nbsp; Device,<br> &nbsp; &nbsp; IOCTL_FILE_DISK_OPEN_FILE,<br> &nbsp; &nbsp; @OpenFileInformation,<br> &nbsp; &nbsp; Sizeof(OpenFileInformation)+ OpenFileInformation.FileNameLength - 1,<br> &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; BytesReturned,<br> &nbsp; &nbsp; nil<br> &nbsp; &nbsp; ))
 
是的,呵呵,谢谢。<br><br>我自己解决了,但还是给你送上300分。
 
后退
顶部