隱藏Console視窗無效(續1),console視窗

來源:互聯網
上載者:User

隱藏Console視窗無效(續1),console視窗

 【2014/10/19  23:57 】  

  :通過連接埠遠端控制主機。運行程式之後,程式自動開放原生999連接埠,其他電腦便可以通過999連接埠對本機操作。


程式中使用的到的命令:

        telnet測試連接埠命令: telnet IP 連接埠 或者 telnet 網域名稱 連接埠(若telnet不是內部命令,使用開啟或關閉windows功能,啟動Telnet服務)
        netstat 測試開放的連接埠號碼

        使用ipconfig控制網路連接的一個命令列工具。它的主要功用,包括用來顯示現時網路連接的設定(/all參數),或通過/release參數來釋放取得的ip位置和 通過 /renew 來重新擷取ip位置的分配。

        使用net user 擷取電腦的賬戶資訊

 

    管道是一種簡單的進程間通訊(IPC)機制,實際上是一段共用記憶體。一個進程在向管道寫入資料之後,另一個進程就可以從管道的另一端讀取資料。沒有解決程式視窗隱藏的問題。


// 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 Socketint main(int argc, char * argv[])  {SOCKET listenFd,acceptfd;             struct sockaddr_in serverAddr,clientAddr;char buffer[1024];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;//協議類型是INETserverAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//本機IPserverAddr.sin_port=htons(MasterPort);//綁定連接埠為9990err=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,1);if(err==INVALID_SOCKET){printf("error: listen socket failed \n");return 1;}printf("listen......");acceptfd=accept(listenFd,(struct sockaddr *)&clientAddr,&nSize);//接收客戶串連的準備/**   nLength : The size, in bytes, of this structure. *   lpSecurityDescriptor : A pointer to a SECURITY_DESCRIPTOR structure that controls access to the object,If the value of this member is NULL, *   the object is assigned the default security descriptor associated with the access token of the calling process. *   bInheritHandle : A Boolean value that specifies whether the returned handle is inherited when a new process is created. */SECURITY_ATTRIBUTES sa;sa.nLength=12;sa.lpSecurityDescriptor=0;sa.bInheritHandle=true;HANDLE hReadPipe1;HANDLE hWritePipe1;HANDLE hReadPipe2;HANDLE hWritePipe2;/** Creates an anonymous pipe, and returns handles to the read and write ends of the pipe.*   hReadPipe [out] : A pointer to a variable that receives the read handle for the pipe.*   hWritePipe [out] : A pointer to a variable that receives the write handle for the pipe.*lpPipeAttributes [in, optional] : If lpPipeAttributes is NULL, the handle cannot be inherited.*   nSize [in] : The size of the buffer for the pipe, in bytes. The size is only a suggestion; *                the system uses the value to calculate an appropriate buffering mechanism. If this parameter is zero, the system uses the default buffer size.*/err=CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0);err=CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0);//配置隱藏視窗結構體StartupInfo.cb=sizeof(STARTUPINFO);StartupInfo.wShowWindow=SW_HIDE;StartupInfo.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;StartupInfo.hStdInput=hReadPipe2;StartupInfo.hStdOutput=hWritePipe1;StartupInfo.hStdError=hWritePipe1;//建立匿名管道BOOL ret=CreateProcess(NULL,szCMDPath,NULL,NULL,TRUE,0,NULL,NULL,&StartupInfo,&ProcessInfo);if(ret){printf("%d",GetLastError());}unsigned long lBytesRead;while(1){/** Copies data from a named or anonymous pipe into a buffer without removing it from the pipe. It also returns information about data in the pipe.*   hNamedPipe [in] : A handle to the pipe. This parameter can be a handle to a named pipe instance*lpBuffer [out, optional] : A pointer to a buffer that receives data read from the pipe. This parameter can be NULL if no data is to be read.*   nBufferSize [in] : The size of the buffer specified by the lpBuffer parameter, in bytes. This parameter is ignored if lpBuffer is NULL.*   lpBytesRead [out, optional] : A pointer to a variable that receives the number of bytes read from the pipe. This parameter can be NULL if no data is to be read.*   lpTotalBytesAvail [out, optional] :  pointer to a variable that receives the total number of bytes available to be read from the pipe.*   lpBytesLeftThisMessage [out, optional] : A pointer to a variable that receives the number of bytes remaining in this message*/err=PeekNamedPipe(hReadPipe1,buffer,1024,&lBytesRead,0,0);if(lBytesRead){/** Reads data from the specified file or input/output (I/O) device*   hFile [in] : A handle to the device *   lpBuffer [out] : A pointer to the buffer that receives the data read from a file or device.*   nNumberOfBytesToRead [in] : The maximum number of bytes to be read.*   lpNumberOfBytesRead [out, optional] : A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter*   lpOverlapped [in, out, optional] : */ret=ReadFile(hReadPipe1,buffer,lBytesRead,&lBytesRead,0);if(!ret){break;}ret=send(acceptfd,buffer,lBytesRead,0);if(ret<=0) break;}else{lBytesRead=recv(acceptfd,buffer,1024,0);if(lBytesRead<=0){break;}/** If the function succeeds, the return value is nonzero (TRUE).*/ret=WriteFile(hWritePipe2,buffer,lBytesRead,&lBytesRead,0);if(!ret) break;}}//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;}

  虛擬機器ip地址:192.168.1.42。

  在虛擬機器中運行本程式。在電腦的cmd中輸入

telnet 192.268.1.42 999

  建立了串連:



  建立的串連目錄為 E:\Release.我們需要切換到C盤



通過命令操作電腦:



關閉cmd視窗:



附1:


已通知狀態(受信狀態) 未通知狀態(非受信狀態)

進程核心對象

      當進程正在運行時,進程核心對象處於未通知狀態。當進程停止運行時,就處於已通知狀態。可以通過等待進程來檢查進程是否仍然運行。

      無成功等待的副作用。

線程核心對象

      當線程正在運行時,線程核心對象處於未通知狀態。當線程停止運行時,就處於已通知狀態。可以通過等待線程來檢查線程是否仍然運行。

      無成功等待的副作用。

     

      附2:

     等待函數可使線程自願進入等待狀態,直到一個特定的核心對象變為已通知狀態為止。這些等待函數中最常用的是WaitForSingleObject:
  

DWORD WaitForSingleObject(HANDLE hObject, DWORD dwMilliseconds);

    當線程調用該函數時,第一個參數hObject 標識一個能夠支援被通知/未通知的核心對象。第二個參數dwMilliseconds 用於該線程指明,為了等待該對象變為已通知狀態,它將等待多長時間。

    調用下面這個函數將告訴系統,調用函數準備等待到hProcess控制代碼標識的進程終止運行為止:【真不懂說了什麼】


WaitForSingleObject(hProcess, INFINITE);

第二個參數告訴系統,調用線程願意永遠等待下去(無限時間量),直到該進程終止運行。
通常情況下, INFINITE是作為第二個參數傳遞給WaitForSingleObject的,不過也可以傳遞任何一個值(以毫秒計算)。順便說一下, INFINITE已經定義為0xFFFFFFFF(或-1)。當然,傳遞INFINITE有些危險。如果對象永遠不變為已通知狀態,那麼調用線程永遠不會被喚醒,它將永遠處於死結狀態,

 

    附3:

    listen函數使用主動串連套介面變為被串連套介面,使得一個進程可以接受其它進程的請求,從而成為一個伺服器處理序。在TCP伺服器編程中listen函數把進程變為一個伺服器,並指定相應的通訊端變為被動串連。

listen函數在一般在調用bind之後-調用accept之前調用,它的函數原型是:

#include<sys/socket.h>int listen(int sockfd, int backlog)

返回:0──成功,-1──失敗

 

參數sockfd

被listen函數作用的通訊端,sockfd之前由socket函數返回。在被socket函數返回的通訊端sockfd之時,它是一個主動串連的通訊端,也就是此時系統假設使用者會對這個通訊端調用connect函數,期待它主動與其它進程串連,然後在伺服器編程中,使用者希望這個通訊端可以接受外來的串連請求,也就是被動等待使用者來串連。由於系統預設時認為一個通訊端是主動串連的,所以需要通過某種方式來告訴系統,使用者進程通過系統調用listen來完成這件事。

參數backlog

這個參數涉及到一些網路的細節。在進程正理一個一個串連請求的時候,可能還存在其它的串連請求。因為TCP串連是一個過程,所以可能存在一種半串連的狀態,有時由於同時嘗試串連的使用者過多,使得伺服器處理序無法快速地完成串連請求。如果這個情況出現了,伺服器處理序希望核心如何處理呢?核心會在自己的進程空間裡維護一個隊列以跟蹤這些完成的串連但伺服器處理序還沒有接手處理或進行中的串連,這樣的一個隊列核心不可能讓其任意大,所以必須有一個大小的上限。這個backlog告訴核心使用這個數值作為上限。
毫無疑問,伺服器處理序不能隨便指定一個數值,核心有一個許可的範圍。這個範圍是實現相關的。很難有某種統一,一般這個值會小30以內。

當調用listen之後,伺服器處理序就可以調用accept來接受一個外來的請求。關於accept更的資訊,請接著關注本系統文章。

 


eclipse 341 console視窗怎不出來?

點功能表列的“window”-“show view”-“console”就能在下面看見了,然後想關掉程式在console上面有個紅色的方塊按鈕,點一下就可以了
 
MyEclipse中Console視窗不可以顯示錯誤異常資訊

按圖上的做做看,圖片上有說明



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.