Dies im Dos-Mode?
-
Tag!
Ich versuche seit langem verzweifelt eine verbinung zum Internet aufzubauen um eine bestimmte html Datei runterzuladen. Jetzt bin ich beim durchsuchen der msdn auf das hier gestoßen: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)) { ErrorOut(hX,GetLastError(),"InternetReadFile"); 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)) { ErrorOut(hX,GetLastError(),"InternetReadFile"); 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; size_t* pcchRemaining; // 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. } } } //do while(TRUE); // Close the HINTERNET handle. InternetCloseHandle(hResource); // Set the cursor back to an arrow. SetCursor(LoadCursor(NULL,IDC_ARROW)); // Return return TRUE; }
Was mich jetzt interessiert:
- Wenn ich es so compiliere geht das nicht. Keine Mainfunktion? Oder warum nicht? Also -> Was ist das jetzt? Eine Funktion?
- Läuft das überhaupt im Dos-mode?
- Welche Headerdateien muss ich includen?
Ich hoffe ihr versteht mich
Danke!
Gruß Huwi
-
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/portal.asp latform SDK: Windows Internet Windows Internet Purpose The Microsoft Windows Internet (WinINet) application programming interface (API) provides client applications with access to standard Internet protocols, such as FTP, Gopher, and HTTP. It abstracts these protocols into a high-level interface for ease of use. Where Applicable WinINet does not support server implementations. In addition, it should not be used from a service. For server implementations or services use Microsoft Windows HTTP Services (WinHTTP). Developer Audience The WinINet API is designed for use by C/C++ programmers. It requires a basic understanding of the FTP, Gopher, and HTTP protocols. Run-Time Requirements Applications that use the WinINet API require Windows NT 4.0 or later, or Windows Me/98/95. For more information about which operating systems or components are required to use a particular programming element, see the Requirements section of the documentation. See Also Microsoft Windows HTTP Services (WinHTTP) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/commiturlcacheentry.asp Requirements Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0, Windows Me, Windows 98, or Windows 95. Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0. Version Requires Internet Explorer 3.0 or later. Header Declared in Wininet.h. Library Link to Wininet.lib. DLL Requires Wininet.dll. Unicode Implemented as CommitUrlCacheEntryW (Unicode) and CommitUrlCacheEntryA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.
-
Huwi schrieb:
Also -> Was ist das jetzt? Eine Funktion?
ja, eine funktion.
Huwi schrieb:
- Läuft das überhaupt im Dos-mode?
nö, das ist win32. braucht ausserdem ein fenster, 'ne dialogbox oder so
-
nigga schrieb:
Huwi schrieb:
- Läuft das überhaupt im Dos-mode?
nö, das ist win32. braucht ausserdem ein fenster, 'ne dialogbox oder so
-
elise schrieb:
nigga schrieb:
Huwi schrieb:
- Läuft das überhaupt im Dos-mode?
nö, das ist win32. braucht ausserdem ein fenster, 'ne dialogbox oder so
, wieso
-
Danke an alle.
Dann weiß ich jetzt immerhin, dass ich hier im falschen Bereich binAber ich wollte auch nur Konsole, alles andere ist mir noch zu hoch. Also kann ich das wohl damit vergessen
-
ragga schrieb:
elise schrieb:
nigga schrieb:
Huwi schrieb:
- Läuft das überhaupt im Dos-mode?
nö, das ist win32. braucht ausserdem ein fenster, 'ne dialogbox oder so
, wieso
wieso fenster, wieso dialogbox. warum nicht alles in win32 konsole reinhaun?
aber egal, er hat sich umentschieden
-
Joa ne eigentlich nicht umentschieden. Nur, weil er sagte das geht nicht mit Konsole. Wie sollte das denn z.B in eine Konsolenanwendung eingebaut werden?
-
Huwi schrieb:
Joa ne eigentlich nicht umentschieden. Nur, weil er sagte das geht nicht mit Konsole. Wie sollte das denn z.B in eine Konsolenanwendung eingebaut werden?
Warum sollte das in einer Konsolenanwendung nicht gehen? Ich hab noch nie gehört, dass man bestimmte WinAPI-Funktionen in einer Konsolenanwendung nicht verwenden kann. (Die Frage ist eher an den gestellt, der meinte, dass das nicht unter der Konsole geht)
Ich geh jetzt erstmal auf deine Fragen ein:
-
Richtig das ist eine Funktion, was du da gefunden hast und diese musst du z. B. aus main() heraus aufrufen. Jedes Programm braucht die main()-Funktion.
-
Unter native DOS läuft es nicht, wenn du allerdings mit DOS-Mode die Windows-Konsole meinst - wovon ich ausgehe - , dann funktioniert es natürlich schon.
-
Welche Headerdateien du brauchst, solltest du auch in der MSDN finden, aber ich tippe mal du brauchst einfach nur die windows.h
-
-
und die #include <wininet.h> //linken: Wininet.lib.
und ein paar sachen müssen umgeschrieben werden.
-
schonmal viele Dank dafür!
Hab jetzt mal versucht was zu machen, hab aber NULL Ahnung ob das, was ich da mache richtig ist.#include <windows.h> #include <wininet.h> int main() { char* buffer[512]; unsigned long* temp; HINTERNET test = InternetOpen("Opera", INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0); test = InternetOpenUrl(test, "http://www.thofrie.de/index.htm", NULL, NULL, INTERNET_FLAG_RELOAD, NULL); test = InternetConnect(test, "thofrie.de", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL); InternetReadFile(test, buffer, 1024, temp); std::cout << buffer; return 0; }
Würde das so funktionieren wie ich mir das vorstelle? Also, dass er in diesem Fall die index.htm ausgibt? Ich mein nur theoretisch, weil im Moment stürzt das Programm einfach nur ab. Hatte vorher noch nie was mit der WinAPI o.ä. zu tun, vielleicht weiß jemand Rat? Danke
-
hi
nach ein wenig rumsucherei hab ich ein konsolengeeignetes beispiel entdeckt, ohne viel tarrah mit CString und sonstigem.
achtung: leg eine .c file an, keinen .cpp, sonst musst du umdoktorn.
kleines wget programm, finde grad die url nicht, aber ist sicher leicht zu ergooglen.#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <winsock.h> #include <wininet.h> #include <assert.h> #include <lm.h> #include <time.h> #include <process.h> #include <string.h> #define BUF_MAXLEN 1024*sizeof(char) int wget(const char *url, const char *option) { HINTERNET hInternet, hFile; HANDLE *sFile; char *buffer = NULL; unsigned long dwRead, dwGot; int file_flag; DWORD size; if(strlen(url) < 3) { return(-1); } buffer = (char *)malloc(100024*sizeof(char)); strcpy(buffer, "Url: "); strcat(buffer, url); strcat(buffer, "\n"); hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); hFile = InternetOpenUrl(hInternet, url, NULL, 0, 0, 0); if(hFile == NULL) { InternetCloseHandle(hInternet); return(-1); } else{} dwGot = 0; file_flag = 0; if(strlen(option) > 1) { file_flag = 1; sFile = CreateFile(option, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (sFile == INVALID_HANDLE_VALUE) { InternetCloseHandle(hFile); InternetCloseHandle(hInternet); return(-1); } } while(InternetReadFile(hFile, buffer, BUF_MAXLEN / 2, &dwRead)) { if (dwRead == 0) break; dwGot+= dwRead; buffer[dwRead] = '\0'; if(file_flag == 1) { size = strlen(buffer); WriteFile(sFile, buffer, size, &size, NULL); } else{} } if(file_flag == 1) { CloseHandle(sFile); sprintf(buffer, "\n => File as been save in %s\n\n", option); } InternetCloseHandle(hFile); InternetCloseHandle(hInternet); return(0); } int main(){ wget("http://www.google.de", "safe.htm"); }
-
Vielen Dank damit funktioniert es!! Klasse hilfe, danke!!
-
Servus, hab da noch eine Frage. Ist es möglich diese Funktion wget() z.B in einer wget.c zu speichern und die in ein Projekt mit z.B. einer programm.cpp, wo die main() funktion enthalten ist, einzubinden, also, dass man aus der main() Funktion, die ja in einer c++ Quellcodedatei steht, die wget() Funktion, in einer c Quellcodedatei, aufruft?
Habs einfach so Probiert, aber das funktioniert nicht:
#include <iostream> #include <string> int wget(const char *url, const char *option); using namespace std; int main() { wget("http://www.thofrie.de", "index.htm"); return 0; }
-
Hat sich erledigt ich hab es jetzt in eince c++ Quellcode Datei umgewandelt bekommen. Falls es wen interessiert:
#include <windows.h> #include <wininet.h> #include <string.h> #include <fstream> using namespace std; #define BUF_MAXLEN 1024*sizeof(char) int wget(const char *url, const char *option) { HINTERNET hInternet, hFile; char *buffer = NULL; unsigned long dwRead, dwGot; int file_flag; if(strlen(url) < 3) { return(-1); } buffer = (char *)malloc(100024*sizeof(char)); strcpy(buffer, "Url: "); strcat(buffer, url); strcat(buffer, "\n"); hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); hFile = InternetOpenUrl(hInternet, url, NULL, 0, 0, 0); if(hFile == NULL) { InternetCloseHandle(hInternet); return(-1); } else{} dwGot = 0; file_flag = 0; ofstream dat_aus; dat_aus.open(option, ios_base::out); while(InternetReadFile(hFile, buffer, BUF_MAXLEN / 2, &dwRead)) { if (dwRead == 0) break; dwGot+= dwRead; buffer[dwRead] = '\0'; dat_aus << buffer; } InternetCloseHandle(hFile); InternetCloseHandle(hInternet); return 0; } int main() { wget("http://www.irgendwas.de", "index.htm"); return 0; }