Discussion:
Having problems with CreateFile/CloseHandle
(too old to reply)
Andrew Falanga
2010-02-12 23:43:38 UTC
Permalink
Hi,

Ultimately, this is to be used in C#, and I asked first there but was
told I might get better suggestions on using CreateFile( ) here. I
figured out how to use CreateFile( ), now I'm hoping that you all can
tell me if I'm doing it correctly because of the problems I'm having
with CloseHandle( ).

I'm using CreateFile( ) to make a file handle to a printer share so
that I write binary data directory to the printer. I'm able to create
my file ok using CreateFile( ). I'm passing the following parameters
to CreateFile:

CreateFile("path",
0x2, // Generic Write
0, // ignore sharing mode
IntPtr.Zero, // C# equivalent to NULL Pointer for
security attr
0x3, // OPEN_EXISTING
0x80, // flags and attr of FILE_ATTRIBUTE_NORMAL
IntPtr.Zero // again, NULL pointer template file
);

The call returns a valid handle and I'm even able to use it, though,
for some reason not 100% of the time. That is, sometimes I print ok,
others times the printer just sits there. At any rate, when I call
CloseHandle( ) on the handle that I made through CreateFile( ), I get
a false return (from the bool) and my code errors with "can't close
the handle." Is this normal? Should I open a printer share
differently than I am?

Any help is greatly appreciated.

Thanks,
Andy
David Lowndes
2010-02-13 00:31:35 UTC
Permalink
>The call returns a valid handle and I'm even able to use it, though,
>for some reason not 100% of the time. That is, sometimes I print ok,
>others times the printer just sits there. At any rate, when I call
>CloseHandle( ) on the handle that I made through CreateFile( ), I get
>a false return (from the bool)

You're not misinterpreting bool and BOOL (int32) are you?

I'd run the code under the debugger and check with the SysInternals
Process Explorer to see if the handle is actually getting closed or
not.

Dave
Andrew Falanga
2010-02-16 15:48:04 UTC
Permalink
On Feb 12, 5:31 pm, David Lowndes <***@example.invalid> wrote:
> >The call returns a valid handle and I'm even able to use it, though,
> >for some reason not 100% of the time.  That is, sometimes I print ok,
> >others times the printer just sits there.  At any rate, when I call
> >CloseHandle( ) on the handle that I made through CreateFile( ), I get
> >a false return (from the bool)
>
> You're not misinterpreting bool and BOOL (int32) are you?
>
> I'd run the code under the debugger and check with the SysInternals
> Process Explorer to see if the handle is actually getting closed or
> not.
>
> Dave

Ah-ha, I was misinterpreting the return. I just noticed, that is I
took closer notice, of what you wrote. I was thinking that a BOOL was
the same thing as a bool and was marshalling as an 8-bit quantity. I
changed the return value to a plain int and that fixed that part of
it. In fact, the handle is closing just fine, apparently. The return
value from CloseHandle( ) is 0.

I found in Sysinternals, the process monitor. That shows 400+ handles
in use by this program. Is that because of the vshost stuff in VS?

Andy
David Lowndes
2010-02-16 17:03:14 UTC
Permalink
>Ah-ha, I was misinterpreting the return. I just noticed, that is I
>took closer notice, of what you wrote. I was thinking that a BOOL was
>the same thing as a bool and was marshalling as an 8-bit quantity. I
>changed the return value to a plain int and that fixed that part of
>it. In fact, the handle is closing just fine, apparently. The return
>value from CloseHandle( ) is 0.
>
>I found in Sysinternals, the process monitor. That shows 400+ handles
>in use by this program. Is that because of the vshost stuff in VS?

Hi Andy,

I'm glad that's resolved your issue, as to the number of handles, I
don't know.

Dave
Andrew Falanga
2010-02-16 15:03:43 UTC
Permalink
On Feb 12, 5:31 pm, David Lowndes <***@example.invalid> wrote:
> >The call returns a valid handle and I'm even able to use it, though,
> >for some reason not 100% of the time.  That is, sometimes I print ok,
> >others times the printer just sits there.  At any rate, when I call
> >CloseHandle( ) on the handle that I made through CreateFile( ), I get
> >a false return (from the bool)
>
> You're not misinterpreting bool and BOOL (int32) are you?
>
> I'd run the code under the debugger and check with the SysInternals
> Process Explorer to see if the handle is actually getting closed or
> not.
>
> Dave

Dave,

Thanks for the reply. Sorry for my delay in responding but I was off
for three days. I'm pretty sure I'm not misinterpreting the bool
return value. I was bitten by that one once before in some other
code. I've decorated the P/Invoke function, CloseHandle( ), with
marshal as unmanaged uint8.

I'm not entirely sure how to do what you propose, I'm not much of a
Microsoft person. I'll give it a shot though.

Andy
Loading...