Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.6 KiB

  1. #include "stdafx.h"
  2. #include <shlobj.h>
  3. #include <shellapi.h>
  4. #define IID_PPV_ARG(IType, ppType) IID_##IType, reinterpret_cast<void**>(static_cast<IType**>(ppType))
  5. #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[1]))
  6. // Jay Simmons
  7. // There are two ways to use the session moniker
  8. // one is via IMoniker::BindToObject, the other IClassActivator::GetClassObject.
  9. // IMoniker::BindToObject has an advantage in that it will work remotely.
  10. // More importantly, the syntax that I mention below is different for the two methods.
  11. // For BindToObject, you must use the full syntax as I describe below.
  12. // For IClassActivator, the clsid is passed in on the GetClassObject call and so
  13. // the syntax is reduced to "Session:7" or "Session:Console".
  14. HRESULT CoCreateOnConsoleWithBindToObject(REFCLSID rclsid, REFIID riid, void** ppv)
  15. {
  16. *ppv = NULL;
  17. IBindCtx* pbc;
  18. HRESULT hr = CreateBindCtx(0, &pbc);
  19. if (SUCCEEDED(hr))
  20. {
  21. WCHAR sz[128], szClsid[64];
  22. StringFromGUID2(rclsid, szClsid, ARRAYSIZE(szClsid));
  23. LPWSTR psz = szClsid + 1; // skip "{"
  24. while (*psz != L'}')
  25. psz++;
  26. *psz = NULL;
  27. lstrcpyW(sz, L"Session:Console!clsid:");
  28. lstrcatW(sz, &szClsid[1]);
  29. // Parse the name and get a moniker:
  30. ULONG ulEaten;
  31. IMoniker* pmk;
  32. hr = MkParseDisplayName(pbc, sz, &ulEaten, &pmk);
  33. if (SUCCEEDED(hr))
  34. {
  35. IClassFactory *pcf;
  36. hr = pmk->BindToObject(pbc, NULL, IID_PPV_ARG(IClassFactory, &pcf));
  37. if (SUCCEEDED(hr))
  38. {
  39. hr = pcf->CreateInstance(NULL, riid, ppv);
  40. pcf->Release();
  41. }
  42. pmk->Release();
  43. }
  44. pbc->Release();
  45. }
  46. return hr;
  47. }
  48. HRESULT CoCreateInSession(ULONG ulSessId, REFCLSID rclsid, DWORD dwClsContext, REFIID riid, void** ppv)
  49. {
  50. *ppv = NULL;
  51. IBindCtx* pbc;
  52. HRESULT hr = CreateBindCtx(0, &pbc);
  53. if (SUCCEEDED(hr))
  54. {
  55. // Form display name string
  56. WCHAR szDisplayName[64];
  57. if (-1 == ulSessId)
  58. {
  59. lstrcpyW(szDisplayName, L"Session:Console");
  60. }
  61. else
  62. {
  63. wsprintfW(szDisplayName, L"Session:%d", ulSessId);
  64. }
  65. // Parse the name and get a moniker:
  66. ULONG ulEaten;
  67. IMoniker* pmk;
  68. hr = MkParseDisplayName(pbc, szDisplayName, &ulEaten, &pmk);
  69. if (SUCCEEDED(hr))
  70. {
  71. IClassActivator* pclsact;
  72. hr = pmk->QueryInterface(IID_PPV_ARG(IClassActivator, &pclsact));
  73. if (SUCCEEDED(hr))
  74. {
  75. IClassFactory* pcf;
  76. hr = pclsact->GetClassObject(rclsid, dwClsContext, GetSystemDefaultLCID(), IID_PPV_ARG(IClassFactory, &pcf));
  77. if (SUCCEEDED(hr))
  78. {
  79. hr = pcf->CreateInstance(NULL, riid, ppv);
  80. pcf->Release();
  81. }
  82. pclsact->Release();
  83. }
  84. pmk->Release();
  85. }
  86. pbc->Release();
  87. }
  88. return hr;
  89. }
  90. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  91. {
  92. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  93. HRESULT hr;
  94. // Sleep(5 * 1000);
  95. IUserNotification *pun;
  96. //hr = CoCreateInstance(CLSID_UserNotification, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IUserNotification, &pun));
  97. //hr = CoCreateInstance(CLSID_UserNotification, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARG(IUserNotification, &pun));
  98. //hr = CoCreateInSession(WTSGetActiveConsoleSessionId(), CLSID_UserNotification, CLSCTX_LOCAL_SERVER, IID_PPV_ARG(IUserNotification, &pun));
  99. //hr = CoCreateOnConsoleWithBindToObject(CLSID_UserNotification, IID_PPV_ARG(IUserNotification, &pun));
  100. hr = CoCreateInSession((ULONG)-1, CLSID_UserNotification, CLSCTX_LOCAL_SERVER, IID_PPV_ARG(IUserNotification, &pun));
  101. if (SUCCEEDED(hr))
  102. {
  103. pun->PlaySound(L"SystemExit");
  104. pun->SetBalloonRetry(120 * 1000, 0, 0);
  105. WCHAR szTitle[128];
  106. wsprintfW(szTitle, L"ProcessID: %d, ActiveConsoleID: %d", GetCurrentProcessId(), WTSGetActiveConsoleSessionId());
  107. pun->SetIconInfo(LoadIcon(NULL, IDI_WINLOGO), szTitle);
  108. pun->SetBalloonInfo(szTitle, L"Balloon Text... 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 ", NIIF_INFO);
  109. hr = pun->Show(NULL, 0);
  110. pun->Release();
  111. }
  112. else
  113. {
  114. TCHAR szErr[128];
  115. wsprintf(szErr, TEXT("hr:%x"), hr);
  116. MessageBox(NULL, TEXT("Error"), szErr, MB_OK);
  117. }
  118. CoUninitialize();
  119. return 0;
  120. }