I'm having real complex problems with Windows API and a console application.

The issue is that I can't have both windows active at the same time.

I tried to include both headers, did both required tasks but only one of the windows opens.

Here is the completed code:

#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
    int socialtwo = 200000000;
    cout << "Struct members: " << socialtwo;
    cin.get();
    return 0;
}

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

I really am frustrated because I wanted to make a game that is played between two different windows, sort of like two programs, but I can't fix or solve the issue for any thing.

If you want to use 2 windows, then you should develop a Windows app, and after the main window is created, create a 2nd window. You can't really do a console app and Windows app at the same time (OK, perhaps you CAN, but I think it would be a lot more trouble than just using 2 windows).

If you want your second window to be like a "console", then create it and use a black background and fixed-width fonts to simulate a "console" look.

I tried to include both headers, did both required tasks but only one of the windows opens.

There can only be one entry point into the program. For a console project that entry point is the traditional main(). For a Windows project, the entry point is WinMain. If you want a console window to use in your application, you can open one with little difficulty:

#include <string>
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
    if (AllocConsole()) {
        std::string s = "I made a console window!";
        DWORD n;
        char c;

        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), s.c_str(), s.size(), &n, 0);
        ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &n, 0);
    }
}

Attaching the standard C++ handles to the new console window so you can take advantage of cin and cout is marginally more difficult (and involves a few assumptions), but nothing terrible:

#include <iostream>
#include <string>
#include <cstdio>
#include <windows.h>
#include <io.h>
#include <fcntl.h>

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
    if (AllocConsole()) {
        int ifd = _open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
        int ofd = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);

        *stdin = *_fdopen(ifd, "r");
        *stdout = *_fdopen(ofd, "w");

        std::cout<<"I made a console window";
        std::cin.get();

        fclose(stdout);
        fclose(stdin);
    }
}

While the latter isn't documented as such anywhere to the best of my knowledge, the former is easily located on MSDN.

Attached is an example that I whipped up for someone else not too long ago that shows how to have both a console and windows gui app in the same *.exe program. First, create a windows gui app, then a console app. Copy all the source files from the gui app into the console app project directory, then add the gui app files to the console app project. At the beginning of main() in the console app crete another thread and in that thread proc call WinMain() of the gui app.

Attached is an example that I whipped up for someone else not too long ago that shows how to have both a console and windows gui app in the same *.exe program. First, create a windows gui app, then a console app. Copy all the source files from the gui app into the console app project directory, then add the gui app files to the console app project. At the beginning of main() in the console app crete another thread and in that thread proc call WinMain() of the gui app.

I tried the zip file download and it doesn't work.

And Narue, both your codes only produce a console application window. What I want is to have both of them at the same time when the program runs.

What didn't work? The download? You can't unzip the file? Or the program? Did you compile the program? I created that project with vc++ 2010 Express.

And Narue, both your codes only produce a console application window.

Duh. Creating the other windows and threading them properly is your job. I only showed you the part you were interested in: creating a console window.

Just create a console application (linker option /SUBSYSTEM:CONSOLE) and write your normal gui code. (Both use the same Win32 or Win64 environment subsystem).

You will have a Console created for you and a main() with stdin, stdout and stderr initialized correctly. Use GetModuleHandle(), GetStartupInfo() etc. if you need the parameters that would have been passed to a WinMain.

#include <iostream>
#include <windows.h>

long __stdcall WindowProcedure( HWND window, unsigned int msg, WPARAM wp, LPARAM lp )
{
    switch(msg)
    {
        case WM_DESTROY:
            std::cout << "\ndestroying window\n" ;
            PostQuitMessage(0) ;
            return 0L ;
        case WM_LBUTTONDOWN:
            std::cout << "\nmouse left button down at (" << LOWORD(lp)
                      << ',' << HIWORD(lp) << ")\n" ;
            // fall thru
        default:
            std::cout << '.' ;
            return DefWindowProc( window, msg, wp, lp ) ;
    }
}

int main()
{
    std::cout << "hello world!\n" ;
    const char* const myclass = "myclass" ;
    WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, WindowProcedure,
                            0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
                            LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
                            0, myclass, LoadIcon(0,IDI_APPLICATION) } ;
    if( RegisterClassEx(&wndclass) )
    {
        HWND window = CreateWindowEx( 0, myclass, "title",
                   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                   CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(0), 0 ) ;
        if(window)
        {
            ShowWindow( window, SW_SHOWDEFAULT ) ;
            MSG msg ;
            while( GetMessage( &msg, 0, 0, 0 ) ) DispatchMessage(&msg) ;
        }
    }
}

Unfortunately I don't think it's possible to have two windows in focus at the same time on Windows. I don't think the OS works like that.

Well, technically a Winxx console is not a window (though its input/output is managed by the Winxx subsystem) and can never be the recipient of Winxx 'focus'. SetFocus() and the like do not work with consoles.

The OS has nothing to do with focus (or with windows for that matter). Focus policies ("focus follows click", "focus follows mouse", "sloppyfocus" etc) are implemented by window managers. For example on X11, different focus models are used by different window managers.

Well, technically a Winxx console is not a window (though its input/output is managed by the Winxx subsystem) and can never be the recipient of Winxx 'focus'. SetFocus() and the like do not work with consoles.

The OS has nothing to do with focus (or with windows for that matter). Focus policies ("focus follows click", "focus follows mouse", "sloppyfocus" etc) are implemented by window managers. For example on X11, different focus models are used by different window managers.

I know how to create a console window, Narue. What, do you think I'm some retard?

Mocking me? lol.

Any ways, neither of the next given code examples show a console window and a Windows API window at the same exact time.

So once some one explains how it's done and doesn't just put wrong codes here then let me know.

commented: Yes. Yes, I do. -5
commented: Me too +0

Closing thread as OP will only come back, if ever, in 6 months and hopefully solve her personality and behaviour issues in the mean time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.