Multiple instances of classes, and passing pointers around, multithreading
I am having a bit of a headache trying to structure my program "the right way".
This is the case:
I am making a dynamic link library, containing some resources and functions that will allow me to open a window, throw a skin on it, pass in some value from the calling language and then return. Then I will poll from the calling language to see if the window is closed, and return either a manipulated version of the value that was passed in, or a value indicating which button was pressed (message box for instance) and so on, depending on what purpose the window opened had.
The way I am trying to do things are:
I have a main source file + header that defines the export functions visible to the calling language.
I have one source file + header which defines a class for the skinning process.
I have one source file + header which defines the common functions for a window, mainly;
- Registering the window class
- Creating the window
- Drawing some graphics in the window that is common to all my windows
- Some functions for getting/setting class name, window title, size, position
I have a recouce script defining my resouces (mainly bitmaps).
I have a source file + header for each of my custom controls (they are not using classes, atm (old style C with structures, and so on))
And finally I have a source file + header for each of my different windows.
Now, it shall be possible for some of my windows to have multiple instances open at the same time, so the way I was trying to go about that was to create an instance of my classes in the DLL export function and put them inside a structure, because I am spawning a new thread for each call to my window. The process goes something like this:
- Calling language calls my function for creating a window
- I malloc a pointer to my structure containting pointers to my two classes
- Creates an instance of the two classes by using the 'new' C++ keyword and assigning them to the pointers in my structure like this: structpointer->class = new Class()
- Uses the syntax structpointer->windowclass->Function() to set title and position of my window to be created
- Calls CreateThread passing in the pointer to my structure containing the class pointers
Now the function CreateThread is calling is just a wrapper that calls a WinMain-style function, with an additional parameter; the pointer to my structure with the class pointers.
Inside my WinMain-style function for my window, the following takes place:
- I set the class name for my window using the syntax structurepointer->windowclass->Function()
- Registering the window, passing in my structure pointer to reserve space for it in the cbWndExtra member of the WINDOWCLASSEX structure
- Create the window, assign the pointer to my structure to the window using SetWindowLongPtr() and load my skin
- In my callback procedure I am obtaining the pointer to my structure by calling GetWindowLongPtr() and then process messages, sending the ones that needs processing to my skin class, and processing the others locally, or inside my window class.
Is this a good way to do it? I am not sure if it will work at all... I am not deleting my classes now, for instance, I am not sure how to go about doing that. I also have to return the window handle to my calling language, in some way, so that I have a way of polling the right window for results, and also for doing cleanup on exit.
The way I initially was intending to do this was to have a class for each window too (would solve some of my cleaning up problems by using the destructor), but I ran into some paradoxes with the callback procedure not being static, and if I were to define it as static, i would run into problems with my pointers to the skin class inside the static function, and so on.
Also, I was experiencing some trouble with accessing my resources. It seemed like the calling language did not load the DLL the way I expected. I found a way to "solve" this by calling LoadLibrary("mydll.dll") from my DLL. Not sure if this is very nice to do either.
Now, do you guys have any suggestions and advices on how to do this the most elegant, and smooth way?
Feel free to flame and yell at me for not doing my homework and coming up with stupid ideas :-) I deserve it :-)