Leaked source code of windows server 2003
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.

427 lines
13 KiB

  1. //////////////////////////////////////////////////////////////////
  2. // File : cpadsvr.cpp
  3. // Purpose : Client source code for IMEPad executable.
  4. //
  5. //
  6. // Date : Fri Apr 16 15:39:33 1999
  7. // Author : ToshiaK
  8. //
  9. // Copyright(c) 1995-1999, Microsoft Corp. All rights reserved
  10. //////////////////////////////////////////////////////////////////
  11. //----------------------------------------------------------------
  12. // Public static methods
  13. //
  14. // static BOOL OnProcessAttach(HINSTANCE hInst);
  15. // static BOOL OnProcessDetach(VOID);
  16. // static BOOL OnThreadAttach(VOID);
  17. // static BOOL OnThreadDetach(VOID);
  18. // static LPCImePadSvr GetCImePadSvr(VOID);
  19. // static LPCImePadSvr LoadCImePadSvr(VOID);
  20. // static LPCImePadSvr FecthCImePadSvr(VOID);
  21. // static VOID DestroyCImePadSvr(VOID);
  22. //
  23. //----------------------------------------------------------------
  24. #include <windows.h>
  25. #ifdef UNDER_CE // stub for CE
  26. #include "stub_ce.h"
  27. #endif // UNDER_CE
  28. #include "cpadsvr.h"
  29. #include "cpaddbg.h"
  30. #include "cpadsvrs.h" //Use Shared Memory for IPC.
  31. //----------------------------------------------------------------
  32. //misc definition
  33. //----------------------------------------------------------------
  34. #define Unref(a) UNREFERENCED_PARAMETER(a)
  35. //990812:ToshiaK For Win64. Use Global Alloc/Free Ptr.
  36. #include <windowsx.h>
  37. #define MemAlloc(a) GlobalAllocPtr(GMEM_FIXED, a)
  38. #define MemFree(a) GlobalFreePtr(a)
  39. //----------------------------------------------------------------
  40. //Static member initialize
  41. //----------------------------------------------------------------
  42. #define UNDEF_TLSINDEX 0xFFFFFFFF
  43. INT CImePadSvr::m_gdwTLSIndex = UNDEF_TLSINDEX; //Thread Local Strage initial value.
  44. HMODULE CImePadSvr::m_ghModClient = NULL; //Client Module handle.
  45. //----------------------------------------------------------------
  46. //OLE function is dynamically loaded/called
  47. //----------------------------------------------------------------
  48. #define SZMOD_OLE32DLL TEXT("OLE32.DLL")
  49. #define SZFN_COINITIALIZE "CoInitialize"
  50. #define SZFN_COCREATEINSTANCE "CoCreateInstance"
  51. #define SZFN_COUNINITIALIZE "CoUninitialize"
  52. #define SZFN_CODISCONNECTOBJECT "CoDisconnectObject"
  53. #define SZFN_COTASKMEMALLOC "CoTaskMemAlloc"
  54. #define SZFN_COTASKMEMREALLOC "CoTaskMemRealloc"
  55. #define SZFN_COTASKMEMFREE "CoTaskMemFree"
  56. #ifdef UNICODE
  57. #pragma message("UNICODE Unicode")
  58. #endif
  59. //////////////////////////////////////////////////////////////////
  60. // Function : CImePadSvr::OnProcessAttach
  61. // Type : BOOL
  62. // Purpose : Get thread local strage index.
  63. // Initialize static value.
  64. // Args :
  65. // : HINSTANCE hInst Caller's moudle handle.
  66. // Return :
  67. // DATE : Fri Apr 16 15:41:32 1999
  68. // Histroy :
  69. //////////////////////////////////////////////////////////////////
  70. BOOL
  71. CImePadSvr::OnProcessAttach(HINSTANCE hInst)
  72. {
  73. DBG(("CImePadSvr::OnProcessAttach START\n"));
  74. #ifdef _DEBUG
  75. DWORD dwPID = ::GetCurrentProcessId();
  76. DWORD dwTID = ::GetCurrentThreadId();
  77. DBG(("-->PID [0x%08x][%d] TID [0x%08x][%d]\n", dwPID, dwPID, dwTID, dwTID));
  78. #endif
  79. #ifdef _DEBUG
  80. if(m_ghModClient) {
  81. ::DebugBreak();
  82. }
  83. if(m_gdwTLSIndex != UNDEF_TLSINDEX) {
  84. ::DebugBreak();
  85. }
  86. #endif
  87. m_ghModClient = (HMODULE)hInst;
  88. m_gdwTLSIndex = ::TlsAlloc(); //Get new TLS index.
  89. if(m_gdwTLSIndex == UNDEF_TLSINDEX) {
  90. DBG(("-->OnPorcessAttach ::TlsAlloc Error ret [%d]\n", GetLastError()));
  91. }
  92. DBG(("-->OnProcessAttach() m_gdwTLSIndex[0x%08x][%d]\n", m_gdwTLSIndex, m_gdwTLSIndex));
  93. DBG(("CImePadSvr::OnProcessAttach END\n"));
  94. return TRUE;
  95. }
  96. //////////////////////////////////////////////////////////////////
  97. // Function : CImePadSvr::OnProcessDetach
  98. // Type : BOOL
  99. // Purpose : Delete all client instance.
  100. // We cannot call COM API in DLL_PROCESS_DETACH.
  101. // See DCOM mailing list article,
  102. // http://discuss.microsoft.com/SCRIPTS/WA-MSD.EXE?A2=ind9712a&L=dcom&F=&S=&P=20706
  103. // Args : None
  104. // Return :
  105. // DATE : Tue Apr 13 17:49:55 1999
  106. // Histroy :
  107. //////////////////////////////////////////////////////////////////
  108. BOOL
  109. CImePadSvr::OnProcessDetach(VOID)
  110. {
  111. DBG(("CImePadSvr::OnProcessDetach\n"));
  112. CImePadSvr::OnThreadDetach();
  113. if(!::TlsFree(m_gdwTLSIndex)) {
  114. DBG(("-->::TlsFree Error [%d]\n", GetLastError()));
  115. }
  116. m_gdwTLSIndex = UNDEF_TLSINDEX;
  117. DBG(("CImePadSvr::OnProcessDetach END\n"));
  118. return TRUE;
  119. }
  120. //////////////////////////////////////////////////////////////////
  121. // Function : CImePadSvr::OnThreadAttach
  122. // Type : BOOL
  123. // Purpose : Do Nothing.
  124. // Args : None
  125. // Return :
  126. // DATE : Mon May 17 21:37:16 1999
  127. // Histroy :
  128. //////////////////////////////////////////////////////////////////
  129. BOOL
  130. CImePadSvr::OnThreadAttach(VOID)
  131. {
  132. return TRUE;
  133. }
  134. //////////////////////////////////////////////////////////////////
  135. // Function : CImePadSvr::OnThreadDetach
  136. // Type : BOOL
  137. // Purpose :
  138. // Args : None
  139. // Return :
  140. // DATE : Mon May 17 21:38:06 1999
  141. // Histroy :
  142. //////////////////////////////////////////////////////////////////
  143. BOOL
  144. CImePadSvr::OnThreadDetach(VOID)
  145. {
  146. DBG(("CImePadSvr::OnThreadDetach\n"));
  147. #ifdef _DEBUG
  148. DWORD dwPID = ::GetCurrentProcessId();
  149. DWORD dwTID = ::GetCurrentThreadId();
  150. DBG(("-->PID [0x%08x][%d] TID [0x%08x][%d]\n", dwPID, dwPID, dwTID, dwTID));
  151. #endif
  152. LPCImePadSvr lpCImePadSvr = (LPCImePadSvr)::TlsGetValue(m_gdwTLSIndex);
  153. if(lpCImePadSvr) {
  154. DBG(("-->First Set TlsSetValue as NULL\n"));
  155. if(!::TlsSetValue(m_gdwTLSIndex, NULL)) {
  156. DBG(("TlsSetValue Failed\n"));
  157. }
  158. DBG(("-->Call ForceDisConnect() START\n"));
  159. lpCImePadSvr->ForceDisConnect();
  160. DBG(("-->Call ForceDisConnect() END\n"));
  161. delete lpCImePadSvr;
  162. }
  163. DBG(("CImePadSvr::OnThreadDetach END\n"));
  164. return TRUE;
  165. }
  166. //////////////////////////////////////////////////////////////////
  167. // Function : CImePadSvr::GetCImePadSvr
  168. // Type : LPCImePadSvr
  169. // Purpose : Get LPCImePadSvr pointer in current Thread.
  170. // Args : None
  171. // Return :
  172. // DATE : Mon May 17 21:41:46 1999
  173. // Histroy :
  174. //////////////////////////////////////////////////////////////////
  175. LPCImePadSvr
  176. CImePadSvr::GetCImePadSvr(VOID)
  177. {
  178. LPCImePadSvr lpCImePadSvr;
  179. if(m_gdwTLSIndex == UNDEF_TLSINDEX) {
  180. DBG(("-->CImePadSvr::GetCImePadSvr() Error, need TLS index\n"));
  181. #ifdef _DEBUG
  182. //DebugBreak();
  183. #endif
  184. return NULL;
  185. }
  186. lpCImePadSvr = (LPCImePadSvr)::TlsGetValue(m_gdwTLSIndex);
  187. return lpCImePadSvr;
  188. }
  189. //////////////////////////////////////////////////////////////////
  190. // Function : CImePadSvr::LoadCImePadSvr
  191. // Type : LPCImePadSvr
  192. // Purpose : Load LPCImePadSvr pointer in current thread.
  193. // Args : None
  194. // Return :
  195. // DATE : Mon May 17 21:42:17 1999
  196. // Histroy :
  197. //////////////////////////////////////////////////////////////////
  198. LPCImePadSvr
  199. CImePadSvr::LoadCImePadSvr(INT protocol)
  200. {
  201. LPCImePadSvr lpCImePadSvr;
  202. lpCImePadSvr = CImePadSvr::GetCImePadSvr();
  203. if(lpCImePadSvr) { //Already created in current thread.
  204. return lpCImePadSvr;
  205. }
  206. lpCImePadSvr = NULL;
  207. switch(protocol) {
  208. case CIMEPADSVR_COM:
  209. //lpCImePadSvr = new CImePadSvrCOM();
  210. break;
  211. case CIMEPADSVR_SHAREDMEM:
  212. lpCImePadSvr = new CImePadSvrSharemem();
  213. if(lpCImePadSvr) {
  214. if(!lpCImePadSvr->IsAvailable()) {
  215. delete lpCImePadSvr;
  216. lpCImePadSvr = NULL;
  217. }
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. if(!lpCImePadSvr) {
  224. DBG(("-->LoadCImePadSvr() Error Out of Memory?\n"));
  225. return NULL;
  226. }
  227. //Set new value to TLS.
  228. if(!::TlsSetValue(m_gdwTLSIndex, lpCImePadSvr)) {
  229. DBG(("-->LoadCImePadSvr() TlsSetValue Failed [%d]\n", GetLastError()));
  230. delete lpCImePadSvr;
  231. ::TlsSetValue(m_gdwTLSIndex, NULL);
  232. return NULL;
  233. }
  234. return lpCImePadSvr;
  235. }
  236. LPCImePadSvr
  237. CImePadSvr::FecthCImePadSvr(VOID)
  238. {
  239. return NULL;
  240. }
  241. VOID
  242. CImePadSvr::DestroyCImePadSvr(VOID)
  243. {
  244. DBG(("CImePadSvr::DestroyCImePadSvr START\n"));
  245. LPCImePadSvr lpCImePadSvr = GetCImePadSvr();
  246. if(!lpCImePadSvr) {
  247. DBG(("-->CImePadSvr::DestroyCImePadSvr() Already Destroyed or not instance\n"));
  248. DBG(("CImePadSvr::DestroyCImePadSvr END\n"));
  249. return;
  250. }
  251. lpCImePadSvr->Terminate(NULL);
  252. delete lpCImePadSvr;
  253. if(!::TlsSetValue(m_gdwTLSIndex, NULL)) {
  254. DBG(("-->TlsSetValue() error [%d]\n", GetLastError()));
  255. }
  256. DBG(("CImePadSvr::DestroyCImePadSvr END\n"));
  257. return;
  258. }
  259. //////////////////////////////////////////////////////////////////
  260. // Function : CImePadSvr::CImePadSvr
  261. // Type :
  262. // Purpose : Constructor of CImePadSvr
  263. // Args : None
  264. // Return :
  265. // DATE : Mon May 17 23:37:18 1999
  266. // Histroy :
  267. //////////////////////////////////////////////////////////////////
  268. CImePadSvr::CImePadSvr()
  269. {
  270. DBG(("CImePadSvr::CImePadSvr START\n"));
  271. m_fCoInitSuccess = FALSE; //Flag for CoInitialize() successed or not.
  272. m_fOLELoaded = FALSE; //OLE32.DLL is loaded by Application or explicitly loaded.
  273. m_hModOLE = FALSE; //OLE32.DLL module handle.
  274. m_fnCoInitialize = NULL; //CoInitialize() function pointer.
  275. m_fnCoCreateInstance = NULL; //CoCreateInstance() function pointer.
  276. m_fnCoUninitialize = NULL; //CoUninitialize() function pointer.
  277. m_fnCoDisconnectObject = NULL; //CoDisconnectObject() function pointer.
  278. m_fnCoTaskMemAlloc = NULL; //CoTaskMemAlloc() function pointer.
  279. m_fnCoTaskMemRealloc = NULL; //CoTaskMemRealloc() function pointer.
  280. m_fnCoTaskMemFree = NULL; //CoTaskMemFree() function pointer.
  281. DBG(("CImePadSvr::CImePadSvr END\n"));
  282. }
  283. CImePadSvr::~CImePadSvr()
  284. {
  285. DBG(("CImePadSvr::~CImePadSvr START\n"));
  286. m_fCoInitSuccess = FALSE; //Flag for CoInitialize() successed or not.
  287. m_fOLELoaded = FALSE; //OLE32.DLL is loaded by Application or explicitly loaded.
  288. m_hModOLE = FALSE; //OLE32.DLL module handle.
  289. m_fnCoInitialize = NULL; //CoInitialize() function pointer.
  290. m_fnCoCreateInstance = NULL; //CoCreateInstance() function pointer.
  291. m_fnCoUninitialize = NULL; //CoUninitialize() function pointer.
  292. m_fnCoDisconnectObject = NULL; //CoDisconnectObject() function pointer.
  293. m_fnCoTaskMemAlloc = NULL; //CoTaskMemAlloc() function pointer.
  294. m_fnCoTaskMemRealloc = NULL; //CoTaskMemRealloc() function pointer.
  295. m_fnCoTaskMemFree = NULL; //CoTaskMemFree() function pointer.
  296. DBG(("CImePadSvr::~CImePadSvr END\n"));
  297. }
  298. BOOL
  299. CImePadSvr::InitOleAPI(VOID)
  300. {
  301. DBG(("CImePadSvr::InitOleAPI START\n"));
  302. // Security Push: Dangerous API
  303. // Mofule name must be with full path.
  304. if(!m_hModOLE) {
  305. INT cbBufSize = (MAX_PATH+lstrlen(SZMOD_OLE32DLL)) * sizeof(TCHAR);
  306. LPTSTR lptstrOLE32FileName = (LPTSTR)MemAlloc(cbBufSize);
  307. if (lptstrOLE32FileName) {
  308. if (GetSystemDirectory(lptstrOLE32FileName, cbBufSize / sizeof(TCHAR))) {
  309. if (SUCCEEDED(StringCbCat(lptstrOLE32FileName, cbBufSize, TEXT("\\")))) {
  310. if (SUCCEEDED(StringCbCat(lptstrOLE32FileName, cbBufSize, SZMOD_OLE32DLL))) {
  311. m_hModOLE = ::GetModuleHandle(lptstrOLE32FileName);
  312. if(m_hModOLE) {
  313. DBG(("-->%s is Loaded by Application\n", SZMOD_OLE32DLL));
  314. m_fOLELoaded = FALSE;
  315. }
  316. else {
  317. m_hModOLE = ::LoadLibrary(lptstrOLE32FileName);
  318. }
  319. }
  320. }
  321. }
  322. MemFree(lptstrOLE32FileName);
  323. }
  324. }
  325. if(m_hModOLE) {
  326. DBG(("--> %s has Loaded Explicitly", SZMOD_OLE32DLL));
  327. m_fOLELoaded = TRUE;
  328. }
  329. else {
  330. return FALSE;
  331. }
  332. m_fnCoInitialize = (FN_COINITIALIZE) GetProcAddress(m_hModOLE, SZFN_COINITIALIZE);
  333. m_fnCoCreateInstance = (FN_COCREATEINSTANCE) ::GetProcAddress(m_hModOLE, SZFN_COCREATEINSTANCE);
  334. m_fnCoUninitialize = (FN_COUNINITIALIZE) ::GetProcAddress(m_hModOLE, SZFN_COUNINITIALIZE);
  335. m_fnCoDisconnectObject= (FN_CODISCONNECTOBJECT) ::GetProcAddress(m_hModOLE, SZFN_CODISCONNECTOBJECT);
  336. m_fnCoTaskMemAlloc = (FN_COTASKMEMALLOC) ::GetProcAddress(m_hModOLE, SZFN_COTASKMEMALLOC);
  337. m_fnCoTaskMemRealloc = (FN_COTASKMEMREALLOC) ::GetProcAddress(m_hModOLE, SZFN_COTASKMEMREALLOC);
  338. m_fnCoTaskMemFree = (FN_COTASKMEMFREE) ::GetProcAddress(m_hModOLE, SZFN_COTASKMEMFREE);
  339. if(!m_fnCoInitialize ||
  340. !m_fnCoCreateInstance ||
  341. !m_fnCoUninitialize ||
  342. !m_fnCoDisconnectObject ||
  343. !m_fnCoTaskMemAlloc ||
  344. !m_fnCoTaskMemRealloc ||
  345. !m_fnCoTaskMemFree) {
  346. DBG(("InitOleAPI Failed: GetProcAddress Error\n"));
  347. return FALSE;
  348. }
  349. DBG(("CImePadSvr::InitOleAPI END\n"));
  350. return TRUE;
  351. }
  352. BOOL
  353. CImePadSvr::TermOleAPI(VOID)
  354. {
  355. DBG(("CImePadSvr::TermOleAPI START\n"));
  356. m_fnCoInitialize = NULL;
  357. m_fnCoCreateInstance = NULL;
  358. m_fnCoUninitialize = NULL;
  359. m_fnCoDisconnectObject = NULL;
  360. m_fnCoTaskMemAlloc = NULL;
  361. m_fnCoTaskMemRealloc = NULL;
  362. m_fnCoTaskMemFree = NULL;
  363. if(!m_hModOLE) {
  364. DBG(("-->TermOleAPI already Terminated?\n"));
  365. return TRUE;
  366. }
  367. if(m_hModOLE && m_fOLELoaded) {
  368. DBG(("--> FreeLibrary\n"));
  369. ::FreeLibrary(m_hModOLE);
  370. }
  371. m_hModOLE = NULL;
  372. m_fOLELoaded = FALSE;
  373. DBG(("CImePadSvr::TermOleAPI END\n"));
  374. return TRUE;
  375. }
  376. VOID*
  377. CImePadSvr::operator new( size_t size )
  378. {
  379. LPVOID lp = (LPVOID)MemAlloc(size);
  380. return lp;
  381. }
  382. VOID
  383. CImePadSvr::operator delete( VOID *lp )
  384. {
  385. if(lp) {
  386. MemFree(lp);
  387. }
  388. return;
  389. }