Ok heres what I want to do: I want to put a file in a console application (during compileation\codeing not running) and when it runs it saves the file in the same place it was run from. Like moving a movie as an exe to hide it while it is in

Recommended Answers

All 12 Replies

Sounds like your trying to inject a piece of code somewhere. I don't think the forum encourages hacking practices.

Ok heres what I want to do: I want to put a file in a console application (during compileation\codeing not running) and when it runs it saves the file in the same place it was run from. Like moving a movie as an exe to hide it while it is in

Add the file to the project as a resource. Of course, if you don't have a legitimate reason to do so, then by all means don't for the good of everyone.

Sorry got cut off, in computer lab typing this and lunch ended. Well it's not hacking... More like hiding (have no reason to do so just sounds interesting) like 7zip's Self extracting Arcive (but with only one file)

Ok and also; if I could duplicate the code many times I would make a fast eassyaly replicatable arcive the downside would be that folders are tought to do aren't they.

I haven't really done this myself but my hypothesis is you should be able to store the directory hierarchy information in your application, and use it to make new folders and then place the actual data files in them.

Oh I see in your sig you're using Code::Blocks, I don't know how to add resources to the project using that compiler/IDE. Hopefully you can find that yourself.

In visual studio you right click in the solution explorer and select "Add->Resource"
and then you click "Import" and select the path to your resource, after which you must name your new resource type.

Afterward something like this is in order:

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	//http://msdn.microsoft.com/en-us/library/ms648042(v=VS.85).aspx
	HRSRC hRes = FindResourceA(NULL,MAKEINTRESOURCEA(IDR_EXECUTABLE1),"Executable");
	assert(hRes != NULL);
	
	//http://msdn.microsoft.com/en-us/library/ms648046(VS.85).aspx
	HGLOBAL res = LoadResource(NULL, hRes);
	assert(res != NULL);

	//Obtain a pointer to the first byte of the resource.
	//MSDN Quote:
	/*
	Note  LockResource does not actually lock memory; it is just used to obtain a pointer to the memory containing the resource 
	data. The name of the function comes from versions prior to Windows XP, when it was used to lock a global memory block 
	allocated by LoadResource.
	*/
	//http://msdn.microsoft.com/en-us/library/ms648047(v=VS.85).aspx
	LPVOID pRes = LockResource(res);
	assert(pRes != NULL);

	//http://msdn.microsoft.com/en-us/library/ms648048(v=VS.85).aspx
	DWORD resourceSize = SizeofResource(NULL, hRes);
	assert(resourceSize != 0);

	//Write file to disk.
	std::cout << "The resource file size is " << resourceSize << " bytes." << std::endl;
	std::cout << "Writing file to current working directory:\n \"";
	char pDir[MAX_PATH];
	GetCurrentDirectoryA(MAX_PATH,pDir);
	std::cout << pDir << "\""<< std::endl;

	std::ofstream outFile("Executable.exe",std::ios::binary);
	outFile.write((const char *)pRes, resourceSize);
	outFile.close();

	std::cin.get();
	return 0;
}

MAN thats that's the third time today that my IPod deleted my post. Ok here's the run through: Theexample of what I want to do: I have a .obj I want to email to someone but my comp doesnt have Internet so I email it from another one but it's a vista comp and it's gona nag me so much about how .objs are system files (xp already does) so I put it in a .exe and every body's happy! When I run the exe the obj will come out this is not my senario just think it sounds cool your code was confusing and is there any way to use ofstream and ifstream for this sorry about sounding rude and fast I hate retyping things

MAN thats that's the third time today that my IPod deleted my post. Ok here's the run through: Theexample of what I want to do: I have a .obj I want to email to someone but my comp doesnt have Internet so I email it from another one but it's a vista comp and it's gona nag me so much about how .objs are system files (xp already does) so I put it in a .exe and every body's happy! When I run the exe the obj will come out this is not my senario just think it sounds cool your code was confusing and is there any way to use ofstream and ifstream for this sorry about sounding rude and fast I hate retyping things

I don't think this is the best solution to the problem, but then again without installing third-party software it may be..
Usually .exe is more restricted than .obj though.

Solution 1. Rename the .obj file.

If that doesn't work you may need to encrypt it, which you can write an application to do (it just needs to obfuscate it somehow and then transform it back to the original).

Normally this is done via renaming the file, then archiving it with a password (using third party software like winrar or peazip). This will prevent determining the type of file from it's data and file name.

Solution 2. What you have proposed, basically the example uses Windows API functions necessary to interact with the resource stored inside the executable.

So it seems to me you can either,
1) Encrypt the file's bytes using XOR or some other method, and then decrypt it (using ifstream/ofstream).
2) Take the time to read the MSDN page links I have included, and include it in an executable as a resource.

Ok like I said That's not my senario the pouts I want to focus on is how to do it via ifstream and ofstream to make something like 7zip's Self extracting Arcive (without encryption or compression)
How would I make a program to do that XOR encryption? It has always sounded cool but couldn't find any good documentation on it. I want the encryption program to be in a console app (just like the file moving thing this thread was made for)
PS Sorry not trying to sound rude I know your trying your best and still have to fight through all of my iPods typos so thanks (I still want you to answer the question that wasn't a thanks you can leave now thanks)
Edit: Ya know I'll start another thread fir the XOR thing you just focus ya effects on the question here, he' (I can't do a street accent can I?)

Oh I know what XOR Encryption is I don't know how to apply it in a program. (like not"what to use this for" but what I need is how to do it) Yegh ... This is akward. Man that Wikipedia article was confusing. I started a thread on XOR Encryption so back to the subject. If you can't find anything (I couldn't google gigablast and alltheinternet just gave me articles on text files.) I know you tried your hardest and I don't need this for a school project or something; just thought it sounded cool... Um are there any other file IOs than ofstream and ifstream?
Edit: I know about iexpress.exe (start,run,iexpress.exe in xp) I just wanted to make my own for console apps.

Of course there are other ways to do file IO, namely the underlying Operating System API (which is the Windows API in this case).

If you do intend to work with it, please try to develop good coding strategies soon or it will become very convoluted as the API is pure C.

http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

I would wager the OS API is more powerful than the C++ std lib, but I may be wrong and they are equal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.