Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Initialize Win32 HANDLE appropriately?

Tags:

winapi

handle

Is it OK to initialize the Win32 HANDLE to NULL? Or are there any better or recommended way to initialize HANDLEs?

For example,

void foo()
{
   HANDLE hFile;
   hFile = CreateFile(/* necessary arguments*/);
}

What shall i initialize the hFile with?

like image 736
Kartikkumar Rao Avatar asked Nov 05 '25 18:11

Kartikkumar Rao


1 Answers

In your example code you don't need to initialise hFile to anything since it is initialised by the CreateFile function.

The Windows API is not completely consistent on what it considers a "null" handle. Some APIs (like CreateFile) return INVALID_HANDLE_VALUE on failure, whereas others return NULL on failure. So the way you test if a HANDLE is valid or not depends on the function you are getting it from - check the actual documentation for the function and see what it says is the return value on failure.

like image 178
Jonathan Potter Avatar answered Nov 07 '25 15:11

Jonathan Potter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!