starting FTP Upload from DLL ?

10/25/2012 12:19 XxharCs#1
hey

i made a dll which is saving ur own game progress in a file.

now i also want to upload that file on a ftp server when the dll detaches the process.

is it possible to upload files when the dll is getting detached ?
i made it like this but it seems to dont work (getting the error of FtpPutFile when it fails)

Code:
...
ofstream ofile;
ofstream f;
char dest[9999];
TCHAR NPath[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HINTERNET hOpen, hConnection;

void upload(char *bla);

BOOL WINAPI DllMain ( HMODULE hDll, DWORD dwReason, LPVOID lpReserved )
{
...
...
case DLL_PROCESS_DETACH:

		if(ofile) 
		{ 
			GetCurrentDirectory(MAX_PATH, NPath);
			
			add_log("\n\n[Log Closed]"); 
			ofile.close();
			Sleep(500);

			FindFirstFile("./progress.txt", &FindFileData);
			
			sprintf_s(dest, "%s\\%s", NPath, FindFileData.cFileName);
                        MessageBox(NULL,  dest, "Test", NULL);
			upload(dest);
                        break;
		}
	}
	return TRUE;
}

...
...


void upload(char *filepath)
{


	hOpen = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

	

	hConnection = InternetConnect(hOpen, "example.bplaced.net", INTERNET_DEFAULT_FTP_PORT,
		"usernameExample", "passwordExample", INTERNET_SERVICE_FTP, NULL, NULL);

	

	if(!(FtpPutFile(hConnection, filepath, "/progress.txt", FTP_TRANSFER_TYPE_BINARY, INTERNET_FLAG_PASSIVE))){

			
		f.open("./Fehler.txt", ios::app);
		f << GetLastError() << endl;
		f.close();
	}else{

		MessageBox(NULL, "Successfully uploaded !", "Ftp Upload", NULL);
	}

	InternetCloseHandle(hConnection);
	InternetCloseHandle(hOpen);
	

}
... => means that there is the other code, didnt want to upload the whole code because itīs a lot of code :p

i hope u can help me :)
10/25/2012 12:53 tnd0#2
It is possible in theory, but only when the process tries to detach your DLL without shutting down.
If your DLL is being detached because your process is shutting down, you cannot guarantee that the WinSock Libraries are still loaded, and you cannot load libraries from a DLL's DllMain() function. You should restructure your program/library not to make use of external functions inside of DllMain(), because that is a recipe for guaranteed disaster.
10/25/2012 13:10 XxharCs#3
thanks for the answer

but is there some alternative way to do this in some same way ?
because i wanted, when the process is shuting down, to upload the file without starting an extra application to upload the file^^
10/25/2012 15:51 tnd0#4
catch the shutdown message with a windowhook or hook the shutdown procedure.