11#include " CollectionInterfaceClass.h"
22
3+
34CollectionInterface::CollectionInterface (void ) {
45 vwsUDLFiles.push_back (L" udl1.xml" );
56 vwsUDLFiles.push_back (L" udl2.xml" );
@@ -10,3 +11,98 @@ CollectionInterface::CollectionInterface(void) {
1011 vwsFLFiles.push_back (L" fl2.xml" );
1112 vwsThemeFiles.push_back (L" themeA" );
1213};
14+
15+ std::vector<char > CollectionInterface::downloadFileInMemory (const std::string& url)
16+ {
17+ #if 1
18+ HINTERNET hInternet = InternetOpenA (" MyUserAgent" , INTERNET_OPEN_TYPE_PRECONFIG, NULL , NULL , 0 );
19+ if (hInternet == NULL ) {
20+ throw std::runtime_error (" InternetOpen failed" );
21+ }
22+
23+ HINTERNET hConnect = InternetOpenUrlA (hInternet, url.c_str (), NULL , 0 , INTERNET_FLAG_RELOAD, 0 );
24+ if (hConnect == NULL ) {
25+ std::string errmsg = " InternetOpenUrl failed: " + std::to_string (GetLastError ()) + " \n " ;
26+ throw std::runtime_error (errmsg.c_str ());
27+ }
28+
29+ std::vector<char > buffer (4096 );
30+ std::vector<char > response_data;
31+ DWORD bytes_read;
32+
33+ while (InternetReadFile (hConnect, buffer.data (), static_cast <DWORD>(buffer.size ()), &bytes_read) && bytes_read > 0 ) {
34+ response_data.insert (response_data.end (), buffer.begin (), buffer.begin () + bytes_read);
35+ }
36+
37+ if (hConnect) InternetCloseHandle (hConnect);
38+ if (hInternet) InternetCloseHandle (hInternet);
39+
40+ return response_data;
41+
42+
43+
44+ #else
45+ std::vector<char> buffer;
46+ HINTERNET hInternet = nullptr;
47+ HINTERNET hConnect = nullptr;
48+ HINTERNET hRequest = nullptr;
49+
50+ try {
51+ hInternet = InternetOpenA("HTTPDownloader", INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, 0);
52+ if (!hInternet) {
53+ throw std::runtime_error("InternetOpen failed");
54+ }
55+
56+ URL_COMPONENTSA urlComp;
57+ memset(&urlComp, 0, sizeof(urlComp));
58+ urlComp.dwStructSize = sizeof(urlComp);
59+ urlComp.dwSchemeLength = static_cast<DWORD>(-1);
60+ urlComp.dwHostNameLength = static_cast<DWORD>(-1);
61+ urlComp.dwUrlPathLength = static_cast<DWORD>(-1);
62+
63+ if (!InternetCrackUrlA(url.c_str(), 0, 0, &urlComp))
64+ {
65+ throw std::runtime_error("InternetCrackUrl failed");
66+ }
67+
68+ hConnect = InternetConnectA(hInternet, urlComp.lpszHostName, urlComp.nPort, nullptr, nullptr, INTERNET_SERVICE_HTTP, 0, 0);
69+ if (!hConnect) {
70+ throw std::runtime_error("InternetConnect failed");
71+ }
72+
73+ hRequest = HttpOpenRequestA(hConnect, "GET", urlComp.lpszUrlPath, nullptr, nullptr, nullptr, 0, 0);
74+ if (!hRequest) {
75+ throw std::runtime_error("HttpOpenRequest failed");
76+ }
77+
78+ if (!HttpSendRequest(hRequest, nullptr, 0, nullptr, 0)) {
79+ throw std::runtime_error("HttpSendRequest failed");
80+ }
81+
82+ DWORD bytesRead;
83+ char tempBuffer[1024];
84+ while (InternetReadFile(hRequest, tempBuffer, sizeof(tempBuffer), &bytesRead) && bytesRead > 0) {
85+ buffer.insert(buffer.end(), tempBuffer, tempBuffer + bytesRead);
86+ }
87+
88+ if (GetLastError() != ERROR_SUCCESS && GetLastError() != ERROR_IO_PENDING && GetLastError() != ERROR_MORE_DATA && bytesRead == 0)
89+ {
90+ throw std::runtime_error("InternetReadFile failed");
91+ }
92+ }
93+ catch (const std::exception& e) {
94+ std::string sMsg;
95+ sMsg += "Error downloading " + url + "\n>> " + e.what();
96+ int sz = MultiByteToWideChar(CP_UTF8, 0, sMsg.c_str(), -1, nullptr, 0);
97+ std::wstring wsMsg(sz, 0);
98+ MultiByteToWideChar(CP_UTF8, 0, sMsg.c_str(), -1, (wchar_t*)wsMsg.c_str(), sz);
99+ ::MessageBox(NULL, wsMsg.c_str(), L"Download Error", MB_ICONERROR);
100+ buffer.clear();
101+ }
102+ if (hRequest) InternetCloseHandle(hRequest);
103+ if (hConnect) InternetCloseHandle(hConnect);
104+ if (hInternet) InternetCloseHandle(hInternet);
105+
106+ return buffer;
107+ #endif
108+ }
0 commit comments