For my first program, instead of "Hello world", I made a console application that allows you to type in the name of a DLL, and the console will load it and explicitly run a function named "Initiate" from the DLL. I have a DLL that creates a Win32 window, and it goes through the usual loop to keep the window alive. It seems to work great. I can even 'cout' messages to the console from the DLL, which is kinda cool.
So when I close the Win32 window, it frees the DLL and the console remains open, which has a goto statement to start over, and allow more input for another DLL. The problem that I am having, is that I want to be able to allow input to open another DLL right away. I'm assuming that the loop to keep the Win32 window open to proccess messages, is preventing the console from executing any further.
However, I'm not really sure how to approach this problem. Infact, the only way I was able to get as far as I did, is with Google. But now I'm not even really sure what search term to use. I don't really know much about threads, but that would be my best guess for this situation. Is that close, or far off?
Here is my code...
RunDLL: (console app to load DLL)
Code:
#include <iostream>
#include <string>
#include <windows.h>
std::string str_input;
typedef int (*INIT)(HWND, HINSTANCE, int);
int main()
{
char t[500]; // Create var to hold title
GetConsoleTitleA(t, 500); // Get title and assign to var
HWND hwndConsole = FindWindowA( NULL, t ); // Assign HWND by search for title
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwndConsole, GWL_HINSTANCE); // Get handle instance using HWND
load: // LOAD SEQUENCE
std::cout << "> ";
std::cin >> str_input;
if (str_input == TEXT("exit")) goto exit;
HINSTANCE hInstLibrary = LoadLibrary(str_input.c_str());
if (hInstLibrary)
{
INIT Initiate = (INIT)GetProcAddress(hInstLibrary, "Initiate");
if (Initiate)
{
Initiate(hwndConsole, hInstance, SW_SHOWNORMAL);
}
FreeLibrary(hInstLibrary);
}
else
{
std::cout << "DLL failed to load" << std::endl;
}
goto load;
exit: // EXIT SEQUENCE
return 0;
}
If anyone decides to compile this, compile it with character set "not set". That was a real pain to figure out. This conversation is open for any kind of suggestions or feedback, so please comment.
For RunDLL, I see a future problem I'm going to need to figure out.
Create a new thread each time the name of a DLL is entered using CreateThread.
The LoadLibrary and the if / else code must be put into the thread function.
«_Superman_» I love work. It gives me something to do between weekends.
Thank you very much. It's weird how searching Google for "C++ CreateThread" happens to come up with some better examples, then searching for something like "C++ threads".
Bookmarks