Advertisement
captmicro

Untitled

Jan 31st, 2010
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 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.     return returnVal;
  13. }
  14.  
  15. DWORD COMM_URLDownloadToTempFile(char *inURL)
  16. {
  17.     HRESULT returnVal = NULL;
  18.     char *tempname;
  19.     tempname = tmpnam(0);
  20.     returnVal = COMM_URLDownloadToFile(inURL, tempname);
  21.     if (returnVal == S_OK)
  22.     {
  23.         remove(tempname);
  24.         return COMM_OK;
  25.     }
  26.     return COMM_GENERR;
  27. }
  28.  
  29. DWORD COMM_URlDownloadToString(char *inURL, char *outBUFF, BOOL binary)
  30. {
  31.     HRESULT returnVal = NULL;
  32.     FILE *tempFile;
  33.     char *fbuffer;
  34.     long fsize;
  35.     size_t fresult;
  36.     char *tempname;
  37.     tempname = tmpnam(0);
  38.     returnVal = COMM_URLDownloadToFile(inURL, tempname);
  39.     if (returnVal == S_OK)
  40.     {
  41.         if (binary) {
  42.             tempFile = fopen(tempname, "rb");
  43.         } else {
  44.             tempFile = fopen(tempname, "r");
  45.         }
  46.         if (tempFile == NULL) { return COMM_FILEERR; }
  47.         fseek(tempFile, 0, SEEK_END);
  48.         fsize = ftell(tempFile);
  49.         rewind(tempFile);
  50.         fbuffer = (char*)malloc(fsize*sizeof(char));
  51.         if (fbuffer == NULL) { return COMM_MEMERR; }
  52.         fresult = fread(fbuffer, 1, fsize, tempFile);
  53.         if (fresult != fsize) { return COMM_READERR; }
  54.         fclose(tempFile);
  55.         free(fbuffer);
  56.     }
  57.     strcpy(outBUFF, fbuffer);
  58.     return COMM_OK;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement