Search code examples
windowswinapicreatewindow

CreateWindowEx fails with ERROR_CANNOT_FIND_WND_CLASS


I am trying to create a window with a certain class type ("VSDebugger CDataTipWnd"), however, when I run CreateWindowEx it fails, and GetLastError tells me that the class wasn't found. Here is the code for reference, though it shouldn't matter:

const Win32WindowStyles dwStyle = Win32WindowStyles.WS_POPUP;
IntPtr wnd = NativeMethods.CreateWindowEx(0L, "VSDebugger CDataTipWnd", "MyWindow",
                                                  dwStyle, 100, 100, 100, 100, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

I am running as a plug-in in the process and I know that a Window of this class is created before (I made sure of that using Winspector/Spy++), yet, when I try to create a window of this type myself, it doesn't work and I can't figure out why.

The documentation says that the "The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names. For a list of system class names, see the Remarks section." is it possible that the RegisterClass was indeed created in another module (dll/exe)? Does merely creating a class in another module make it inaccessible by name from another module?? If so, can I still find that class somehow and create a window with it?

Thanks! Vitaly


Solution

  • You are passing IntPtr.Zero to the HINSTANCE argument.

    The HINSTANCE of the module (EXE or DLL) which registered the class must be provided to CreateWindowEx.

    If you think the main EXE registered the class then you can get its HINSTANCE via GetModuleHandle(IntPtr.Zero).

    If another DLL registered the class then you'll need some way to know which one and get its HINSTANCE/HMODULE (the two are the same these days).