edwin.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifdef _WIN32
  14. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  15. #include <windows.h>
  16. #include "edwin.h"
  17. __declspec(thread) HWND gt_hWndThunking = 0;
  18. int g_hThread = 0;
  19. ATOM g_hWinClass = 0;
  20. HANDLE g_hModule = 0;
  21. void *GetThunkingHandle()
  22. {
  23. //assert(0);
  24. if (gt_hWndThunking == 0 && g_hWinClass)
  25. {
  26. gt_hWndThunking = CreateWindow("edwin","edwin",0,0,0,0,0,0,0, GetModuleHandle(NULL),(LPVOID)4242);
  27. }
  28. return (void *)gt_hWndThunking;
  29. }
  30. typedef int (*PFN_THUNK_CLIENT)(void *);
  31. void ThunkToClientThread(void *hThunk, PFN_THUNK_CLIENT fn, void *data)
  32. {
  33. ::PostMessage((HWND)hThunk, (WM_USER + 4242), (WPARAM)fn, (LPARAM) data);
  34. }
  35. LRESULT CALLBACK edwinProc
  36. (
  37. HWND hwnd, // handle to window
  38. UINT uMsg, // message identifier
  39. WPARAM wParam, // first message parameter
  40. LPARAM lParam // second message parameter
  41. )
  42. {
  43. switch (uMsg)
  44. {
  45. case (WM_USER + 4242):
  46. {
  47. if (wParam)
  48. {
  49. ((PFN_THUNK_CLIENT)(wParam))((void*)lParam);
  50. }
  51. return 0;
  52. }
  53. default:
  54. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  55. }
  56. return 0;
  57. }
  58. BOOL APIENTRY DllMain
  59. (
  60. HANDLE hModule,
  61. DWORD ul_reason_for_call,
  62. LPVOID lpReserved
  63. )
  64. {
  65. //assert(0);
  66. switch (ul_reason_for_call)
  67. {
  68. case DLL_PROCESS_DETACH:
  69. {
  70. UnregisterClass("edwin", GetModuleHandle(NULL));
  71. }
  72. //fall through
  73. case DLL_THREAD_DETACH:
  74. // if (gt_hWndThunking != 0)
  75. // {
  76. // DestroyWindow(gt_hWndThunking);
  77. // gt_hWndThunking = 0;
  78. // }
  79. break;
  80. case DLL_PROCESS_ATTACH:
  81. {
  82. WNDCLASS edwinClass;
  83. memset(&edwinClass, 0, sizeof(edwinClass));
  84. edwinClass.hInstance = GetModuleHandle(NULL);
  85. edwinClass.lpfnWndProc = edwinProc;
  86. edwinClass.lpszClassName = "edwin";
  87. g_hWinClass = RegisterClass(&edwinClass);
  88. g_hModule = hModule;
  89. }
  90. //fall through
  91. case DLL_THREAD_ATTACH:
  92. {
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. return TRUE;
  99. }
  100. int GetResourceData(const char *restype, int resid, void *&data, unsigned &len)
  101. {
  102. HRSRC hRsrc = ::FindResource((HINSTANCE)g_hModule, MAKEINTRESOURCE(resid), restype);
  103. if (hRsrc != NULL)
  104. {
  105. len = ::SizeofResource((HINSTANCE)g_hModule, hRsrc);
  106. if (len > 0)
  107. {
  108. HGLOBAL hResData = ::LoadResource((HINSTANCE)g_hModule, hRsrc);
  109. if (hResData != NULL)
  110. {
  111. data = ::LockResource(hResData);
  112. }
  113. }
  114. }
  115. return 0;
  116. }
  117. #endif//_WIN32