[Win32utils-devel] Calling CreateFile on an instance of File - possible?

win32utils-devel at rubyforge.org win32utils-devel at rubyforge.org
Sat Nov 6 06:30:07 EST 2004


Hi all,

I'm going over win32-file this weekend.  I'm creating instance methods for 
setting (or unsetting) the various file attributes.  So, you can do 
something like:

f = File.open("foo.txt")
f.archive = true
f.hidden = true
f.close

This works for the basic attributes, but it requires extra work for others.  
Specifically, I am having trouble trying to set the 'compress' attribute.  
To do this requires calling CreateFile() + DeviceIoControl().

The thing is, I don't know how to get a valid HANDLE to a instance of an 
already opened file.  I've been trying something like this:

static VALUE file_set_compressed(VALUE self, VALUE rbBool){
   DWORD dwAttr;
   VALUE rbPath;
   HANDLE h;

   if((rbBool != Qtrue) && (rbBool != Qfalse)){
      rb_raise(rb_eTypeError,"Argument must be true or false");
   }

   rbPath = rb_funcall(self,rb_intern("path"),0,0);
   dwAttr = GetFileAttributes(StringValuePtr(rbPath));

   h = CreateFile(
      (LPCTSTR)StringValuePtr(rbPath),
      FILE_ALL_ACCESS,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      dwAttr,
      NULL
   );

   if(h == INVALID_HANDLE_VALUE){
      printf("Error opening file: %s\n",ErrorDescription(GetLastError()));
      return Qnil;
   }

   ...
}

I've tried various flags for the different arguments, all to no avail.  I 
either get "permission denied" or "Error opening file: The process cannot 
access the file because it is being used
by another process.", depending on the flag I use.

So, is it even possible to call CreateFile() to get a HANDLE?  Or is there 
some way I could use the existing file descriptor?  I tried using fileno, 
but had no luck.

Or, am I simply going to have to force this to be a class method?

Regards,

Dan




More information about the win32utils-devel mailing list