win32utils-devel@rubyforge.org
2004-Nov-06 06:31 UTC
[Win32utils-devel] Calling CreateFile on an instance of File - possible?
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
win32utils-devel@rubyforge.org
2004-Nov-06 20:44 UTC
[Win32utils-devel] Calling CreateFile on an instance of File - possible?
Hi Dan, First, thanks for all your great work on the win32-* libraries!> I don''t know how to get a valid HANDLE to a instance of an > already opened file.I took a look at how to determine the HANDLE that corresponds to a given FILE* or a fileno. Not surprisingly, I couldn''t find any way to recover that HANDLE. Since this file must have a HANDLE, another approach would be to look at all the HANDLEs in the process and find the one that corresponds to this file. This is possible, because Process Explorer from www.sysinternals.com will display (among many other things) all the File HANDLES in a process, and the full path to their files. Unfortunately, sysinternals no longer releases the source for Process Explorer, and I don''t know how they get this information. Take care, Wayne Vucenic Saturday, November 6, 2004, 3:30:07 AM, you wrote:> 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> _______________________________________________ > win32utils-devel mailing list > win32utils-devel@rubyforge.org > http://rubyforge.org/mailman/listinfo/win32utils-devel
win32utils-devel@rubyforge.org
2004-Nov-06 21:10 UTC
[Win32utils-devel] Calling CreateFile on an instance of File -possible?
Hi,> Hi Dan, > > First, thanks for all your great work on the win32-* libraries! > >> I don''t know how to get a valid HANDLE to a instance of an >> already opened file. > > I took a look at how to determine the HANDLE that corresponds to a > given FILE* or a fileno. Not surprisingly, I couldn''t find any way to > recover that HANDLE. > >You can use _get_osfhandle function to get the HANDLE from a fileno. Refer to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__get_osfhandle.asp> Take care, > > Wayne Vucenic >Regards, Park Heesob
win32utils-devel@rubyforge.org
2004-Nov-06 21:42 UTC
[Win32utils-devel] Calling CreateFile on an instance of File -possible?
Hi Park,> You can use _get_osfhandle function to get the HANDLE from a fileno.> Refer to > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__get_osfhandle.aspThat''s a great piece of information! Since the _fileno function will get the fileno from a FILE*: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__fileno.asp we can now convert FILE* ==> fileno ==> HANDLE I think this might do what Dan needs. Take care, Wayne Vucenic