func.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "Win32.h"
  2. #include "ID.h"
  3. //Error check&output
  4. int Err(HWND hwnd, LPCTSTR title, LPCTSTR message)
  5. {
  6. if (!hwnd)
  7. {
  8. MessageBox(NULL,message,title, MB_ICONERROR);
  9. return 1;
  10. }
  11. return 0;
  12. }
  13. int Err(ATOM atom, LPCTSTR message, LPCTSTR title)
  14. {
  15. if (!atom)
  16. {
  17. MessageBox(NULL, message, title, MB_ICONERROR);
  18. return 1;
  19. }
  20. return 0;
  21. }
  22. int Err(LPCTSTR title, LPCTSTR message)
  23. {
  24. if (GetLastError() != 0)
  25. {
  26. MessageBox(NULL, message, title, MB_ICONERROR);
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. //
  32. static void AutoClose(DWORD time, LPCWSTR title)
  33. {
  34. Sleep(time*1000);
  35. HWND box = FindWindow(NULL, title);
  36. if (box)
  37. {
  38. SendMessage(box, WM_CLOSE, 0, 0);
  39. }
  40. }
  41. //Open&Stop
  42. HANDLE BDSr_key = NULL;
  43. HANDLE UIw_key = NULL;
  44. void StartBDS()
  45. {
  46. SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE };
  47. if(!CreatePipe(&BDSr_key, &UIw_key, &sa, 0))
  48. Err(TITLE, _T("Failed to create pipe"));
  49. STARTUPINFO si = { sizeof(si) };
  50. si.dwFlags = STARTF_USESTDHANDLES;
  51. si.hStdInput = UIw_key;
  52. si.wShowWindow = SW_SHOWMINIMIZED;
  53. PROCESS_INFORMATION pi;
  54. if (CreateProcess(
  55. L"bedrock_server.exe",
  56. NULL, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
  57. {
  58. HWND hBox = NULL;
  59. MessageBoxW(hWnd,
  60. _T("开始启动服务器\n5s后弹窗自动关闭"), L"Info",
  61. MB_OK);
  62. AutoClose(5, L"Info");
  63. }
  64. else {
  65. Err(TITLE, _T("Failed to send command to BDS!"));
  66. CloseHandle(BDSr_key);
  67. CloseHandle(UIw_key);
  68. }
  69. }