captmicro

Untitled

Jan 31st, 2010
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include "comm.h"
  2.  
  3. HRESULT COMM_URLDownloadToFile(char *inURL, char *inFILE)
  4. {
  5.     HRESULT returnVal;
  6.     HMODULE library = LoadLibraryA("urlmon.dll");
  7.     _COMM_URLDownloadToFileA function;
  8.     function = (_COMM_URLDownloadToFileA)GetProcAddress(library, "URLDownloadToFileA");
  9.     returnVal = function(0, inURL, inFILE, 0, 0);
  10.     FreeLibrary(library);
  11.     free(function);
  12.     if (returnVal == S_OK) {
  13.         return COMM_OK;
  14.     } else {
  15.         return COMM_GENERR;
  16.     }
  17. }
  18.  
  19. DWORD COMM_URLDownloadToTempFile(char *inURL)
  20. {
  21.     HRESULT returnVal = NULL;
  22.     char *tempname;
  23.     tempname = tmpnam(0);
  24.     returnVal = COMM_URLDownloadToFile(inURL, tempname);
  25.     if (returnVal == S_OK)
  26.     {
  27.         remove(tempname);
  28.         return COMM_OK;
  29.     }
  30.     return COMM_GENERR;
  31. }
  32.  
  33. DWORD COMM_URlDownloadToString(char *inURL, char *outBUFF, BOOL binary)
  34. {
  35.     HRESULT returnVal = NULL;
  36.     FILE *tempFile;
  37.     char *fbuffer;
  38.     long fsize;
  39.     size_t fresult;
  40.     char *tempname;
  41.     tempname = tmpnam(0);
  42.     returnVal = COMM_URLDownloadToFile(inURL, tempname);
  43.     if (returnVal == S_OK)
  44.     {
  45.         if (binary) {
  46.             tempFile = fopen(tempname, "rb");
  47.         } else {
  48.             tempFile = fopen(tempname, "r");
  49.         }
  50.         if (tempFile == NULL) { return COMM_FILEERR; }
  51.         fseek(tempFile, 0, SEEK_END);
  52.         fsize = ftell(tempFile);
  53.         rewind(tempFile);
  54.         fbuffer = (char*)malloc(fsize*sizeof(char));
  55.         if (fbuffer == NULL) { return COMM_MEMERR; }
  56.         fresult = fread(fbuffer, 1, fsize, tempFile);
  57.         if (fresult != fsize) { return COMM_READERR; }
  58.         fclose(tempFile);
  59.         free(fbuffer);
  60.     }
  61.     strcpy(outBUFF, fbuffer);
  62.     return COMM_OK;
  63. }
Add Comment
Please, Sign In to add comment