1

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?

TheTrueJard
  • 471
  • 4
  • 18
  • One approach would be for the application to PostMessage to your cache with its main window handle, a GUID that it generates, and perhaps its process ID. Note that process IDs and HWNDs can be re-used so your cache should not assume the HWND and PID will always point to an instance of your application – Dave S Jan 03 '19 at 01:17

2 Answers2

6

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

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
2

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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299