So i found this c++ program and i thought i would use it to automate backing up my files to my desktop ftp server but it always runs into the same seg fault problem, After checking the ftp server logs i can see that is does actually connect to the ftp server and log in with the user name and password but it when it reaches the actual upload part it crashes.
I ran it through the debugger in dev c++ and it says "Access violation (Seg faut)"
Is this a bug in wininet? and if so is there some sort of workaround?
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet")
#include <fstream>
#include <string.h>
int send(const char * ftp, const char * user, const char * pass, const char * pathondisk, char * nameonftp)
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if(hInternet==NULL)
{
cout << GetLastError();
//system("PAUSE");
return 1;
}
hFtpSession = InternetConnect(hInternet,
(LPTSTR)ftp, INTERNET_DEFAULT_FTP_PORT,
(LPTSTR)user, (LPTSTR)pass, INTERNET_SERVICE_FTP,
INTERNET_FLAG_PASSIVE, 0);
if(hFtpSession==NULL)
{
cout << GetLastError();
//system("PAUSE");
return 1;
}
Sleep(1000);
char * buf=nameonftp;
strcat(buf,".txt");
if (!FtpPutFile(hFtpSession, (LPTSTR)pathondisk, (LPTSTR)buf, FTP_TRANSFER_TYPE_ASCII, 0)) {
cout << GetLastError();//this is never reached
return 1;
}
Sleep(1000);
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
int main() {
send("127.0.0.1","testuser","test","file.pdf","backup");
return 0;
}
You must not modify string literals. Copy the string to a new buffer to edit the contents.
Also, you should use hogeA() APIs to have the system use ANSI charset explicitly.
Try this:
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet")
#include <iostream>
#include <fstream>
#include <string.h>
using std::cout;
int send(const char * ftp, const char * user, const char * pass, const char * pathondisk, const char * nameonftp)
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hInternet==NULL)
{
cout << GetLastError();
//system("PAUSE");
return 1;
}
hFtpSession = InternetConnectA(hInternet,
ftp, INTERNET_DEFAULT_FTP_PORT,
user, pass, INTERNET_SERVICE_FTP,
INTERNET_FLAG_PASSIVE, 0);
if(hFtpSession==NULL)
{
cout << GetLastError();
//system("PAUSE");
return 1;
}
Sleep(1000);
char * buf=new char[strlen(nameonftp) + 4 + 1];
strcpy(buf, nameonftp);
strcat(buf, ".txt");
if (!FtpPutFileA(hFtpSession, pathondisk, buf, FTP_TRANSFER_TYPE_ASCII, 0)) {
cout << GetLastError();
delete[] buf;
return 1;
}
delete[] buf;
Sleep(1000);
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
int main() {
send("127.0.0.1", "testuser", "test", "file.pdf", "backup");
return 0;
}
Related
This is a shortened version of my code:
#include <iostream>
#include <urlmon.h>
#include <wininet.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "wininet.lib")
void data();
void test(std::string received) {
data();
Sleep(1);
}
void data() {
- >>HINTERNET connect = InternetOpen(L"MyBrowser", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET OpenAddress = InternetOpenUrl(connect, L"web page", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
char dataReceived[5000];
std::string received;
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, dataReceived, 5000, &NumberOfBytesRead) && NumberOfBytesRead)
{
received += std::string(dataReceived);
}
- >>InternetCloseHandle(connect);
InternetCloseHandle(OpenAddress);
test(received);
}
int main() {
data();
}
Please tell me how to normally move the selected lines (- >>) outside the function to run them once?
Change data() and test() to take an HINTERNET as a parameter, and then main() can create the HINTERNET to pass in, eg:
#include <iostream>
#include <urlmon.h>
#include <wininet.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "wininet.lib")
void data(HINTERNET connect);
void test(HINTERNET connect, std::string received) {
data(connect);
Sleep(1);
}
void data(HINTERNET connect) {
HINTERNET OpenAddress = InternetOpenUrl(connect, L"web page", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
char dataReceived[5000];
std::string received;
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, dataReceived, 5000, &NumberOfBytesRead) && NumberOfBytesRead)
{
received += std::string(dataReceived);
}
InternetCloseHandle(OpenAddress);
test(connect, received);
}
int main() {
HINTERNET connect = InternetOpen(L"MyBrowser", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
data(connect);
InternetCloseHandle(connect);
}
This is a shortened version of my code:
#include <iostream>
#include <urlmon.h>
#include <wininet.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "wininet.lib")
void data();
void test(std::string received) {
data();
Sleep(1);
}
void data() {
- >>HINTERNET connect = InternetOpen(L"MyBrowser", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET OpenAddress = InternetOpenUrl(connect, L"web page", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
char dataReceived[5000];
std::string received;
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, dataReceived, 5000, &NumberOfBytesRead) && NumberOfBytesRead)
{
received += std::string(dataReceived);
}
- >>InternetCloseHandle(connect);
InternetCloseHandle(OpenAddress);
test(received);
}
int main() {
data();
}
Please tell me how to normally move the selected lines (- >>) outside the function to run them once?
Change data() and test() to take an HINTERNET as a parameter, and then main() can create the HINTERNET to pass in, eg:
#include <iostream>
#include <urlmon.h>
#include <wininet.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "wininet.lib")
void data(HINTERNET connect);
void test(HINTERNET connect, std::string received) {
data(connect);
Sleep(1);
}
void data(HINTERNET connect) {
HINTERNET OpenAddress = InternetOpenUrl(connect, L"web page", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
char dataReceived[5000];
std::string received;
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, dataReceived, 5000, &NumberOfBytesRead) && NumberOfBytesRead)
{
received += std::string(dataReceived);
}
InternetCloseHandle(OpenAddress);
test(connect, received);
}
int main() {
HINTERNET connect = InternetOpen(L"MyBrowser", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
data(connect);
InternetCloseHandle(connect);
}
So this is what i am trying todo i am trying to copy serial number to clipboard but it doesnt work is there anything i did wrong if yes then plz help me i would like to have this working becouse its something for a project of me that i am selling
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include "windows.h"
namespace std {}
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR volumeName[MAX_PATH + 1] = { 0 };
TCHAR fileSystemName[MAX_PATH + 1] = { 0 };
DWORD serialNumber = 0;
DWORD maxComponentLen = 0;
DWORD fileSystemFlags = 0;
if (GetVolumeInformation(
_T("C:\\"),
volumeName,
ARRAYSIZE(volumeName),
& serialNumber,
& maxComponentLen,
& fileSystemFlags,
fileSystemName,
ARRAYSIZE(fileSystemName)))
{
_tprintf(_T("Serial Number: %lu\n"), serialNumber);
GlobalUnlock(GetVolumeInformation);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(1, GetVolumeInformation);
CloseClipboard();
MessageBoxA(NULL, "HWID COPYED.", "HWID", NULL);
std::cout << std::endl << "Press any key to continue...";
getchar();
}
}
You should avoid using the T macros (the macros starting with _T and _t). Microsoft still uses these macros in some of its documentations for historical reasons, but it's useless and confusing. I don't know if you are using ANSI or Unicode (Unicode is recommended).
If you only need the serial number from GetVolumeInformation then you can set the other variables to NULL, see documentation.
Once you get the serial number, convert it to text. Then copy the text to clipboard. Below is ANSI example:
void copy(const char* text)
{
int len = strlen(text) + 1;
HGLOBAL hmem = GlobalAlloc(GMEM_MOVEABLE, len);
char* buffer = (char*)GlobalLock(hmem);
strcpy_s(buffer, len, text);
GlobalUnlock(hmem);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_TEXT, hmem);
CloseClipboard();
}
int _tmain()
{
DWORD serialNumber = 0;
if (GetVolumeInformation(
_T("C:\\"),
NULL,
0,
&serialNumber,
NULL,
0,
NULL,
0))
{
std::cout << serialNumber << std::endl;
char buf[100];
sprintf_s(buf, 100, "%d", serialNumber);
copy(buf);
MessageBoxA(NULL, buf, "HWID", NULL);
}
return 0;
}
I'm trying to make the equivalent code made using Sockets in a code using WinInet (I'm already in Windows).
This is what I've got:
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main (){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
system("pause");
return 1;
}
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.google.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(80);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout << "Connecting...\n";
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
return 1;
}
cout << "Connected.\n";
send(Socket,"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"),0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
system("pause");
return 0;
}
This is what I did with WinInet:
#include "stdafx.h"
#include <Windows.h>
#include <WinInet.h>
#pragma comment (lib, "Wininet.lib")
#pragma comment (lib, "urlmon.lib")
// Defines
#define POST 1
#define GET 0
void Request(int Method, LPCSTR Host, LPCSTR url, LPCSTR header, LPSTR data)
{
try {
//Retrieve default http user agent
char httpUseragent[512];
DWORD szhttpUserAgent = sizeof(httpUseragent);
ObtainUserAgentString(0, httpUseragent, &szhttpUserAgent);
char m[5];
if (Method == GET)
strcpy_s(m, "GET");
else
strcpy_s(m, "POST");
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa385096%28v=vs.85%29.aspx
HINTERNET internet = InternetOpenA(httpUseragent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (internet != NULL)
{
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa384363%28v=vs.85%29.aspx
HINTERNET connect = InternetConnectA(internet, Host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (connect != NULL)
{
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa384233%28v=vs.85%29.aspx
HINTERNET request = HttpOpenRequestA(connect, m, url, "HTTP/1.1", NULL, NULL,
INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_NO_UI |
INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_RELOAD, NULL);
if (request != NULL)
{
int datalen = 0;
if (data != NULL) datalen = strlen(data);
int headerlen = 0;
if (header != NULL) headerlen = strlen(header);
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa384247%28v=vs.85%29.aspx
HttpSendRequestA(request, header, headerlen, data, datalen);
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa384350%28v=vs.85%29.aspx
InternetCloseHandle(request);
}
}
InternetCloseHandle(connect);
}
InternetCloseHandle(internet);
}
catch (...) {}
}
int main()
{
char URL[1024];
char* geturi = ""; // string.empty ( I want to make the request to the main page )
wsprintfA(URL, geturi, NULL, NULL);
Request(GET, "google.com", URL, NULL, NULL);
// How can I see if it works? I want to get the webpage html source
getchar(); // I avoided system("pause") function as it isn't portable.
}
How can I see if it works?
Thanks in advance,
Qasim
I wrote this code that is SUPPOSED to write to a file on an ftp server, but it doesn't work. The file stays 0 bytes. There are also no errors. Here's my code:
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
int main()
{
int error=0;
char buffer[256];
char *text="Hello world.";
HINTERNET hOpen=InternetOpen("",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_PASSIVE);
InternetGetLastResponseInfo((LPDWORD)error,(LPSTR)buffer,(LPDWORD)256);
printf("hOpen:%d:%s\n",error,buffer);
HINTERNET hConnect=InternetConnect(hOpen,"diabloip.host22.com",INTERNET_DEFAULT_FTP_PORT,"MY_USER_NAME","MY_PASSWORD",INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
InternetGetLastResponseInfo((LPDWORD)error,(LPSTR)buffer,(LPDWORD)256);
printf("hConnect:%d:%s\n",error,buffer);
HINTERNET hFile=FtpOpenFile(hConnect,"diabloip.host22.com/public_html/log.txt",GENERIC_WRITE,FTP_TRANSFER_TYPE_ASCII,0);
InternetGetLastResponseInfo((LPDWORD)error,(LPSTR)buffer,(LPDWORD)256);
printf("hFile:%d:%s\n",error,buffer);
InternetWriteFile(hFile,text,strlen(text),NULL);
return 0;
}
The problem is passing NULL as the last parameter to InternetWriteFile. It is not an optional parameter and if you had error checking for that call as the rest you'd see GetLastError returns 87, or ERROR_INVALID_PARAMETER.
This works correctly and cleans up some of the other issues with incorrect parameters and the excessive casting that masks the issues.
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#pragma comment(lib, "wininet.lib")
void PrintStatus(const char* title)
{
DWORD error = 0;
DWORD sz = 256;
char buffer[256];
InternetGetLastResponseInfo(&error, buffer, &sz);
printf("%s:%u:%s\n", title, error, buffer);
}
int main()
{
const char *text = "Hello world.";
HINTERNET hOpen = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_PASSIVE);
PrintStatus("hOpen");
HINTERNET hConnect = InternetConnect(hOpen, "localhost", INTERNET_DEFAULT_FTP_PORT, "test", "test", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
PrintStatus("hConnect");
HINTERNET hFile = FtpOpenFile(hConnect, "log.txt", GENERIC_WRITE, FTP_TRANSFER_TYPE_ASCII, 0);
PrintStatus("hFile");
DWORD wb = 0;
BOOL Success = InternetWriteFile(hFile, text, strlen(text), &wb);
if(!Success)
{
DWORD err = GetLastError();
printf("InternetWriteFile - Error = %u\n", err);
}
PrintStatus("InternetWriteFile");
InternetCloseHandle(hOpen);
return 0;
}