Skip to content

Commit 9095482

Browse files
committed
CollectionInterface::downloadFileInMemory() is working using InternetOpenUrlA()
1 parent 7b09ba4 commit 9095482

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

src/Classes/CollectionInterfaceClass.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CollectionInterfaceClass.h"
22

3+
34
CollectionInterface::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+
}

src/Classes/CollectionInterfaceClass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22
#include <string>
33
#include <vector>
4+
#include <stdexcept>
5+
#include <windows.h>
6+
#include <wininet.h>
47

58
class CollectionInterface {
69
public:
@@ -12,4 +15,6 @@ class CollectionInterface {
1215
std::vector<std::wstring> vwsACFiles;
1316
std::vector<std::wstring> vwsFLFiles;
1417
std::vector<std::wstring> vwsThemeFiles;
18+
19+
std::vector<char> downloadFileInMemory(const std::string& url);
1520
};

src/Dialogs/CollectionInterfaceDialog.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ INT_PTR CALLBACK ciDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam
6565
::SendDlgItemMessage(hwndDlg, IDC_CI_COMBO_CATEGORY, CB_SETCURSEL, index2Begin, 0);
6666

6767
pobjCI = new CollectionInterface;
68+
std::vector<char> vcThemeJSON = pobjCI->downloadFileInMemory("https://raw.githubusercontent.com/notepad-plus-plus/nppThemes/master/themes/.toc.json");
69+
// TODO: process JSON
70+
71+
std::vector<char> vcUdlJSON = pobjCI->downloadFileInMemory("https://raw.githubusercontent.com/notepad-plus-plus/userDefinedLanguages/refs/heads/master/udl-list.json");
72+
// TODO: process JSON
73+
6874
_populate_file_cbx(hwndDlg, pobjCI->vwsUDLFiles);
6975
}
7076

vs.proj/CollectionInterface.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@
192192
<Link>
193193
<SubSystem>Windows</SubSystem>
194194
<GenerateDebugInformation>true</GenerateDebugInformation>
195-
<AdditionalDependencies>shlwapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
195+
<AdditionalDependencies>shlwapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;wininet.lib;%(AdditionalDependencies)</AdditionalDependencies>
196+
<AdditionalLibraryDirectories>
197+
</AdditionalLibraryDirectories>
196198
</Link>
197199
<PostBuildEvent>
198200
<Command>echo Hello World &amp; pause</Command>

0 commit comments

Comments
 (0)