static int l_CreateProcess(lua_State *L){ static const MAX_CMD = 1024; BOOL RetCode = 0; int DoWait = 0; int top = lua_gettop(L); char *CmdLine = 0; STARTUPINFO si; PROCESS_INFORMATION pi; DWORD exit_code = 0;//Код завершения процесса if(top == 0){ lua_pushnil(L); lua_pushstring(L,"No parameters!"); return 2; } if( !lua_isstring(L,1) ){ lua_pushnil(L); lua_pushstring(L,"First param must be a string!"); return 2; } if( top > 1 ){ if( !lua_isboolean(L,2) ){ lua_pushnil(L); lua_pushstring(L,"Second param must be a boolean!"); return 2; } DoWait = lua_toboolean(L, 2); } CmdLine = malloc(MAX_CMD * sizeof(char)); ZeroMemory( CmdLine , sizeof(MAX_CMD * sizeof(char)) ); strncpy(CmdLine,luaL_checkstring(L,1),MAX_CMD-1); ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. RetCode = CreateProcess( NULL, // No module name (use command line) CmdLine, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi // Pointer to PROCESS_INFORMATION structure ); if( ! RetCode ){ lua_pushnil(L); push_lasterr(L,"\"CreateProcess\""); free(CmdLine); return 2; } CloseHandle( pi.hThread ); if(DoWait){ // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); } free(CmdLine); GetExitCodeProcess(pi.hProcess,&exit_code); // Close process and thread handles. CloseHandle( pi.hProcess ); lua_pushnumber( L, exit_code ); return 1; } |