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

win32utils-devel at rubyforge.org win32utils-devel at rubyforge.org
Sat Nov 6 20:44:08 EST 2004


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 at rubyforge.org
> http://rubyforge.org/mailman/listinfo/win32utils-devel





More information about the win32utils-devel mailing list