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.

389 lines
8.9 KiB

  1. #include <windows.h>
  2. #include "common.h"
  3. #define BLOCKLEN 100
  4. #ifndef DBCS
  5. #define AnsiNext(x) ((x)+1)
  6. #endif
  7. extern HANDLE hInstance;
  8. extern FARPROC lpOldHook;
  9. extern HWND hWndMain, hWndHelp;
  10. extern WORD wHelpMain;
  11. extern char *pszLongName;
  12. extern char *pszOutOfMemory;
  13. HANDLE NEAR PASCAL StringToLocalHandle(LPSTR szStr, WORD wFlags)
  14. {
  15. HANDLE hStr;
  16. LPSTR lpStr;
  17. if(!(hStr=LocalAlloc(wFlags, lstrlen(szStr) + 1)))
  18. goto Error1;
  19. if(!(lpStr=LocalLock(hStr)))
  20. goto Error2;
  21. lstrcpy(lpStr, szStr);
  22. LocalUnlock(hStr);
  23. goto Error1;
  24. Error2:
  25. LocalFree(hStr);
  26. hStr = NULL;
  27. Error1:
  28. return(hStr);
  29. }
  30. LPSTR NEAR _fastcall MyStrTok(LPSTR szList, char cEnd)
  31. {
  32. LPSTR szTemp;
  33. /* if there are no more tokens return NULL */
  34. if(!*szList)
  35. return NULL;
  36. /* find delimiter or end of string */
  37. while(*szList && *szList!=cEnd)
  38. szList = AnsiNext(szList);
  39. /* if we found a delimiter insert string terminator and skip */
  40. if(*szList) {
  41. szTemp = szList;
  42. szList = AnsiNext(szTemp);
  43. *szTemp = '\0';
  44. }
  45. /* return token */
  46. return(szList);
  47. }
  48. int NEAR PASCAL DoDialogBoxParam(LPCSTR lpDialog, HWND hWnd, FARPROC lpfnProc,
  49. DWORD dwParam)
  50. {
  51. int result = -1;
  52. if(!(lpfnProc = MakeProcInstance(lpfnProc, hInstance)))
  53. goto Error1;
  54. result = DialogBoxParam(hInstance, lpDialog, hWnd, lpfnProc, dwParam);
  55. FreeProcInstance(lpfnProc);
  56. Error1:
  57. return(result);
  58. }
  59. int NEAR PASCAL DoDialogBox(LPCSTR lpDialog, HWND hWnd, FARPROC lpfnProc)
  60. {
  61. return(DoDialogBoxParam(lpDialog, hWnd, lpfnProc, 0L));
  62. }
  63. unsigned long NEAR PASCAL MyQueryValue(HKEY hKey, PSTR pSubKey, HANDLE *hBuf)
  64. {
  65. HANDLE hTemp;
  66. PSTR pBuf;
  67. WORD wBufSize = BLOCKLEN;
  68. unsigned long result = ERROR_OUTOFMEMORY;
  69. LONG lSize;
  70. if(!(*hBuf=LocalAlloc(LMEM_MOVEABLE, wBufSize)))
  71. goto Error1;
  72. if(!(pBuf=LocalLock(*hBuf)))
  73. goto Error2;
  74. while((lSize=wBufSize, (result=RegQueryValue(hKey, pSubKey, pBuf, &lSize))
  75. ==ERROR_SUCCESS) && (WORD)lSize>wBufSize-10) {
  76. LocalUnlock(*hBuf);
  77. wBufSize += BLOCKLEN;
  78. if(!(hTemp=LocalReAlloc(*hBuf, wBufSize, LMEM_MOVEABLE))) {
  79. result = ERROR_OUTOFMEMORY;
  80. goto Error2;
  81. }
  82. pBuf = LocalLock(*hBuf=hTemp);
  83. }
  84. LocalUnlock(*hBuf);
  85. if(result!=ERROR_SUCCESS || !lSize)
  86. goto Error2;
  87. goto Error1;
  88. Error2:
  89. LocalFree(*hBuf);
  90. *hBuf = NULL;
  91. Error1:
  92. return(result);
  93. }
  94. HANDLE NEAR PASCAL GetEditString(HWND hWndEdit)
  95. {
  96. HANDLE hEdit = NULL;
  97. PSTR pEdit;
  98. WORD wLen;
  99. wLen = LOWORD(SendMessage(hWndEdit, WM_GETTEXTLENGTH, 0, 0L)) + 1;
  100. if(!(hEdit=LocalAlloc(LMEM_MOVEABLE, wLen)))
  101. goto Error1;
  102. if(!(pEdit=LocalLock(hEdit)))
  103. goto Error2;
  104. SendMessage(hWndEdit, WM_GETTEXT, wLen, (DWORD)((LPSTR)pEdit));
  105. LocalUnlock(hEdit);
  106. goto Error1;
  107. Error2:
  108. LocalFree(hEdit);
  109. hEdit = NULL;
  110. Error1:
  111. return(hEdit);
  112. }
  113. HANDLE NEAR _fastcall MyLoadString(WORD wId, WORD *pwSize, WORD wFlags)
  114. {
  115. char szString[258]; /* RC limits strings to 256 chars */
  116. WORD wSize;
  117. wSize = LoadString(hInstance, wId, szString, sizeof(szString));
  118. if(pwSize)
  119. *pwSize = wSize;
  120. return(StringToLocalHandle(szString, wFlags));
  121. }
  122. int NEAR cdecl MyMessageBox(HWND hWnd, WORD wText, WORD wType, WORD wExtra, ...)
  123. {
  124. HANDLE hText, hRText;
  125. PSTR pText, pRText;
  126. WORD wSize;
  127. int result = 0;
  128. if(wText == IDS_OUTOFMEMORY)
  129. goto Error1;
  130. if(!(hText=MyLoadString(wText, &wSize, LMEM_MOVEABLE)))
  131. goto Error1;
  132. /* We allocate enough room for a bunch of numbers and the strings
  133. */
  134. if(!(hRText=LocalAlloc(LMEM_MOVEABLE, 2*wSize + wExtra)))
  135. goto Error2;
  136. if(!(pRText=LocalLock(hRText)))
  137. goto Error3;
  138. pText = LocalLock(hText);
  139. wvsprintf(pRText, pText, (LPSTR)(&wExtra+1));
  140. result = MessageBox(hWnd, pRText, pszLongName, wType);
  141. LocalUnlock(hText);
  142. LocalUnlock(hRText);
  143. Error3:
  144. LocalFree(hRText);
  145. Error2:
  146. LocalFree(hText);
  147. Error1:
  148. if(!result) {
  149. MessageBox(hWnd, pszOutOfMemory, pszLongName,
  150. MB_ICONHAND | MB_SYSTEMMODAL | MB_OK);
  151. }
  152. return(result);
  153. }
  154. VOID NEAR PASCAL WriteProfileInt(WORD wAppName, WORD wKey, int nVal)
  155. {
  156. HANDLE hAppName, hKey;
  157. char buf[10];
  158. if(!(hAppName=MyLoadString(wAppName, NULL, LMEM_MOVEABLE)))
  159. goto Error1;
  160. if(!(hKey=MyLoadString(wKey, NULL, LMEM_MOVEABLE)))
  161. goto Error2;
  162. wsprintf(buf, "%d", nVal);
  163. WriteProfileString(LocalLock(hAppName), LocalLock(hKey), buf);
  164. LocalUnlock(hKey);
  165. LocalUnlock(hAppName);
  166. Error2:
  167. LocalFree(hKey);
  168. Error1:
  169. LocalFree(hAppName);
  170. }
  171. int NEAR PASCAL MyGetProfileInt(WORD wAppName, WORD wKey, int nDefault)
  172. {
  173. HANDLE hAppName, hKey;
  174. if(!(hAppName=MyLoadString(wAppName, NULL, LMEM_MOVEABLE)))
  175. goto Error1;
  176. if(!(hKey=MyLoadString(wKey, NULL, LMEM_MOVEABLE)))
  177. goto Error2;
  178. nDefault = GetProfileInt(LocalLock(hAppName), LocalLock(hKey), nDefault);
  179. LocalUnlock(hKey);
  180. LocalUnlock(hAppName);
  181. Error2:
  182. LocalFree(hKey);
  183. Error1:
  184. LocalFree(hAppName);
  185. return(nDefault);
  186. }
  187. HANDLE NEAR PASCAL StringToHandle(LPSTR szStr)
  188. {
  189. HANDLE hStr;
  190. LPSTR lpStr;
  191. if(!(hStr=GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
  192. (DWORD)(lstrlen(szStr)+1))))
  193. goto Error1;
  194. if(!(lpStr=GlobalLock(hStr)))
  195. goto Error2;
  196. lstrcpy(lpStr, szStr);
  197. GlobalUnlock(hStr);
  198. goto Error1;
  199. Error2:
  200. GlobalFree(hStr);
  201. hStr = NULL;
  202. Error1:
  203. return(hStr);
  204. }
  205. int FAR PASCAL MessageFilter(int nCode, WORD wParam, LPMSG lpMsg)
  206. {
  207. switch(nCode) {
  208. case MSGF_MENU:
  209. case MSGF_DIALOGBOX:
  210. if(lpMsg->message==WM_KEYDOWN && lpMsg->wParam==VK_F1
  211. && !(lpMsg->lParam&(1L<<30)))
  212. PostMessage(hWndHelp, WM_COMMAND, ID_HELP,
  213. MAKELONG(lpMsg->hwnd, nCode));
  214. break;
  215. default:
  216. DefHookProc(nCode, wParam, (DWORD)lpMsg, &lpOldHook);
  217. return(FALSE);
  218. }
  219. return(FALSE);
  220. }
  221. #ifndef NOHELP
  222. // #define ONLYID
  223. VOID NEAR PASCAL MyHelp(HWND hWnd, WORD wCommand, DWORD wId)
  224. {
  225. #ifdef ONLYID
  226. if(wCommand != HELP_QUIT)
  227. MyMessageBox(hWnd, IDS_HELP, MB_OK, 0, wId);
  228. #else
  229. HANDLE hHelpFile;
  230. PSTR pHelpFile;
  231. if(!(hHelpFile=MyLoadString(wHelpMain==IDW_SDKMAIN ?
  232. IDS_SDKHELPFILE : IDS_HELPFILE, NULL, LMEM_MOVEABLE)))
  233. return;
  234. if(!WinHelp(hWndMain, pHelpFile=LocalLock(hHelpFile), wCommand, wId))
  235. MyMessageBox(hWnd, IDS_HELPERR, MB_OK, 0);
  236. else
  237. WinHelp(hWndMain, pHelpFile, HELP_SETINDEX, wHelpIndex);
  238. LocalUnlock(hHelpFile);
  239. LocalFree(hHelpFile);
  240. #endif
  241. }
  242. #endif
  243. HANDLE NEAR PASCAL GetListboxString(HWND hWndEdit, int nId)
  244. {
  245. HANDLE hEdit = NULL;
  246. PSTR pEdit;
  247. WORD wLen;
  248. wLen = LOWORD(SendMessage(hWndEdit, LB_GETTEXTLEN, nId, 0L)) + 1;
  249. if(!(hEdit=LocalAlloc(LMEM_MOVEABLE, wLen)))
  250. goto Error1;
  251. if(!(pEdit=LocalLock(hEdit)))
  252. goto Error2;
  253. SendMessage(hWndEdit, LB_GETTEXT, nId, (DWORD)((LPSTR)pEdit));
  254. LocalUnlock(hEdit);
  255. goto Error1;
  256. Error2:
  257. LocalFree(hEdit);
  258. hEdit = NULL;
  259. Error1:
  260. return(hEdit);
  261. }
  262. unsigned long NEAR PASCAL MyEnumKey(HKEY hKey, WORD wIndex, HANDLE *hBuf)
  263. {
  264. HANDLE hTemp;
  265. PSTR pBuf;
  266. WORD wBufSize = BLOCKLEN, wSize;
  267. unsigned long result = ERROR_OUTOFMEMORY;
  268. if(!(*hBuf=LocalAlloc(LMEM_MOVEABLE, wBufSize)))
  269. goto Error1;
  270. if(!(pBuf=LocalLock(*hBuf)))
  271. goto Error2;
  272. while((result=RegEnumKey(hKey, wIndex, pBuf, (DWORD)wBufSize))
  273. ==ERROR_SUCCESS && (wSize=lstrlen(pBuf))>wBufSize-10) {
  274. LocalUnlock(*hBuf);
  275. wBufSize += BLOCKLEN;
  276. if(!(hTemp=LocalReAlloc(*hBuf, wBufSize, LMEM_MOVEABLE))) {
  277. result = ERROR_OUTOFMEMORY;
  278. goto Error2;
  279. }
  280. pBuf = LocalLock(*hBuf=hTemp);
  281. }
  282. LocalUnlock(*hBuf);
  283. if(result!=ERROR_SUCCESS || !wSize)
  284. goto Error2;
  285. goto Error1;
  286. Error2:
  287. LocalFree(*hBuf);
  288. *hBuf = NULL;
  289. Error1:
  290. return(result);
  291. }
  292. static WORD wErrMsgs[] = {
  293. 0, IDS_BADDB, IDS_BADKEY, IDS_CANTOPENDB, IDS_CANTREADDB, IDS_CANTWRITEDB,
  294. IDS_OUTOFMEMORY, IDS_INVALIDPARM
  295. } ;
  296. WORD NEAR _fastcall GetErrMsg(WORD wRet)
  297. {
  298. return(wRet>=sizeof(wErrMsgs)/sizeof(wErrMsgs[0]) ?
  299. IDS_INVALIDPARM : wErrMsgs[wRet]);
  300. }
  301. VOID NEAR PASCAL RepeatMove(LPSTR lpDest, LPSTR lpSrc, WORD wBytes)
  302. {
  303. /* WARNING: This assumes that the buffers are in different segments, or
  304. * the offset of the dest is less than the offset of the src
  305. */
  306. /* Save DS, and load up ES:DI, DS:SI, and CX with the parameters */
  307. _asm push ds
  308. _asm les di,lpDest
  309. _asm lds si,lpSrc
  310. _asm mov cx,wBytes
  311. _asm cld
  312. /* Do a movsb if CX is odd, and then do movsw for CX/2 */
  313. _asm shr CX,1
  314. _asm jnc repm1
  315. _asm movsb
  316. _asm repm1:
  317. _asm jcxz repm2
  318. _asm rep movsw
  319. _asm repm2:
  320. /* Restore DS and return */
  321. _asm pop ds
  322. }