win32utils-devel@rubyforge.org
2004-Nov-07 11:09 UTC
[Win32utils-devel] Problems with DeviceIoControl()
Hi all, Thanks to Wayne and Park, I''ve got something like this now: static VALUE file_set_compressed(VALUE self, VALUE rbBool){ HANDLE h; BOOL rv; DWORD dwBytesReturned; int fn; USHORT inBuf = COMPRESSION_FORMAT_DEFAULT; if((rbBool != Qtrue) && (rbBool != Qfalse)){ rb_raise(rb_eTypeError,"Argument must be true or false"); } if(rbBool == Qfalse){ inBuf = COMPRESSION_FORMAT_NONE; } fn NUM2INT(rb_funcall(self,rb_intern("fileno"),0,0)); h = (HANDLE)_get_osfhandle(fn); // Get HANDLE based on fileno if(h == INVALID_HANDLE_VALUE){ rb_raise(cFileError,ErrorDescription(GetLastError())); } rv = DeviceIoControl( h, FSCTL_SET_COMPRESSION, (LPVOID)inBuf, sizeof(inBuf), NULL, 0, &dwBytesReturned, NULL ); if(!rv){ rb_raise(cFileError,ErrorDescription(GetLastError())); } return self; } // Inside Init_file() rb_define_method(rb_cFile,"compressed=",file_set_compressed,1); # Sample program f = File.open("temp.txt") f.compressed = true f.close But, I''m still getting "access denied" or "invalid access to memory location", depending on the open mode flag I use. Testing indicates that I am getting a valid HANDLE, but that DeviceIoControl() is failing. I can, however, compress the file using the GUI interface. Any ideas? Wayne, I saw your note on using _fileno, but I don''t think I need it here, do I? Regards, Dan __________________________________ Do you Yahoo!? Check out the new Yahoo! Front Page. www.yahoo.com
win32utils-devel@rubyforge.org
2004-Nov-08 01:26 UTC
[Win32utils-devel] Problems with DeviceIoControl()
Hi Dan,> Wayne, I saw your note on using _fileno, but I don''t > think I need it here, do I?No, it doesn''t look like you need to use _fileno. Yesterday I took a quick look at Ruby''s file.c code, and it looked to me that it was using a FILE* instead of a fileno, so I thought it might be necessary to first use _fileno to get a fileno to be able to use the function Park pointed out. But based on your code it looks like you directly have access to fileno, so _fileno isn''t needed. I''ll try to run some experiments with DeviceIoControl(), and if I learn anything useful, I''ll let you know. Take care, Wayne Vucenic
win32utils-devel@rubyforge.org
2004-Nov-08 03:00 UTC
[Win32utils-devel] Problems with DeviceIoControl()
Hi,> > Hi all, > > Thanks to Wayne and Park, I''ve got something like this > now: >..> > But, I''m still getting "access denied" or "invalid > access to memory location", depending on the open mode > flag I use. > > Testing indicates that I am getting a valid HANDLE, > but that DeviceIoControl() is failing. I can, > however, compress the file using the GUI interface. > > Any ideas? > > Wayne, I saw your note on using _fileno, but I don''t > think I need it here, do I? >You have missed & before inBuf. Here is my modified version: --------------------------------------------------------------- static VALUE file_set_compressed(VALUE self, VALUE rbBool){ HANDLE h; BOOL rv; DWORD dwBytesReturned; int fn; VALUE rbPath; USHORT inBuf = COMPRESSION_FORMAT_DEFAULT; if((rbBool != Qtrue) && (rbBool != Qfalse)){ rb_raise(rb_eTypeError,"Argument must be true or false"); } if(rbBool == Qfalse){ inBuf = COMPRESSION_FORMAT_NONE; } rbPath = rb_funcall(self,rb_intern("path"),0,0); h = CreateFile( StringValuePtr(rbPath), FILE_READ_DATA | FILE_WRITE_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); rv = DeviceIoControl( h, FSCTL_SET_COMPRESSION, (LPVOID)&inBuf, sizeof(inBuf), NULL, 0, &dwBytesReturned, NULL ); CloseHandle( h ); if(!rv){ rb_raise(cFileError,ErrorDescription(GetLastError())); } return self; } ---------------------------------------------------------------- Regards, Park Heesob --MIME Multi-part separator--