Win32: Creating a new HINSTANCE ?

Started by
6 comments, last by Darragh 18 years, 2 months ago
Hi everyone. I'm currently in the process of creating a simplified little Win32 / OpenGL wrapper for use in our maths class, so we can all mess about with transforms and matrices etc. quickly and easily. What I want to do is make a simple class which allows the user to display some common primitives, modify the transformation matrices and other such things and handle all of the details behind Windows programming. I want to hide away as much as possible, all of the Win32 details especially the standard WinMain function. Presently the main part of program uses the standard Win32 template- and passes the hInstance paramter to the constructor for my OpenGL Window class, which it then uses when creating the window. The code for this looks something like below:


#include <windows.h>
#include "GL_Window.h"

int WinMain
(          
  HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR     lpCmdLine,
  int        nCmdShow
);
{
   // Create a new OpenGL Window which allows drawing etc.. 

   GL_Window pWindow = new GL_Window( hInstance ); return 0;

}

What I'd prefer to do is replace WinMain with the standard main() function and have the window create its own process and handle in its constructor:

#include "GL_Window.h"

int main ( void );
{
   // Create a new OpenGL Window which allows drawing etc..

   GL_Window pWindow = new GL_Window(); return 0;
}

Is such a thing possible to do ? Or can only Windows itself create a new program space and pass the handle through WinMain ? It would be ideal if the GL_Window class could manage its own details completely without reliance on the main() function for paramters. If anyone has any ideas or good links then i would be grateful. I've been looking through MSDN but haven't found anything to point me in the right direction, then again maybe i've been looking in the wrong places. Cheers, Darragh
Advertisement
Mu.

Windows creates your process for you. Where would your executable run if not in its own process? You can't run two executables in the same process.
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
You can get all those parameters from the WinMain with WinAPI functions.

GetModuleHandle( NULL ) returns your HINSTANCE
GetCommandLine returns the passed parameters (appended in one string, so you have to seperate them yourself)
GetStartupInfo fills a struct that holds in wShowWindow the nCmdShow param

The previous instance handle is always NULL for 32bit-apps.

Edit: Bad Oluseyi, stop being that fast :)

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Quote:Original post by Catafriggm
Mu.

Windows creates your process for you. Where would your executable run if not in its own process? You can't run two executables in the same process.


I understand that much. I thought it was possible to create your own though ? - maybe i'm a bit confused here. Is HINSTANCE a handle to a program or just an individual process ?

Is there another way to get the HINSTANCE variable i need from within the GL_Window constructor without relying on main() ? The aim is simply to make the GL_Window class as independant as possible. It shouldn't (ideally) need a handle obtained from main in order to create its window.

Quote:Original post by Oluseyi
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);


Wow, that was quick!

Thank you. That is exactly what i was looking for! : )
Also, if you're programming only for the NT based kernels (NT, Win2K and XP) then the HINSTANCE parameter to CreateWindow() is ignored, which means you don't need it to create a window.
Quote:Original post by SiCrane
Also, if you're programming only for the NT based kernels (NT, Win2K and XP) then the HINSTANCE parameter to CreateWindow() is ignored, which means you don't need it to create a window.


Cool. Thats even easier still. Thanks everyone, I can go ahead now and get my little window up and running..

This topic is closed to new replies.

Advertisement