Kartikkumar Rao
Kartikkumar Rao

Reputation: 181

How to Initialize Win32 HANDLE appropriately?

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?

Upvotes: 4

Views: 5165

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37122

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.

Upvotes: 6

Related Questions