Home > Blockchain >  What is the expected lifetime of lpszClassName in WNDCLASS(EX)?
What is the expected lifetime of lpszClassName in WNDCLASS(EX)?

Time:11-27

When creating a WNDCLASS(EX) in C , one might do as follows:

WNDCLASSEX wndClass {};
wndClass.lpszClassName = "MyWndClass";

The data backed by the string literal is available for the whole program's lifetime.

So what if the data was only available during the invocation of RegisterClassEx?

{
  char className[] = "MyWndClass";

  WNDCLASSEX wndClass {};
  wndClass.lpszClassName = className;
  // other things...

  RegisterClassEx(&wndClass);
}

HWND window = CreateWindowEx(0, "MyWndClass", /* other parameters... */);

Would this still work?

CodePudding user response:

So what if the data was only available during the invocation of RegisterClassEx? Would this still work?

Yes, it is perfectly fine. The values that you register are copied by the OS until the class is unregistered at a later time. The actual WNDCLASSEX instance itself is no longer needed once RegisterClassEx() returns.

So, what is important is that the class name value that you register must match the class name value that you pass to CreateWindowEx(). They do not need to be pointing at the same memory address.

  • Related