StdAfx.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // stdafx.cpp : source file that includes just the standard includes
  2. // DBServer.pch will be the pre-compiled header
  3. // stdafx.obj will contain the pre-compiled type information
  4. #include "stdafx.h"
  5. /********************************************************************/
  6. /* */
  7. /* Function name : BrowseForFolder */
  8. /* Description : Browse for folder using SHBrowseForFolder. */
  9. /* */
  10. /********************************************************************/
  11. CString BrowseForFolder(HWND hWnd, LPCSTR lpszTitle, UINT nFlags)
  12. {
  13. // We're going to use the shell to display a
  14. // "Choose Directory" dialog box for the user.
  15. CString strResult = "";
  16. LPMALLOC lpMalloc;
  17. if (::SHGetMalloc(&lpMalloc) != NOERROR)
  18. {
  19. // failed to get allocator
  20. return strResult;
  21. }
  22. char szBuffer[_MAX_PATH];
  23. char szDisplayName[_MAX_PATH];
  24. BROWSEINFO browseInfo;
  25. browseInfo.hwndOwner = hWnd;
  26. // set root at Desktop
  27. browseInfo.pidlRoot = NULL;
  28. browseInfo.pszDisplayName = szDisplayName;
  29. browseInfo.lpszTitle = lpszTitle;
  30. browseInfo.ulFlags = nFlags;
  31. browseInfo.lpfn = NULL;
  32. browseInfo.lParam = 0;
  33. LPITEMIDLIST lpItemIDList;
  34. if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
  35. {
  36. // Get the path of the selected folder from the item ID list.
  37. if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
  38. {
  39. // At this point, szBuffer contains the path the user chose.
  40. if (szBuffer[0] == '\0')
  41. {
  42. // SHGetPathFromIDList failed, or SHBrowseForFolder failed.
  43. AfxMessageBox("Failed to get directory", MB_ICONSTOP|MB_OK);
  44. return strResult;
  45. }
  46. // We have a path in szBuffer!
  47. strResult = szBuffer;
  48. return strResult;
  49. }
  50. else
  51. {
  52. // The thing referred to by lpItemIDList
  53. // might not have been a file system object.
  54. // For whatever reason, SHGetPathFromIDList didn't work!
  55. AfxMessageBox("Failed to get directory", MB_ICONSTOP|MB_OK);
  56. return strResult; // strResult is empty
  57. }
  58. lpMalloc->Free(lpItemIDList);
  59. lpMalloc->Release();
  60. }
  61. return strResult;
  62. }
  63. void DoEvents()
  64. {
  65. MSG msg;
  66. // window message
  67. while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  68. {
  69. TranslateMessage(&msg);
  70. DispatchMessage(&msg);
  71. }
  72. }
  73. /********************************************************************/
  74. /* */
  75. /* Function name : GetAppDir */
  76. /* Description : Get application directory. */
  77. /* */
  78. /********************************************************************/
  79. void GetAppDir(CString& strAppDir)
  80. {
  81. TCHAR szFullPath[MAX_PATH];
  82. TCHAR szDrive[_MAX_DRIVE] = {0};
  83. TCHAR szDir[_MAX_DIR] = {0};
  84. TCHAR szFName[_MAX_FNAME] = {0};
  85. TCHAR szExt[_MAX_EXT] = {0};
  86. ::GetModuleFileName(NULL, szFullPath, MAX_PATH);
  87. _tsplitpath_s(szFullPath, szDrive, szDir, szFName, szExt);
  88. strAppDir.Format(_T("%s%s"), szDrive, szDir);
  89. }
  90. PFNSHGETFOLDERPATHA GetFuncPtr_SHGetFolderPathA()
  91. {
  92. static HMODULE hMod = NULL;
  93. PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
  94. // Load SHFolder.dll only once
  95. if (!hMod)
  96. hMod = LoadLibrary("SHFolder.dll");
  97. // Obtain a pointer to the SHGetFolderPathA function
  98. if (hMod)
  99. pSHGetFolderPath = (PFNSHGETFOLDERPATHA)GetProcAddress(hMod, "SHGetFolderPathA");
  100. return pSHGetFolderPath;
  101. }
  102. /********************************************************************/
  103. /* */
  104. /* Function name : CreateStartMenuShortcut */
  105. /* Description : Create a shortcut using the IShellLink interface.*/
  106. /* */
  107. /********************************************************************/
  108. HRESULT CreateStartMenuShortcut(LPSTR lpszShortcutFile, LPSTR lpszDescription, LPTSTR lpszRelativeFolder)
  109. {
  110. HRESULT hr;
  111. PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
  112. TCHAR lpszLink[MAX_PATH];
  113. BOOL bFound = FALSE;
  114. pSHGetFolderPath = GetFuncPtr_SHGetFolderPathA();
  115. // Find the current user's Start Menu Programs folder
  116. if (pSHGetFolderPath)
  117. bFound = SUCCEEDED(pSHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, 0, lpszLink));
  118. if (bFound)
  119. {
  120. // Proceed to create the shortcut
  121. IShellLink *pIShellLink = NULL;
  122. IPersistFile *ppf = NULL;
  123. WCHAR pLinkUnicode[MAX_PATH];
  124. CoInitialize(NULL);
  125. // Get a pointer to the IShellLink interface.
  126. hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  127. IID_IShellLink, (void **)&pIShellLink);
  128. if (SUCCEEDED(hr))
  129. {
  130. // Get a pointer to IPersistFile interface for saving shortcut
  131. hr = pIShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf);
  132. if (SUCCEEDED(hr))
  133. {
  134. hr = pIShellLink->SetPath(lpszShortcutFile);
  135. hr = pIShellLink->SetDescription(lpszDescription);
  136. if (SUCCEEDED(hr))
  137. {
  138. // Add the target folder to the Start Menu Programs path
  139. lstrcat(lpszLink, "\\");
  140. lstrcat(lpszLink, lpszRelativeFolder);
  141. lstrcat(lpszLink, "\\");
  142. // Create the directory if it does not exist
  143. CreateDirectory(lpszLink,NULL);
  144. // Add the file name for the shortcut
  145. lstrcat(lpszLink, lpszDescription);
  146. lstrcat(lpszLink, ".lnk");
  147. // Convert string to Unicode, and call IPersistFile::Save()
  148. MultiByteToWideChar(CP_ACP, 0, lpszLink, -1, pLinkUnicode, MAX_PATH);
  149. hr = ppf->Save(pLinkUnicode, TRUE);
  150. }
  151. ppf->Release();
  152. }
  153. pIShellLink->Release();
  154. }
  155. CoUninitialize();
  156. }
  157. return hr;
  158. }
  159. /********************************************************************/
  160. /* */
  161. /* Function name : RemoveStartMenuShortcut */
  162. /* Description : Create a shortcut using the IShellLink interface.*/
  163. /* */
  164. /********************************************************************/
  165. void RemoveStartMenuShortcut(LPSTR lpszDescription, LPTSTR lpszRelativeFolder)
  166. {
  167. PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
  168. TCHAR lpszLink[MAX_PATH];
  169. BOOL bFound = FALSE;
  170. pSHGetFolderPath = GetFuncPtr_SHGetFolderPathA();
  171. // Find the current user's Start Menu Programs folder
  172. if (pSHGetFolderPath)
  173. bFound = SUCCEEDED(pSHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, 0, lpszLink));
  174. if (bFound)
  175. {
  176. // Add the target folder to the Start Menu Programs path
  177. lstrcat(lpszLink, "\\");
  178. lstrcat(lpszLink, lpszRelativeFolder);
  179. lstrcat(lpszLink, "\\");
  180. // Add the file name for the shortcut
  181. lstrcat(lpszLink, lpszDescription);
  182. lstrcat(lpszLink, ".lnk");
  183. DeleteFile(lpszLink); // ɾ³ý*.lnk;
  184. }
  185. }
  186. /********************************************************************/
  187. /* */
  188. /* Function name : GetShortcutTarget */
  189. /* Description : Get target filename from shortcut. */
  190. /* */
  191. /********************************************************************/
  192. CString GetShortcutTarget(LPCTSTR lpszFilename)
  193. {
  194. CoInitialize(0);
  195. CString strResult;
  196. HRESULT hResult;
  197. IShellLink *psl;
  198. char szPath[MAX_PATH];
  199. WIN32_FIND_DATA findData;
  200. // Get a pointer to the IShellLink interface.
  201. hResult = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
  202. if (SUCCEEDED(hResult))
  203. {
  204. IPersistFile *ppf;
  205. // Get a pointer to the IPersistFile interface.
  206. hResult = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
  207. if (SUCCEEDED(hResult))
  208. {
  209. WCHAR wsz [MAX_PATH]; // buffer for Unicode string
  210. // Ensure that the string consists of Unicode characters.
  211. MultiByteToWideChar (CP_ACP, 0, lpszFilename, -1, wsz, MAX_PATH);
  212. // Load the shortcut.
  213. hResult = ppf->Load (wsz, STGM_READ);
  214. if (SUCCEEDED (hResult))
  215. {
  216. // Resolve the shortcut.
  217. hResult = psl->Resolve (0, SLR_ANY_MATCH|SLR_NO_UI);
  218. if (SUCCEEDED (hResult))
  219. {
  220. lstrcpy (szPath, lpszFilename);
  221. // Get the path to the shortcut target.
  222. hResult = psl->GetPath(szPath, MAX_PATH, (WIN32_FIND_DATA *)&findData, 0);
  223. strResult = szPath;
  224. }
  225. }
  226. // Release the pointer to IPersistFile.
  227. ppf->Release ();
  228. }
  229. // Release the pointer to IShellLink.
  230. psl->Release ();
  231. }
  232. CoUninitialize();
  233. if (SUCCEEDED(hResult))
  234. return strResult;
  235. else
  236. return "";
  237. }
  238. // Pump messages while waiting for event
  239. BOOL WaitWithMessageLoop(HANDLE hEvent, int nTimeout)
  240. {
  241. DWORD dwRet;
  242. while (1)
  243. {
  244. // wait for event or message, if it's a message, process it and return to waiting state
  245. dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE, nTimeout, QS_ALLINPUT);
  246. if (dwRet == WAIT_OBJECT_0)
  247. {
  248. TRACE0("WaitWithMessageLoop() event triggered.\n");
  249. return TRUE;
  250. }
  251. else
  252. if (dwRet == WAIT_OBJECT_0 + 1)
  253. {
  254. // process window messages
  255. AfxGetApp()->PumpMessage();
  256. }
  257. else
  258. if (dwRet == WAIT_TIMEOUT)
  259. {
  260. // timed out !
  261. return FALSE;
  262. }
  263. else
  264. {
  265. // WAIT_ABANDONED_0 ...
  266. return TRUE;
  267. }
  268. }
  269. }
  270. /********************************************************************/
  271. /* */
  272. /* Function name : AutoSizeColumns */
  273. /* Description : */
  274. /* */
  275. /********************************************************************/
  276. void AutoSizeColumns(CListCtrl *pListCtrl)
  277. {
  278. // Call this after your the control is filled
  279. pListCtrl->SetRedraw(FALSE);
  280. int mincol = 0;
  281. int maxcol = pListCtrl->GetHeaderCtrl()->GetItemCount()-1;
  282. for (int col = mincol; col <= maxcol; col++)
  283. {
  284. pListCtrl->SetColumnWidth(col, LVSCW_AUTOSIZE);
  285. int wc1 = pListCtrl->GetColumnWidth(col);
  286. pListCtrl->SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
  287. int wc2 = pListCtrl->GetColumnWidth(col);
  288. // 10 is minumim column width
  289. int wc = max(10, max(wc1,wc2));
  290. pListCtrl->SetColumnWidth(col,wc);
  291. }
  292. pListCtrl->SetRedraw(TRUE);
  293. }
  294. /********************************************************************/
  295. /* */
  296. /* Function name : MakeSureDirectoryPathExists */
  297. /* Description : This function creates all the directories in */
  298. /* the specified DirPath, beginning with the root. */
  299. /* This is a clone a Microsoft function with the */
  300. /* same name. */
  301. /* */
  302. /********************************************************************/
  303. BOOL MyMakeSureDirectoryPathExists(LPCTSTR lpszDirPath)
  304. {
  305. CString strDirPath = lpszDirPath;
  306. int nPos = 0;
  307. while((nPos = strDirPath.Find('\\', nPos+1)) != -1)
  308. {
  309. CreateDirectory(strDirPath.Left(nPos), NULL);
  310. }
  311. return CreateDirectory(strDirPath, NULL);
  312. }
  313. /********************************************************************/
  314. /* */
  315. /* Function name : IsNumeric */
  316. /* Description : Check if specified data is numeric */
  317. /* */
  318. /********************************************************************/
  319. BOOL IsNumeric(char *buff)
  320. {
  321. // validate data
  322. char *ptr = buff;
  323. while(*ptr)
  324. {
  325. if (isdigit(*ptr))
  326. {
  327. ptr++;
  328. }
  329. else
  330. return FALSE;
  331. }
  332. return TRUE;
  333. }
  334. CArray<CODBCPool*, CODBCPool*> g_dbpool;