app-helpers.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "app-helpers.h"
  4. #include "nt-stuff.h"
  5. WINADVAPI WINAPI ConvertSidToStringSidW(PSID sid, LPWSTR *str);
  6. bool is_app(HANDLE process)
  7. {
  8. DWORD size_ret;
  9. DWORD ret = 0;
  10. HANDLE token;
  11. if (OpenProcessToken(process, TOKEN_QUERY, &token)) {
  12. BOOL success = GetTokenInformation(token, TokenIsAppContainer,
  13. &ret, sizeof(ret), &size_ret);
  14. if (!success) {
  15. DWORD error = GetLastError();
  16. int test = 0;
  17. }
  18. CloseHandle(token);
  19. }
  20. return !!ret;
  21. }
  22. wchar_t *get_app_sid(HANDLE process)
  23. {
  24. wchar_t *ret = NULL;
  25. DWORD size_ret;
  26. BOOL success;
  27. HANDLE token;
  28. if (OpenProcessToken(process, TOKEN_QUERY, &token)) {
  29. DWORD info_len = GetSidLengthRequired(12) +
  30. sizeof(TOKEN_APPCONTAINER_INFORMATION);
  31. PTOKEN_APPCONTAINER_INFORMATION info = malloc(info_len);
  32. success = GetTokenInformation(token, TokenAppContainerSid,
  33. info, info_len, &size_ret);
  34. if (success)
  35. ConvertSidToStringSidW(info->TokenAppContainer, &ret);
  36. free(info);
  37. CloseHandle(token);
  38. }
  39. return ret;
  40. }
  41. static const wchar_t *path_format =
  42. L"\\Sessions\\%lu\\AppContainerNamedObjects\\%s\\%s";
  43. HANDLE open_app_mutex(const wchar_t *sid, const wchar_t *name)
  44. {
  45. wchar_t path[MAX_PATH];
  46. DWORD session_id = WTSGetActiveConsoleSessionId();
  47. _snwprintf(path, MAX_PATH, path_format, session_id, sid, name);
  48. return nt_open_mutex(path);
  49. }
  50. HANDLE open_app_event(const wchar_t *sid, const wchar_t *name)
  51. {
  52. wchar_t path[MAX_PATH];
  53. DWORD session_id = WTSGetActiveConsoleSessionId();
  54. _snwprintf(path, MAX_PATH, path_format, session_id, sid, name);
  55. return nt_open_event(path);
  56. }
  57. HANDLE open_app_map(const wchar_t *sid, const wchar_t *name)
  58. {
  59. wchar_t path[MAX_PATH];
  60. DWORD session_id = WTSGetActiveConsoleSessionId();
  61. _snwprintf(path, MAX_PATH, path_format, session_id, sid, name);
  62. return nt_open_map(path);
  63. }