Skocz do zawartości

Dołącz do społeczności!

👋 Witaj na MPCForum!

Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

  • ✅ Pełny dostęp do działów i ukrytych treści
  • ✅ Możliwość pisania i odpowiadania w tematach
  • ✅ System prywatnych wiadomości
  • ✅ Zbieranie reputacji i rozwijanie swojego profilu
  • ✅ Członkostwo w jednej z największych społeczności graczy

👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

Zarejestruj się teraz

[Pytanie]Jak mogę pobrać plik?[c++]

Rekomendowane odpowiedzi

Opublikowano

Yo,

chcę zrobić patchera plików w C++.

Tylko nie wiem jak zrobić, aby pobrał odpowiednie pliki, z odpowiedniej strony.

Np. png z np. http://jakiśadres/OBRAZ.png i zapisał w tym samym folderze gdzie jest ten program, albo folderze "BoX" który jest w folderze w którym jest ten program.

Proszę o pomoc :).

Edytowane przez Bartomil™

54355832.png

 

beznazwyln.gif

 

54355832.png

Opublikowano

Możesz.

 

 

 

@EDIT

Eh, no dobra.

 

Dla protokołu HTTP:

MSDN > About WinInet > HTTP Sessions

 

http://msdn.microsoft.com/en-us/library/aa384322(v=VS.85).aspx

 

int WINAPI Dumper(HWND hX, int intCtrlID, HINTERNET hResource)
{
   LPTSTR lpszData;    // buffer for the data
   DWORD  dwSize;       // size of the data available
   DWORD  dwDownloaded; // size of the downloaded data
   DWORD  dwSizeSum=0;  // size of the data in the textbox
   LPTSTR lpszHolding;  // buffer to merge the textbox data and buffer

   // Set the cursor to an hourglass.
   SetCursor(LoadCursor(NULL,IDC_WAIT));

   // This loop handles reading the data.
   do
   {
       // The call to InternetQueryDataAvailable determines the
       // amount of data available to download.
       if (!InternetQueryDataAvailable(hResource,&dwSize,0,0))
       {
           printf("InternetQueryDataAvailable failed (%d)\n", GetLastError());
           SetCursor(LoadCursor(NULL,IDC_ARROW));
           return FALSE;
       }
       else
       {
           // Allocate a buffer of the size returned by
           // InternetQueryDataAvailable.
           lpszData = new TCHAR[dwSize+1];

           // Read the data from the HINTERNET handle.
           if(!InternetReadFile(hResource,
                                (LPVOID)lpszData,
                                dwSize,
                                &dwDownloaded))
           {
               printf("InternetReadFile failed (%d)\n", GetLastError());
               delete[] lpszData;
               break;
           }
           else
           {
               // Add a null terminator to the end of the data buffer
               lpszData[dwDownloaded]='\0';

               // Allocate the holding buffer.
               lpszHolding = new TCHAR[dwSizeSum + dwDownloaded + 1];

               // Check if there has been any data written
               // to the textbox.
               if (dwSizeSum != 0)
               {
                   // Retrieve the data stored in the textbox if any
                   GetDlgItemText(hX,intCtrlID,
                                  (LPTSTR)lpszHolding,
                                  dwSizeSum);

                   // Add a null terminator at the end of the
                   // textbox data.
                   lpszHolding[dwSizeSum]='\0';
               }
               else
               {
                   // Make the holding buffer an empty string.
                   lpszHolding[0]='\0';
               }

               size_t cchDest = dwSizeSum + dwDownloaded + dwDownloaded + 1;
               LPTSTR* ppszDestEnd = 0;
               size_t* pcchRemaining = 0;

               // Add the new data to the holding buffer
               HRESULT hr = StringCchCatEx(lpszHolding,
                                           cchDest,
                                           lpszData,
                                           ppszDestEnd,
                                           pcchRemaining,
                                           STRSAFE_NO_TRUNCATION);

               if(SUCCEEDED(hr))
               {
                   // Write the holding buffer to the textbox.
                   SetDlgItemText(hX,intCtrlID,(LPTSTR)lpszHolding);

                   // Delete the two buffers.
                   delete[] lpszHolding;
                   delete[] lpszData;

                   // Add the size of the downloaded data to the
                   // textbox data size.
                   dwSizeSum = dwSizeSum + dwDownloaded + 1;

                   // Check the size of the remaining data.
                   // If it is zero, break.
                   if (dwDownloaded == 0)
                       break;
                   else
                   {
                   //  TODO: Insert error handling code here.
                   }
               }
           }
       }
   }
   while(TRUE);

   // Close the HINTERNET handle.
   InternetCloseHandle(hResource);

   // Set the cursor back to an arrow.
   SetCursor(LoadCursor(NULL,IDC_ARROW));

   return TRUE;
}

 

Jeśli chcesz potem zapisać do pliku, to po prostu kopiujesz* bufor do strumienia (podstawy fstream'a). Oczywiście plikowi nadajesz odpowiedni format, żeby potem Shell sobie z nim poradził.

 

 

Tak się składa, że pracuję akurat nad biblioteką klas (od nooba dla nooba) do obsługi HTTP. Niedługo ją gdzieś zamieszczę ;P

 

* Oczywiście lepiej jest od razu przekazywać pointer do bufora w funkcji InternetReadFile

 

 

 

 

 

 

 

A co tam, podzielę się nawet własną funkcją (to jeszcze nie ta biblioteka klas :P).

 

namespace ___4ggr35510n__stadard_heder___ {

   inline void n(int i = 0) { for(int j = 0; j < i; j++) std::cout << '\n'; }

}

#define BR ;___4ggr35510n__stadard_heder___::n(1);


bool _net_readData(HINTERNET hRequest, unsigned char* &sHeaders, unsigned char* &sBody, int predictedSize = 35, int* total_data_readed = NULL) {

   //cout << "Zaczynam readData"; BR

   DWORD iDataReaded = 0;
   DWORD iNumberOfData = 0;
   unsigned int iTotalData = 0;
   unsigned int iBuferSize = predictedSize * 1024;

   unsigned char* buffer = new unsigned char[iBuferSize];
   unsigned char* newBuffer;
   unsigned char* headers_buffer = new unsigned char[1024];

   // headers //
   if ( HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)headers_buffer, &iDataReaded, NULL) ) {
       headers_buffer[iDataReaded] = '\0';
   } else {
       if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
           delete [] headers_buffer;
           headers_buffer = new unsigned char[iDataReaded+1];
           if ( ! HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)headers_buffer, &iDataReaded, NULL) ) {
               delete [] headers_buffer;
               return 0;
           }
           headers_buffer[iDataReaded] = '\0';
       } else {
           delete [] headers_buffer;
           return 0;
       }
   }

   //BR cout << "Ilosc bajtow na headers: " << iDataReaded; BR

   // ciało //



   while (true) {
       if ( InternetQueryDataAvailable(hRequest, &iNumberOfData, 0, 0) ) {

          // cout << "availble: " << iNumberOfData; BR

           if (!iNumberOfData)
               break;

           if ( (iTotalData + iNumberOfData) > iBuferSize ) { // jeśli ma do zapisania więcej niż jest w buforze (+ przypisanie nowej wartości iTotalData)
               iBuferSize *= 2; // zwiększ bufor dwukrotnie
               BR BR; std::cout << "Zwiekszam bufor do rozmiaru: " << iBuferSize; BR BR
               newBuffer = new unsigned char[iBuferSize];
               memcpy(newBuffer, buffer, iTotalData); // przekopiuj do nowego bufora tyle, ile do tej pory jest w starym buforze
               delete [] buffer;
               buffer = newBuffer; // pozamien nazwy i -> kopiuj dalej
           }

           if ( InternetReadFile(hRequest, (LPVOID)(buffer + iTotalData), iNumberOfData, &iDataReaded) ) {

               if (!iDataReaded) { // nie przeczytano żadnych danych -> koniec danych
                   break;
               }

               if (iDataReaded<iNumberOfData) {
                   BR BR std::cout << "Przeczytalo mniej nasienie szatana!"; BR  BR
                   buffer[iDataReaded] = '\0';
               }

               iTotalData += iDataReaded;

           } else
               return false; // failed ReadFile(), shoulda never happen
       } else
           return false; // failed QDA(), shoulda never happen

   }

   buffer[iTotalData] = '\0';
   *total_data_readed = iTotalData;

   sHeaders = headers_buffer;
   sBody = buffer;

   // cout << sBody;

   return true;
}



/// przykład zastosowania
#include <iostream>
using namespace std;
int main () {

HINTERNET internet = InternetOpen( "Opera/9.80 (Windows NT 5.1; U; Edition Campaign 21; pl) Presto/2.8.131 Version/11.11", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL );
HINTERNET session = InternetConnect( internet, "www.google.pl" , INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
HINTERNET httpRequest = HttpOpenRequest( session, "GET", "search?q=4ggr35510n" , NULL, NULL, NULL, NULL, NULL);
if (!HttpSendRequest(httpRequest, NULL, NULL, NULL, NULL)){
	cout << "SendRequest failed!";
	return 1;
}

unsigned char* sHeaders;
unsigned char* sBody;

int iTC = GetTickCount();
int total_data;

if ( ! _net_readData(httpRequest, sHeaders, sBody, 20, &total_data) ) { // przewidujemy 2kB, lolz, na pewno będzie za mało, ale mój wrodzony geniusz zwiększa rozmiar buforu
	cout << "_net_ReadData failed!";
	return 2; }

cout << "Czas pracy funkcji _net_readData: " << GetTickCount() - iTC << ". Total data readed: " << total_data << "\n\n\n\n";

cout << sHeaders;
// cout << sBody;

return 0;
}

 

 

Odkumentuj sobie cout << sBody, żeby zalać się niepotrzebnym spamem z google ;p

 

Żeby zobaczyć, że faktycznie działa, należy przekopiować do pliku (a raczej od razu kopiować w InternetReadFile, ale to już nie w tej skromnej funkcji _net_readData) i odpalić ShellExecute'm, przeglądarka zajmie się resztą.

 

 

@EDIT któryś z rzędu:

Należy jeszcze delete[] sBody i delete[] sHeaders, jako, że są dynamicznie alokowane.

 

 

 

Ta sygnatura jest pusta.

Ostatnio przeglądający 0

  • Brak zarejestrowanych użytkowników przeglądających tę stronę.

Konto

Nawigacja

Skonfiguruj powiadomienia push w przeglądarce

Chrome (Android)
  1. Stuknij ikonę kłódki obok paska adresu.
  2. Stuknij Uprawnienia → Powiadomienia.
  3. Dostosuj swoje ustawienie.
Chrome (Komputer)
  1. Kliknij ikonę kłódki na pasku adresu.
  2. Wybierz Ustawienia witryny.
  3. Znajdź Powiadomienia i dostosuj swoje ustawienie.