BranchDeviceHelper.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef __BRANCH_DEVICE_HLEPER_H
  2. #define __BRANCH_DEVICE_HLEPER_H
  3. #pragma once
  4. const int MAX_PATH_SIZE = 256;
  5. #include <strsafe.h>
  6. #include <Shlobj.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. int GetCurrentRunPath(char *pPath)
  11. {
  12. char *pBuf = new char[MAX_PATH_SIZE];
  13. if (pBuf == NULL)
  14. return -1;
  15. ZeroMemory(pBuf,MAX_PATH_SIZE);
  16. GetModuleFileName(NULL,pBuf,MAX_PATH_SIZE);
  17. int len = strnlen_s(pBuf,MAX_PATH_SIZE);
  18. if (len <= 0)
  19. {
  20. delete []pBuf;
  21. return -2;
  22. }
  23. char *pch;
  24. pch = strstr (pBuf,"bin");
  25. if (pch == NULL)
  26. return -3;
  27. int lenDel = strnlen_s(pch,MAX_PATH_SIZE);
  28. if (len <= 0)
  29. {
  30. delete []pBuf;
  31. return -3;
  32. }
  33. strncpy_s(pPath,MAX_PATH_SIZE,pBuf,len-lenDel);
  34. delete []pBuf;
  35. return strnlen_s(pPath,MAX_PATH_SIZE);
  36. }
  37. // >0 means suc and set the path size
  38. // pPath store directory path like "C:\Users\{UserName}\AppData\LocalLow"
  39. // <0 means failed !!
  40. int GetWriteAvailableDirPath(char *pPath)
  41. {
  42. char *pBuf = new char[MAX_PATH_SIZE];
  43. if (pBuf == NULL)
  44. return -1;
  45. ZeroMemory(pBuf,MAX_PATH_SIZE);
  46. PWSTR pszPath = NULL;
  47. if(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, NULL, &pszPath) != S_OK)
  48. {
  49. delete[] pBuf;
  50. return -2;
  51. }
  52. WideCharToMultiByte(CP_ACP, 0, pszPath, -1, pBuf, MAX_PATH_SIZE, NULL, NULL);
  53. CoTaskMemFree((LPVOID)pszPath);
  54. int len = strnlen_s(pBuf, MAX_PATH_SIZE);
  55. strncpy_s(pPath, MAX_PATH_SIZE, pBuf, len);
  56. delete []pBuf;
  57. return strnlen_s(pPath, MAX_PATH_SIZE);
  58. }
  59. #ifdef __cplusplus
  60. }
  61. #endif
  62. #endif //__BRANCH_DEVICE_HLEPER_H