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 szDir[_MAX_DIR];
  83. TCHAR szDrive[_MAX_DRIVE];
  84. // Get application's full path.
  85. ::GetModuleFileName(NULL, szFullPath, MAX_PATH);
  86. // Break full path into seperate components.
  87. _splitpath(szFullPath, szDrive, szDir, NULL, NULL);
  88. // Store application's drive and path
  89. strAppDir.Format(_T("%s%s"), szDrive, szDir);
  90. }
  91. PFNSHGETFOLDERPATHA GetFuncPtr_SHGetFolderPathA()
  92. {
  93. static HMODULE hMod = NULL;
  94. PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
  95. // Load SHFolder.dll only once
  96. if (!hMod)
  97. hMod = LoadLibrary("SHFolder.dll");
  98. // Obtain a pointer to the SHGetFolderPathA function
  99. if (hMod)
  100. pSHGetFolderPath = (PFNSHGETFOLDERPATHA)GetProcAddress(hMod, "SHGetFolderPathA");
  101. return pSHGetFolderPath;
  102. }
  103. /********************************************************************/
  104. /* */
  105. /* Function name : CreateStartMenuShortcut */
  106. /* Description : Create a shortcut using the IShellLink interface.*/
  107. /* */
  108. /********************************************************************/
  109. HRESULT CreateStartMenuShortcut(LPSTR lpszShortcutFile, LPSTR lpszDescription, LPTSTR lpszRelativeFolder)
  110. {
  111. HRESULT hr;
  112. PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
  113. TCHAR lpszLink[MAX_PATH];
  114. BOOL bFound = FALSE;
  115. pSHGetFolderPath = GetFuncPtr_SHGetFolderPathA();
  116. // Find the current user's Start Menu Programs folder
  117. if (pSHGetFolderPath)
  118. bFound = SUCCEEDED(pSHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, 0, lpszLink));
  119. if (bFound)
  120. {
  121. // Proceed to create the shortcut
  122. IShellLink *pIShellLink = NULL;
  123. IPersistFile *ppf = NULL;
  124. WCHAR pLinkUnicode[MAX_PATH];
  125. CoInitialize(NULL);
  126. // Get a pointer to the IShellLink interface.
  127. hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  128. IID_IShellLink, (void **)&pIShellLink);
  129. if (SUCCEEDED(hr))
  130. {
  131. // Get a pointer to IPersistFile interface for saving shortcut
  132. hr = pIShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf);
  133. if (SUCCEEDED(hr))
  134. {
  135. hr = pIShellLink->SetPath(lpszShortcutFile);
  136. hr = pIShellLink->SetDescription(lpszDescription);
  137. if (SUCCEEDED(hr))
  138. {
  139. // Add the target folder to the Start Menu Programs path
  140. lstrcat(lpszLink, "\\");
  141. lstrcat(lpszLink, lpszRelativeFolder);
  142. lstrcat(lpszLink, "\\");
  143. // Create the directory if it does not exist
  144. CreateDirectory(lpszLink,NULL);
  145. // Add the file name for the shortcut
  146. lstrcat(lpszLink, lpszDescription);
  147. lstrcat(lpszLink, ".lnk");
  148. // Convert string to Unicode, and call IPersistFile::Save()
  149. MultiByteToWideChar(CP_ACP, 0, lpszLink, -1, pLinkUnicode, MAX_PATH);
  150. hr = ppf->Save(pLinkUnicode, TRUE);
  151. }
  152. ppf->Release();
  153. }
  154. pIShellLink->Release();
  155. }
  156. CoUninitialize();
  157. }
  158. return hr;
  159. }
  160. /********************************************************************/
  161. /* */
  162. /* Function name : RemoveStartMenuShortcut */
  163. /* Description : Create a shortcut using the IShellLink interface.*/
  164. /* */
  165. /********************************************************************/
  166. void RemoveStartMenuShortcut(LPSTR lpszDescription, LPTSTR lpszRelativeFolder)
  167. {
  168. PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
  169. TCHAR lpszLink[MAX_PATH];
  170. BOOL bFound = FALSE;
  171. pSHGetFolderPath = GetFuncPtr_SHGetFolderPathA();
  172. // Find the current user's Start Menu Programs folder
  173. if (pSHGetFolderPath)
  174. bFound = SUCCEEDED(pSHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, 0, lpszLink));
  175. if (bFound)
  176. {
  177. // Add the target folder to the Start Menu Programs path
  178. lstrcat(lpszLink, "\\");
  179. lstrcat(lpszLink, lpszRelativeFolder);
  180. lstrcat(lpszLink, "\\");
  181. // Add the file name for the shortcut
  182. lstrcat(lpszLink, lpszDescription);
  183. lstrcat(lpszLink, ".lnk");
  184. DeleteFile(lpszLink); // ɾ³ý*.lnk;
  185. }
  186. }
  187. /********************************************************************/
  188. /* */
  189. /* Function name : GetShortcutTarget */
  190. /* Description : Get target filename from shortcut. */
  191. /* */
  192. /********************************************************************/
  193. CString GetShortcutTarget(LPCTSTR lpszFilename)
  194. {
  195. CoInitialize(0);
  196. CString strResult;
  197. HRESULT hResult;
  198. IShellLink *psl;
  199. char szPath[MAX_PATH];
  200. WIN32_FIND_DATA findData;
  201. // Get a pointer to the IShellLink interface.
  202. hResult = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
  203. if (SUCCEEDED(hResult))
  204. {
  205. IPersistFile *ppf;
  206. // Get a pointer to the IPersistFile interface.
  207. hResult = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
  208. if (SUCCEEDED(hResult))
  209. {
  210. WCHAR wsz [MAX_PATH]; // buffer for Unicode string
  211. // Ensure that the string consists of Unicode characters.
  212. MultiByteToWideChar (CP_ACP, 0, lpszFilename, -1, wsz, MAX_PATH);
  213. // Load the shortcut.
  214. hResult = ppf->Load (wsz, STGM_READ);
  215. if (SUCCEEDED (hResult))
  216. {
  217. // Resolve the shortcut.
  218. hResult = psl->Resolve (0, SLR_ANY_MATCH|SLR_NO_UI);
  219. if (SUCCEEDED (hResult))
  220. {
  221. lstrcpy (szPath, lpszFilename);
  222. // Get the path to the shortcut target.
  223. hResult = psl->GetPath(szPath, MAX_PATH, (WIN32_FIND_DATA *)&findData, 0);
  224. strResult = szPath;
  225. }
  226. }
  227. // Release the pointer to IPersistFile.
  228. ppf->Release ();
  229. }
  230. // Release the pointer to IShellLink.
  231. psl->Release ();
  232. }
  233. CoUninitialize();
  234. if (SUCCEEDED(hResult))
  235. return strResult;
  236. else
  237. return "";
  238. }
  239. // Pump messages while waiting for event
  240. BOOL WaitWithMessageLoop(HANDLE hEvent, int nTimeout)
  241. {
  242. DWORD dwRet;
  243. while (1)
  244. {
  245. // wait for event or message, if it's a message, process it and return to waiting state
  246. dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE, nTimeout, QS_ALLINPUT);
  247. if (dwRet == WAIT_OBJECT_0)
  248. {
  249. TRACE0("WaitWithMessageLoop() event triggered.\n");
  250. return TRUE;
  251. }
  252. else
  253. if (dwRet == WAIT_OBJECT_0 + 1)
  254. {
  255. // process window messages
  256. AfxGetApp()->PumpMessage();
  257. }
  258. else
  259. if (dwRet == WAIT_TIMEOUT)
  260. {
  261. // timed out !
  262. return FALSE;
  263. }
  264. else
  265. {
  266. // WAIT_ABANDONED_0 ...
  267. return TRUE;
  268. }
  269. }
  270. }
  271. /********************************************************************/
  272. /* */
  273. /* Function name : AutoSizeColumns */
  274. /* Description : */
  275. /* */
  276. /********************************************************************/
  277. void AutoSizeColumns(CListCtrl *pListCtrl)
  278. {
  279. // Call this after your the control is filled
  280. pListCtrl->SetRedraw(FALSE);
  281. int mincol = 0;
  282. int maxcol = pListCtrl->GetHeaderCtrl()->GetItemCount()-1;
  283. for (int col = mincol; col <= maxcol; col++)
  284. {
  285. pListCtrl->SetColumnWidth(col, LVSCW_AUTOSIZE);
  286. int wc1 = pListCtrl->GetColumnWidth(col);
  287. pListCtrl->SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
  288. int wc2 = pListCtrl->GetColumnWidth(col);
  289. // 10 is minumim column width
  290. int wc = max(10, max(wc1,wc2));
  291. pListCtrl->SetColumnWidth(col,wc);
  292. }
  293. pListCtrl->SetRedraw(TRUE);
  294. }
  295. /********************************************************************/
  296. /* */
  297. /* Function name : MakeSureDirectoryPathExists */
  298. /* Description : This function creates all the directories in */
  299. /* the specified DirPath, beginning with the root. */
  300. /* This is a clone a Microsoft function with the */
  301. /* same name. */
  302. /* */
  303. /********************************************************************/
  304. BOOL MakeSureDirectoryPathExists(LPCTSTR lpszDirPath)
  305. {
  306. CString strDirPath = lpszDirPath;
  307. int nPos = 0;
  308. while((nPos = strDirPath.Find('\\', nPos+1)) != -1)
  309. {
  310. CreateDirectory(strDirPath.Left(nPos), NULL);
  311. }
  312. return CreateDirectory(strDirPath, NULL);
  313. }
  314. /********************************************************************/
  315. /* */
  316. /* Function name : IsNumeric */
  317. /* Description : Check if specified data is numeric */
  318. /* */
  319. /********************************************************************/
  320. BOOL IsNumeric(char *buff)
  321. {
  322. // validate data
  323. char *ptr = buff;
  324. while(*ptr)
  325. {
  326. if (isdigit(*ptr))
  327. {
  328. ptr++;
  329. }
  330. else
  331. return FALSE;
  332. }
  333. return TRUE;
  334. }