TheTrueJard
TheTrueJard

Reputation: 481

Are win32 hInstances unique?

I’m writing a cache handler that needs a unique ID number for every instance of the application, so that when someone has two projects open in two instances, the caches don’t get confused. According to this thread, it appears the HINSTANCE passed to WinMain is a handle to the module, which could simply be the exe, not necessarily a unique process ID.

The thread seems to say that information about the module/process to be run is brought into memory only once, and the HINSTANCE is a handle to that. Does that mean the HINSTANCE can’t be used as a unique identifier for the process because they all point to the same module? Or am I mistaken?

Upvotes: 1

Views: 297

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126827

On Win32 the HINSTANCE corresponds to the HMODULE of the executable, which in turn boils down to its base address. It isn't unique to the process in any way - AFAIK a given executable will always be loaded at its requested base address.

You can either use the process ID for your task, or, if the fact that process IDs are recycled is a problem, or if you prefer an unique ID across machines, just generate a new GUID at startup and use that as the ID.

Upvotes: 2

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

HINSTANCE is mostly obsolete, a holdover from 16-bit days. It'll have the same value for all instances of your application.

For a unique process ID, use GetCurrentProcessId

Upvotes: 6

Related Questions