Yes the whole double thing makes no sense. Well, I'm pretty proud with my latest experimentations with all this win32 console programming. The name decoration stumped me for a little while, but I finally got this ugly P.O.S.
The .DLL module
Code:
#define _WIN32_WINNT 0x0600
#include <windows.h>
__declspec(dllexport) char* __cdecl WriteColoredString(const char* s, short int color)
{
DWORD dwNumChars;
HANDLE hStdout = ::GetStdHandle(STD_OUTPUT_HANDLE);
::SetConsoleTextAttribute(hStdout, color);
/*
* std::cout << s;
* printf("%s", s);
*/
// Write string and change it back to white
::WriteConsole(hStdout, s, strlen(s), &dwNumChars, 0);
::SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
return const_cast<char*>(s);
}
And the executable that loads it:
Code:
#include <windows.h>
#include <iostream>
typedef enum colors
{
red = FOREGROUND_RED,
blue = FOREGROUND_BLUE,
green = FOREGROUND_GREEN
};
typedef char* (*LPFUNC) (const char*, short int);
int main(int argc, char *argv[])
{
HINSTANCE hLib;
hLib = ::LoadLibrary("C:\\Development\\Dev\\test\\test1\\Debug\\Project1.dll");
if(hLib != NULL)
{
char* (*ourFunc)(const char*, short int) =
reinterpret_cast<char* (*) (const char*, short int)>
(::GetProcAddress(hLib, "_Z18WriteColoredStringPKcs"));
if(ourFunc != NULL)
(*ourFunc) ("hello", red);
else std::cout << "Error loading function from DLL\n";
}
else
std::cout << "Error loading DLL\n";
return 0;
}