Code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
BOOL WINAPI AttachConsole(DWORD);
pid_t GetProcessID( char * ProcessName )
{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
/* Take a snapshot of all processes in the system. */
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
return 0;
}
/* Set the size of the structure before using it. */
pe32.dwSize = sizeof( PROCESSENTRY32 );
if( !Process32First( hProcessSnap, &pe32 ) )
{
CloseHandle( hProcessSnap );
exit(EXIT_FAILURE);
}
/* Walk through the snapshot, and return the process handle when
found. */
do
{
if (strcmp(pe32.szExeFile, ProcessName) == 0)
{
CloseHandle( hProcessSnap );
return pe32.th32ProcessID;
}
} while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return 0;
}
int main( int argc, char * argv[] )
{
if ( argc != 2 )
{
fprintf(stderr, "You must have at least 1 argument!");
exit(1);
}
pid_t CommandPromptID = 0;
while ( ( CommandPromptID = GetProcessID("cmd.exe") ) == 0 ) Sleep(5000); /* Get command prompt's identifier */
if ( !AttachConsole( CommandPromptID ) )
{
fprintf(stderr, "Could not attach to the command prompt console!");
exit(1);
}
if ( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
fprintf(stderr, "Failed to initialize SDL!");
exit(1);
}
SDL_Surface* image = NULL;
SDL_Surface* console = NULL;
SDL_Rect my_image = { 160, 100, 200, 200 };
SDL_Rect * p_my_image = &my_image;
if ( ( image = SDL_LoadBMP( argv[1] ) ) == NULL )
{
fprintf(stderr, "Could not load the bitmap image!");
SDL_FreeSurface( image );
SDL_Quit();
exit(1);
}
if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
{
fprintf(stderr, "Could not get the current console screen!");
SDL_FreeSurface( image );
SDL_Quit();
exit(1);
}
SDL_BlitSurface( image, NULL, console, p_my_image);
SDL_Flip( console );
SDL_Delay(2000);
SDL_FreeSurface( image );
SDL_Quit();
return 0;
}
After linking the code there are no errors or warnings. It always fails at this line though
Code:
if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
Then it, of course, displays my error message. Is there a better way of filling out the console struct? Should I do it myself? Is my method of calling AttachConsole to be able to get the image on the screen even valid? Those are the questions I can't answer for myself right now. Also, even if it would be better to fill out the data myself, I would have trouble trying to get the information since I don't know how to get it.