首页 > 代码库 > 求助(VC++) 隐藏Console窗口无效
求助(VC++) 隐藏Console窗口无效
[逝去的100~~ 2014/10/07 20: 20]
程序想要实现控制台窗口的隐藏,但是窗口每次运行总会弹出来。为什么呢?
代码如下:
// Mini.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #pragma comment(lib,"ws2_32.lib") #include <winsock2.h> #include <windows.h> //#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) #define MAX_SER 10 #define HOST_PATH 256 #define HOSTNAME_SIZE HOST_PATH #define MasterPort 999 //定义监听的端口 char hostName[MAX_PATH]={0}; unsigned short maxService; unsigned short port; void Service(LPVOID lpv); int LoopControl(SOCKET llistenfd,int isMultiTasking); void initial(); int initSockets(void); //初始化Windows Socket int main(int argc, char * argv[]) { SOCKET listenFd,acceptfd; struct sockaddr_in serverAddr,clientAddr; int nSize=sizeof(sockaddr_in); int err; PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; char szCMDPath[255]; initial(); initSockets(); //分配内存资源 ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION)); ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); GetEnvironmentVariable("COMSPEC",szCMDPath,sizeof(szCMDPath)); //GetStartupInfo(&StartupInfo); //创建socket //listenFd=socket(PF_INET,SOCK_STREAM,0); listenFd=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,0,0); if(listenFd==INVALID_SOCKET){ printf("error:out of socket resource \n"); return 1; } //bind本机的端口 serverAddr.sin_family=AF_INET; //协议类型是INET serverAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); //本机IP serverAddr.sin_port=htons(MasterPort); //绑定端口为5054 err=bind(listenFd,(const struct sockaddr *)&serverAddr,sizeof(serverAddr)); if(err==INVALID_SOCKET){ printf("error: unable to bind socket \n"); return 1; } //listen 监听端口 err=listen(listenFd,3); if(err==INVALID_SOCKET){ printf("error: listen socket failed \n"); return 1; } printf("listen......"); acceptfd=accept(listenFd,(struct sockaddr *)&clientAddr,&nSize); //接收客户连接的准备 //配置隐藏窗口结构体 StartupInfo.cb=sizeof(STARTUPINFO); StartupInfo.wShowWindow=SW_HIDE; StartupInfo.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; //StartupInfo.cbReserved2=0; //StartupInfo.lpReserved2=NULL; StartupInfo.hStdInput=(HANDLE)listenFd; StartupInfo.hStdOutput=(HANDLE)listenFd; StartupInfo.hStdError=(HANDLE)listenFd; //创建匿名管道 CreateProcess(NULL,szCMDPath,NULL,NULL,TRUE,CREATE_NO_WINDOW,NULL,NULL,&StartupInfo,&ProcessInfo); WaitForSingleObject(ProcessInfo.hProcess,INFINITE); CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); printf("server is down \n"); //关闭进程句柄 closesocket(listenFd); closesocket(acceptfd); WSACleanup(); return 0; } void initial() { maxService=3; port=5054; } /* * Winsock服务初始化 */ int initSockets(void) { WSADATA wsaData; WORD sockVersion; //typedef unsigned short WORD(16) int err; sockVersion=MAKEWORD(2,2); err=WSAStartup(sockVersion,&wsaData); if(err!=0) { printf("error %d :winsock not avaliable\n",err); } printf("environemnt invaild success.....\n"); return 0; }
想法是通过CreateProcess来实现窗口的隐藏,添加以下的代码,确实实现了窗口的隐藏,但不是本意:
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
运行的效果图如下:
看了其他的介绍说:要达到DOS程序运行时不显示窗口的效果,有两种可能:一是创建该DOS进程时,指定不创建窗口;二是隐藏该DOS进程有窗口。我这里都明确指定了啊。
Help
求助(VC++) 隐藏Console窗口无效
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。