i'd let known i'm relatively new c++ (and feeling guilty asking 2 questions on same day pertaining same project).
running loop below (or uncommenting 5 consecutive lines calling mydownloadfunction running), cause application crash.
error message:
terminate called after throwing instance of 'std::ios_base::failure' what(): basic_ios::clear application has requested runtime terminate in unusual way. please contact application's support team more information.
what i'd know why doesn't crash if function called once or twice, crashes if run 3 or more times (and on third time, file saved properly) and, of course, how fix it.
please assume https://mywebsite.com exists question.
#include <iostream> #include <sstream> // #include <stdio.h> // #include <string> #include <windows.h> using namespace std; int main() { typedef int * (*mydownloadtourl)(void*, const char*, const char*, dword, void*); hinstance libhnd = loadlibrary("urlmon.dll"); mydownloadtourl mydownloadfunction = (mydownloadtourl)getprocaddress(libhnd,"urldownloadtofilea"); stringstream url; stringstream iteration; // mydownloadfunction(null, "https://google.ca", "google 1.htm", 0, null); // mydownloadfunction(null, "https://google.ca", "google 2.htm", 0, null); // mydownloadfunction(null, "https://google.ca", "google 3.htm", 0, null); // mydownloadfunction(null, "https://google.ca", "google 4.htm", 0, null); // mydownloadfunction(null, "https://google.ca", "google 5.htm", 0, null); (int = 1; <= 5; i++) { url << "https://mywebsite.com/" << << "/"; cout << url.str() << "\r\n"; iteration << << ".htm"; cout << iteration.str() << "\r\n\r\n"; mydownloadfunction(null, url.str().c_str(), iteration.str().c_str(), 0, null); url.str(""); iteration.str(""); } }
urldownloadtofile (and if not other windows api functions) uses stdcall calling convention rather ccall convention. first , last parameter not void*s, lpunknown , lpbindstatuscallback, , returns hresult, not int*. undefined behavior call function of 1 type through pointer different type. need change typedef to:
typedef hresult (__stdcall *mydownloadtourl)(lpunknown, const char*, const char*, dword, lpbindstatuscallback);
Comments
Post a Comment