how can i create a new console?

i'm trying build a class for create and use a console:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
HANDLE hPipeRead,hPipeWrite;
    SECURITY_ATTRIBUTES tSecur;
    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    char *lBuffer;

public:
    console(string title)
    {
        static bool i=false;
        AllocConsole();
        if(i==true)
        {
            tSecur.nLength = sizeof(SECURITY_ATTRIBUTES);
            tSecur.bInheritHandle = TRUE;
            tSecur.lpSecurityDescriptor = NULL;

            CreatePipe(&hPipeRead,&hPipeWrite,&tSecur,0);
            ZeroMemory (&si,sizeof(STARTUPINFO));
            ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
            si.cb=sizeof(STARTUPINFO);
            si.dwFlags |= STARTF_USESTDHANDLES ;
            si.hStdInput = hPipeRead;
            si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
            si.hStdError = GetStdHandle(STD_ERROR_HANDLE);

            if (! CreateProcess(NULL,"cmd.exe",0,0,TRUE,CREATE_NEW_CONSOLE, NULL,NULL, &si, &pi))
            {
                DebugText("error: no console created!!! getlasterror: " +  to_string(GetLastError()));
            }
            hConsole=hPipeRead;
            windowconsole=GetHWNDConsole();
            hdcwindow=GetDC(windowconsole);
        }
        else
        {
            hConsole=GetStdHandle(STD_OUTPUT_HANDLE);

            i=true;
        }

        HANDLE out=CreateFile(TEXT("CONOUT$"),GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
        hConsole=out;
        windowconsole=GetHWNDConsole();
        hdcwindow=GetDC(windowconsole);
        SetWindowText(windowconsole,title.c_str());
        cls();
        cout << "hello world";
        DWORD srect;
        WriteConsoleOutputCharacter(hConsole, "hello world" ,strlen("hello world"),{0,10},&srect);
    }

i get the Command Prompt but not an empty console and i can't write on it or clean, because i'm not getting the right console handle :(
what i'm doing wrong?
Last edited on
i'm trying build a class for create and use a console:

Are you doing so in a console or windows app? A program may only have one console window associated with it. If you already have a console, you cannot create another one.
i'm on windows app. so the best it's create my own console using a window and 1 edit control?
but think with me:
- the CreateProcess() open a new console. but it's the window Command Prompt... can i get it's handle for work with it?
(yah the console functions needs it's handle)
the CreateProcess() open a new console


You don't open a console via CreateProcess.

You may benefit from perusing: http://dslweb.nwnexus.com/~ast/dload/guicon.htm
It is typically a good idea to Google stuff like this.

http://www.google.com/search?btnI=1&q=msdn+Creation+of+a+Console
Duoas (9932): after i clicked on them.. right
I don't understand incomplete sentences.
Topic archived. No new replies allowed.