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.

532 lines
12 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: hsz.c
  3. *
  4. * Copyright (c) 1985 - 1999, Microsoft Corporation
  5. *
  6. * HSZ.C - DDEML String handle functions
  7. *
  8. * History:
  9. * 10-28-91 Sanfords Created
  10. \***************************************************************************/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. /***************************************************************************\
  14. * DdeCreateStringHandle (DDEML API)
  15. *
  16. * Description:
  17. * Create an HSZ from a string.
  18. *
  19. * History:
  20. * 11-1-91 sanfords Created.
  21. \***************************************************************************/
  22. FUNCLOG3(LOG_GENERAL, HSZ, DUMMYCALLINGTYPE, DdeCreateStringHandleA, DWORD, idInst, LPCSTR, psz, int, iCodePage)
  23. HSZ DdeCreateStringHandleA(
  24. DWORD idInst,
  25. LPCSTR psz,
  26. int iCodePage)
  27. {
  28. if (iCodePage == 0) {
  29. iCodePage = CP_WINANSI;
  30. }
  31. return (InternalDdeCreateStringHandle(idInst, (PVOID)psz, iCodePage));
  32. }
  33. FUNCLOG3(LOG_GENERAL, HSZ, DUMMYCALLINGTYPE, DdeCreateStringHandleW, DWORD, idInst, LPCWSTR, psz, int, iCodePage)
  34. HSZ DdeCreateStringHandleW(
  35. DWORD idInst,
  36. LPCWSTR psz,
  37. int iCodePage)
  38. {
  39. if (iCodePage == 0) {
  40. iCodePage = CP_WINUNICODE;
  41. }
  42. return (InternalDdeCreateStringHandle(idInst, (PVOID)psz, iCodePage));
  43. }
  44. HSZ InternalDdeCreateStringHandle(
  45. DWORD idInst,
  46. PVOID psz,
  47. int iCodePage)
  48. {
  49. PCL_INSTANCE_INFO pcii;
  50. HSZ hszRet = 0;
  51. EnterDDECrit;
  52. pcii = ValidateInstance((HANDLE)LongToHandle( idInst ));
  53. if (pcii == NULL) {
  54. BestSetLastDDEMLError(DMLERR_INVALIDPARAMETER);
  55. goto Exit;
  56. }
  57. switch (iCodePage) {
  58. case CP_WINANSI:
  59. if (*(LPSTR)psz == '\0') {
  60. goto Exit;
  61. }
  62. hszRet = NORMAL_HSZ_FROM_LATOM(AddAtomA((LPSTR)psz));
  63. break;
  64. case CP_WINUNICODE:
  65. if (*(LPWSTR)psz == L'\0') {
  66. goto Exit;
  67. }
  68. hszRet = NORMAL_HSZ_FROM_LATOM(AddAtomW((LPWSTR)psz));
  69. break;
  70. default:
  71. BestSetLastDDEMLError(DMLERR_INVALIDPARAMETER);
  72. break;
  73. }
  74. MONHSZ(pcii, hszRet, MH_CREATE);
  75. Exit:
  76. LeaveDDECrit;
  77. return (hszRet);
  78. }
  79. /***************************************************************************\
  80. * DdeQueryString (DDEML API)
  81. *
  82. * Description:
  83. * Recall the string associated with an HSZ.
  84. *
  85. * History:
  86. * 11-1-91 sanfords Created.
  87. \***************************************************************************/
  88. FUNCLOG5(LOG_GENERAL, DWORD, DUMMYCALLINGTYPE, DdeQueryStringA, DWORD, idInst, HSZ, hsz, LPSTR, psz, DWORD, cchMax, INT, iCodePage)
  89. DWORD DdeQueryStringA(
  90. DWORD idInst,
  91. HSZ hsz,
  92. LPSTR psz,
  93. DWORD cchMax,
  94. INT iCodePage)
  95. {
  96. if (iCodePage == 0) {
  97. iCodePage = CP_WINANSI;
  98. }
  99. return (InternalDdeQueryString(idInst, hsz, psz, cchMax, iCodePage));
  100. }
  101. FUNCLOG5(LOG_GENERAL, DWORD, DUMMYCALLINGTYPE, DdeQueryStringW, DWORD, idInst, HSZ, hsz, LPWSTR, psz, DWORD, cchMax, INT, iCodePage)
  102. DWORD DdeQueryStringW(
  103. DWORD idInst,
  104. HSZ hsz,
  105. LPWSTR psz,
  106. DWORD cchMax,
  107. INT iCodePage)
  108. {
  109. if (iCodePage == 0) {
  110. iCodePage = CP_WINUNICODE;
  111. }
  112. return (InternalDdeQueryString(idInst, hsz, psz, cchMax * sizeof(WCHAR), iCodePage));
  113. }
  114. DWORD InternalDdeQueryString(
  115. DWORD idInst,
  116. HSZ hsz,
  117. PVOID psz,
  118. DWORD cbMax,
  119. INT iCodePage)
  120. {
  121. PCL_INSTANCE_INFO pcii;
  122. DWORD dwRet = 0;
  123. WCHAR szw[256];
  124. // BOOL fDefUsed; // LATER
  125. EnterDDECrit;
  126. pcii = ValidateInstance((HANDLE)LongToHandle( idInst ));
  127. if (pcii == NULL) {
  128. BestSetLastDDEMLError(DMLERR_INVALIDPARAMETER);
  129. goto Exit;
  130. }
  131. if (ValidateHSZ(hsz) == HSZT_INVALID) {
  132. SetLastDDEMLError(pcii, DMLERR_INVALIDPARAMETER);
  133. goto Exit;
  134. }
  135. if (LATOM_FROM_HSZ(hsz) == 0) {
  136. if (iCodePage == CP_WINUNICODE) {
  137. if (psz != NULL) {
  138. *(LPWSTR)psz = L'\0';
  139. }
  140. dwRet = sizeof(WCHAR);
  141. goto Exit;
  142. } else {
  143. if (psz != NULL) {
  144. *(LPSTR)psz = '\0';
  145. }
  146. dwRet = sizeof(CHAR);
  147. goto Exit;
  148. }
  149. }
  150. if (psz == NULL) {
  151. cbMax = sizeof(szw);
  152. psz = (PVOID)szw;
  153. }
  154. switch (iCodePage) {
  155. case CP_WINANSI:
  156. dwRet = GetAtomNameA(LATOM_FROM_HSZ(hsz), psz, cbMax);
  157. break;
  158. default:
  159. dwRet = GetAtomNameW(LATOM_FROM_HSZ(hsz), (LPWSTR)psz, cbMax / sizeof(WCHAR));
  160. if (iCodePage != CP_WINUNICODE) {
  161. /*
  162. * convert psz to the appropriate codepage and count the
  163. * characters(ie BYTES for DBCS!) to alter dwRet.
  164. */
  165. #ifdef LATER
  166. // Does this routine work in place? (i.e. input and output buffer the same).
  167. WideCharToMultiByte((UINT)iCodePage, 0, szw,
  168. sizeof(szw) / sizeof(WCHAR),
  169. (LPSTR)psz, cbMax, NULL, &fDefUsed);
  170. #endif
  171. dwRet = cbMax + 1;
  172. }
  173. break;
  174. }
  175. Exit:
  176. LeaveDDECrit;
  177. return (dwRet);
  178. }
  179. /***************************************************************************\
  180. * DdeFreeStringHandle (DDEML API)
  181. *
  182. * Description:
  183. * Decrement the use count of an HSZ.
  184. *
  185. * History:
  186. * 11-1-91 sanfords Created.
  187. \***************************************************************************/
  188. FUNCLOG2(LOG_GENERAL, BOOL, DUMMYCALLINGTYPE, DdeFreeStringHandle, DWORD, idInst, HSZ, hsz)
  189. BOOL DdeFreeStringHandle(
  190. DWORD idInst,
  191. HSZ hsz)
  192. {
  193. PCL_INSTANCE_INFO pcii;
  194. BOOL fRet = FALSE;
  195. EnterDDECrit;
  196. pcii = ValidateInstance((HANDLE)LongToHandle( idInst ));
  197. if (pcii == NULL) {
  198. BestSetLastDDEMLError(DMLERR_INVALIDPARAMETER);
  199. goto Exit;
  200. }
  201. if (ValidateHSZ(hsz) == HSZT_INVALID) {
  202. SetLastDDEMLError(pcii, DMLERR_INVALIDPARAMETER);
  203. goto Exit;
  204. }
  205. MONHSZ(pcii, hsz, MH_DELETE);
  206. fRet = TRUE;
  207. if (LATOM_FROM_HSZ(hsz) != 0) {
  208. if (DeleteAtom(LATOM_FROM_HSZ(hsz))) {
  209. SetLastDDEMLError(pcii, DMLERR_INVALIDPARAMETER);
  210. fRet = FALSE;
  211. }
  212. }
  213. Exit:
  214. LeaveDDECrit;
  215. return (fRet);
  216. }
  217. /***************************************************************************\
  218. * DdeKeepStringHandle (DDEML API)
  219. *
  220. * Description:
  221. * Increments the use count of an HSZ.
  222. *
  223. * History:
  224. * 11-1-91 sanfords Created.
  225. \***************************************************************************/
  226. FUNCLOG2(LOG_GENERAL, BOOL, DUMMYCALLINGTYPE, DdeKeepStringHandle, DWORD, idInst, HSZ, hsz)
  227. BOOL DdeKeepStringHandle(
  228. DWORD idInst,
  229. HSZ hsz)
  230. {
  231. PCL_INSTANCE_INFO pcii;
  232. BOOL fRet = FALSE;
  233. EnterDDECrit;
  234. pcii = ValidateInstance((HANDLE)LongToHandle( idInst ));
  235. if (pcii == NULL) {
  236. BestSetLastDDEMLError(DMLERR_INVALIDPARAMETER);
  237. goto Exit;
  238. }
  239. if (ValidateHSZ(hsz) == HSZT_INVALID) {
  240. SetLastDDEMLError(pcii, DMLERR_INVALIDPARAMETER);
  241. goto Exit;
  242. }
  243. if (LATOM_FROM_HSZ(hsz) == 0) {
  244. fRet = TRUE;
  245. goto Exit;
  246. }
  247. MONHSZ(pcii, hsz, MH_KEEP);
  248. fRet = IncLocalAtomCount(LATOM_FROM_HSZ(hsz)) ? TRUE : FALSE;
  249. Exit:
  250. LeaveDDECrit;
  251. return (fRet);
  252. }
  253. /***************************************************************************\
  254. * DdeCmpStringHandles (DDEML API)
  255. *
  256. * Description:
  257. * Useless comparison of hszs. Provided for case sensitivity expandability.
  258. * Direct comparison of hszs would be a case sensitive comparison while
  259. * using this function would be case-insensitive. For now both ways are ==.
  260. *
  261. * History:
  262. * 11-1-91 sanfords Created.
  263. \***************************************************************************/
  264. FUNCLOG2(LOG_GENERAL, int, DUMMYCALLINGTYPE, DdeCmpStringHandles, HSZ, hsz1, HSZ, hsz2)
  265. int DdeCmpStringHandles(
  266. HSZ hsz1,
  267. HSZ hsz2)
  268. {
  269. if (hsz2 > hsz1) {
  270. return (-1);
  271. } else if (hsz2 < hsz1) {
  272. return (1);
  273. } else {
  274. return (0);
  275. }
  276. }
  277. /***************************************************************************\
  278. * ValidateHSZ
  279. *
  280. * Description:
  281. * Verifies the probability of a reasonable hsz
  282. *
  283. * History:
  284. * 11-1-91 sanfords Created.
  285. \***************************************************************************/
  286. DWORD ValidateHSZ(
  287. HSZ hsz)
  288. {
  289. if (hsz == 0) {
  290. return (HSZT_NORMAL);
  291. }
  292. if (LOWORD((ULONG_PTR)hsz) < 0xC000) {
  293. return (HSZT_INVALID);
  294. }
  295. if (HIWORD((ULONG_PTR)hsz) == 0) {
  296. return (HSZT_NORMAL);
  297. }
  298. if (HIWORD((ULONG_PTR)hsz) == 1) {
  299. return (HSZT_INST_SPECIFIC);
  300. }
  301. return (HSZT_INVALID);
  302. }
  303. /***************************************************************************\
  304. * MakeInstSpecificAtom
  305. *
  306. * Description:
  307. * Creates a new atom that has hwnd imbeded into it.
  308. *
  309. * History:
  310. * 11-1-91 sanfords Created.
  311. \***************************************************************************/
  312. LATOM MakeInstSpecificAtom(
  313. LATOM la,
  314. HWND hwnd)
  315. {
  316. WCHAR sz[256];
  317. LPWSTR psz;
  318. if (GetAtomName(la, sz, 256) == 0) {
  319. return (0);
  320. }
  321. #ifdef UNICODE
  322. psz = sz + wcslen(sz);
  323. #else
  324. psz = sz + strlen(sz);
  325. #endif
  326. wsprintf(psz, TEXT("(%#p)"), hwnd);
  327. la = AddAtom(sz);
  328. return (la);
  329. }
  330. /***************************************************************************\
  331. * ParseInstSpecificAtom
  332. *
  333. * Description:
  334. * Extracts the hwnd value out of the atom.
  335. *
  336. * History:
  337. * 11-1-91 sanfords Created.
  338. \***************************************************************************/
  339. HWND ParseInstSpecificAtom(
  340. LATOM la,
  341. LATOM *plaNormal)
  342. {
  343. CHAR sz[256];
  344. LPSTR pszHwnd;
  345. HWND hwnd;
  346. /*
  347. * LATER- NEED TO MAKE THIS UNICODE BASED WHEN WE GET A SCANF WE CAN USE
  348. */
  349. if (GetAtomNameA(la, sz, 256) == 0) {
  350. return (0);
  351. }
  352. pszHwnd = strrchr(sz, '(');
  353. if (pszHwnd == NULL) {
  354. return (0);
  355. }
  356. if (sscanf(pszHwnd, "(%#p)", &hwnd) != 1) {
  357. return (0);
  358. }
  359. if (plaNormal != NULL) {
  360. *pszHwnd = '\0';
  361. *plaNormal = AddAtomA(sz);
  362. }
  363. return (hwnd);
  364. }
  365. /***************************************************************************\
  366. * LocalToGlobalAtom
  367. *
  368. * Description:
  369. * Converts a Local Atom to a Global Atom
  370. *
  371. * History:
  372. * 12-1-91 sanfords Created.
  373. \***************************************************************************/
  374. GATOM LocalToGlobalAtom(
  375. LATOM la)
  376. {
  377. WCHAR sz[256];
  378. if (la == 0) {
  379. return (0);
  380. }
  381. if (GetAtomName((ATOM)la, sz, 256) == 0) {
  382. RIPMSG0(RIP_WARNING, "LocalToGlobalAtom out of memory");
  383. return (0);
  384. }
  385. return ((GATOM)GlobalAddAtom(sz));
  386. }
  387. /***************************************************************************\
  388. * GlobalToLocalAtom
  389. *
  390. * Description:
  391. * Converts a Global Atom to a Local Atom
  392. *
  393. * History:
  394. * 12-1-91 sanfords Created.
  395. \***************************************************************************/
  396. LATOM GlobalToLocalAtom(
  397. GATOM ga)
  398. {
  399. WCHAR sz[256];
  400. if (ga == 0) {
  401. return (0);
  402. }
  403. if (GlobalGetAtomName((ATOM)ga, sz, 256) == 0) {
  404. RIPMSG0(RIP_WARNING, "GlobalToLocalAtom out of memory");
  405. return (0);
  406. }
  407. return ((LATOM)AddAtom(sz));
  408. }
  409. /***************************************************************************\
  410. * IncGlobalAtomCount
  411. *
  412. * Description:
  413. * Duplicates an atom.
  414. *
  415. *
  416. * History:
  417. * 1-22-91 sanfords Created.
  418. \***************************************************************************/
  419. GATOM IncGlobalAtomCount(
  420. GATOM ga)
  421. {
  422. WCHAR sz[256];
  423. if (ga == 0) {
  424. return (0);
  425. }
  426. if (GlobalGetAtomName(ga, sz, 256) == 0) {
  427. RIPMSG0(RIP_WARNING, "IncGlobalAtomCount out of memory");
  428. return (0);
  429. }
  430. return ((GATOM)GlobalAddAtom(sz));
  431. }
  432. /***************************************************************************\
  433. * IncGlobalAtomCount
  434. *
  435. * Description:
  436. * Duplicates an atom.
  437. *
  438. *
  439. * History:
  440. * 1-22-91 sanfords Created.
  441. \***************************************************************************/
  442. LATOM IncLocalAtomCount(
  443. LATOM la)
  444. {
  445. WCHAR sz[256];
  446. if (la == 0) {
  447. return (0);
  448. }
  449. if (GetAtomName(la, sz, 256) == 0) {
  450. RIPMSG0(RIP_WARNING, "IncLocalAtomCount out of memory");
  451. return (0);
  452. }
  453. return ((LATOM)AddAtom(sz));
  454. }