| 235 | } |
| 236 | |
| 237 | long WindowService::RunApp(const std::wstring &cmd, long mode, DWORD *pid) { |
| 238 | auto cmdptr = std::make_unique<wchar_t[]>(cmd.length() + 1); |
| 239 | memcpy(cmdptr.get(), cmd.data(), cmd.length() * sizeof(wchar_t)); |
| 240 | cmdptr.get()[cmd.length()] = 0; // C字符串需要末尾有0 |
| 241 | /*SECURITY_ATTRIBUTES SA; |
| 242 | SA.bInheritHandle = NULL; |
| 243 | SA.*/ |
| 244 | STARTUPINFO si; |
| 245 | PROCESS_INFORMATION pi; |
| 246 | ZeroMemory(&si, sizeof(si)); |
| 247 | ZeroMemory(&pi, sizeof(pi)); |
| 248 | set_out(pid, 0); |
| 249 | int bret; |
| 250 | std::wstring curr_dir; |
| 251 | if (mode == 1) { |
| 252 | // find |
| 253 | size_t pos; |
| 254 | pos = cmd.find(L".exe"); |
| 255 | if (pos != std::wstring::npos && pos != 0) { |
| 256 | for (int i = static_cast<int>(pos) - 1; i >= 1; --i) { |
| 257 | if (cmd[i] == L'\\' || cmd[i] == L'/') { |
| 258 | pos = i; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | if (pos > 0) { |
| 263 | curr_dir = cmd.substr(0, pos); |
| 264 | } |
| 265 | } |
| 266 | // setlog(curr_dir.c_str()); |
| 267 | } |
| 268 | bret = ::CreateProcessW(nullptr, //// 应用程序名称 |
| 269 | cmdptr.get(), // 命令行字符串 |
| 270 | NULL, // 进程的安全属性 |
| 271 | NULL, // 进程的安全属性 |
| 272 | false, // 是否继承父进程的属性 |
| 273 | 0, // 进程的安全属性 |
| 274 | nullptr, // 进程的安全属性 |
| 275 | mode == 1 && !curr_dir.empty() ? curr_dir.c_str() : nullptr, // 指向当前目录名的指针 |
| 276 | &si, // 传递给新进程的信息 |
| 277 | &pi // 新进程返回的信息 |
| 278 | ); |
| 279 | if (bret) { |
| 280 | set_out(pid, pi.dwProcessId); |
| 281 | op::win32::unique_handle process(pi.hProcess); |
| 282 | op::win32::unique_handle thread(pi.hThread); |
| 283 | } |
| 284 | |
| 285 | return bret; |
| 286 | } |
| 287 | |
| 288 | } // namespace op |