1
0

filecopy.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "api.h"
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <wchar.h>
  5. #include "resource.h"
  6. #include <strsafe.h>
  7. typedef struct CopyData
  8. {
  9. void * callbackContext;
  10. void (*callback)(void * callbackContext, wchar_t * status);
  11. } CopyData;
  12. DWORD CALLBACK CopyToIpodProgressRoutine(LARGE_INTEGER TotalFileSize, LARGE_INTEGER TotalBytesTransferred,
  13. LARGE_INTEGER StreamSize, LARGE_INTEGER StreamBytesTransferred,
  14. DWORD dwStreamNumber,
  15. DWORD dwCallbackReason,
  16. HANDLE hSourceFile, HANDLE hDestinationFile,
  17. LPVOID lpData)
  18. {
  19. CopyData *inst = (CopyData *)lpData;
  20. if (inst && inst->callback)
  21. {
  22. wchar_t status[100] = {0};
  23. wchar_t langtemp[100] = {0};
  24. StringCbPrintf(status, sizeof(status), WASABI_API_LNGSTRINGW_BUF(IDS_TRANSFERING_PERCENT, langtemp, 100), (int)(100ULL * TotalBytesTransferred.QuadPart / (TotalFileSize.QuadPart)));
  25. inst->callback(inst->callbackContext,status);
  26. }
  27. return PROGRESS_CONTINUE;
  28. }
  29. int CopyFile(const wchar_t *infile, const wchar_t *outfile, void * callbackContext, void (*callback)(void * callbackContext, wchar_t * status), int * killswitch)
  30. {
  31. wchar_t langtemp[100] = {0};
  32. CopyData c;
  33. c.callback = callback;
  34. c.callbackContext = callbackContext;
  35. if (CopyFileEx(infile, outfile, CopyToIpodProgressRoutine, &c, killswitch,0))
  36. {
  37. if (callback)
  38. {
  39. callback(callbackContext, WASABI_API_LNGSTRINGW_BUF(IDS_DONE, langtemp, 100));
  40. }
  41. return 0;
  42. }
  43. else
  44. {
  45. switch(GetLastError())
  46. {
  47. case ERROR_REQUEST_ABORTED:
  48. DeleteFile(outfile);
  49. if (callback)
  50. {
  51. callback(callbackContext, WASABI_API_LNGSTRINGW_BUF(IDS_CANCELLED, langtemp, 100));
  52. }
  53. default:
  54. if (callback)
  55. {
  56. callback(callbackContext, WASABI_API_LNGSTRINGW_BUF(IDS_TRANSFER_FAILED, langtemp, 100));
  57. }
  58. }
  59. return -1;
  60. }
  61. }