Packing a DLL into an exe and deleting it after running [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to pack a dynamic link library (D L L) into an executable and when I run it I want it to be deleted from the hard drive.
so the first question i need help with is: How to PROPERLY add a D L L into my project? so when i build it, the D L L is in the executable?
and the second question is: once the D L L is properly included in the project & the project is build into an executable, i want the D L L to be injected to another process from the executable using the following code:
pastebin.com/zsYVMqvs
and now to the third question: once the D L L is injected & it has to be extracted somewhere right? so i would like to delete the extracted D L L instantly after injecting so it does not stay there.

Putting the DLL into the resources of your .exe is a possibility. You then need to extract the DLL from the resources to a dll file on the local hard drive, probably into the temp directory. Then use it. Before the program quits, delete the dll created beforehand.
To extract use FindResource, LoadResource, LockResource and SizeofResource .
Flow of operations:
Extract the DLL to the TEMP directory (use GetTempPath)
run program doing whatever stuff you want to do with the DLL
befor the program quit make sure the DLL isn't used any more
delete the dll created under 1.

Related

object is not being saved even after fstream.clear() in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am using C++ to write a music memorize game for my school project, here the Player objects are not being saved in the PLAYER_DATA.DAT file even i tried clear() function
here is the peace of code (as the whole code is 600+ lines)
Player p_dat,plyr;
plyr.getData();
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
while(P_file.read((char*)&p_dat, sizeof(p_dat)))
{
if(nameEqual(plyr,p_dat))
{
P_file.clear();
gotoxy(1,10);
delline();
textcolor(RED);
cout<<"\t\t EXIXTING PLAYER PROFILE FOUND!\n";
int ch = playPanel("It's me", "Change Name");
if(ch == 0)
{
P_file.seekp(P_file.tellg() - sizeof(Player));
GameStarted = 1;
if(c == 1)
Campaign(p_dat,P_file);
else
Endless(p_dat,P_file);
return;
}
else
{
startGame(c);
return;
}
}
}
P_file.clear();
P_file.seekp(0,ios::end);
P_file.write((char*)&plyr,sizeof(plyr));
Just to make it short, The last line of the code is not doing anything the file already exists and of size 0kb
however,
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary)
P_file.write((char*)&plyr,sizeof(plyr));
is saving the file. Please help me.
EDIT 1.1
finally found this line is problematic
P_file.seekp(0,ios::end);
Its working for code, i.e correctly saving objects
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
P_file.seekp(0,ios::end);
while removing
P_file.seekp(0,ios::end);
making the code look like,
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
after the while loop, is not saving the file
again does not save the object
this line is making problem, are there any alternatives or solutions?
Compile with all warnings and debug info (g++ -Wall -Wextra -g with GCC) then use the debugger (e.g. gdb).
Read carefully documentation of C++ IO functions.
Check that your file has been opened correctly; after:
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
add
if (!P_file) { std::cerr << "failed to open PLAYER_DATA.DAT" << std::endl; };
On some systems (e.g. Linux), you might also display strerror(errno).
After some intermediate call to write, consider using flush.
Be sure that your program is started in the correct working directory.
On Linux, you might also use strace(1) to understand the system calls done by your program.
At last, did you consider using some simple database, e.g. with sqlite, or some indexed file, e.g. with gdbm ?

Why file is not created in /etc/ directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please find the code sample
void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);
File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.
There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.
In order to be able to write to /etc, your program must be executed as root.
It seems to be a permission issue. Try to run your program using sudo:
sudo yourprogram

How can i make my program run automatically in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I export my python code by pyinstaller i need my program to startup automatically whith the windows (i don't need to do that by startup folder)
First you need to create a shortcut. This will create shortcut on your desktop
import os, sys
import pythoncom
from win32com.shell import shell, shellcon
shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)
desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)
You can make toany of windows locations for startup files ( or create it only in startup place) :
Run Once :HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
Run each Start: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
StartUp folder : C:\Documents and Settings\All Users\Start Menu\Programs\Startup
Shared Task Manager : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SharedTaskScheduler
Make it as a service, and make that service automatic started
To see what programs start automatically on your computer, or to add your entries easy, you can use autoruns from SysInternals, http://technet.microsoft.com/en-us/sysinternals/bb963902.aspx
P.S. Python example is from timgolden.me.uk site.

How to change filename alongwith ext in C/CPP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have hundreds of files in a folder like:
mp_12345.dat
mp_23455.dat
mp_12323.dat
mp_44445.dat
.
.
.
I want to rename all file to another folder:
Ind_somecircle_mp_12345.mbin
Ind_somecircle_mp_23455.mbin
Ind_somecircle_mp_12323.mbin
Ind_somecircle_mp_44445.mbin
.
.
And so on.
source folder: /home/dir1/foo/
destination folder: /home/dir2/foo/
I am looking for C or C++ code to do so.
Thanks in advance.
Actually this would be a better job for a shell script, but if you insist on C then rename(src, tgt); is what you are looking for.
If you want to read the filenames from the directory, you must use opendir and readdir to loop through the files.
A short sample:
void main(int c,char **args)
{
DIR *dir;
struct dirent *dent;
dir = opendir("mydir);
if(dir!=NULL)
{
while((dent=readdir(dir))!=NULL)
printf(dent->d_name); <-- rename
}
close(dir);
}
If you insist on using C/C++ for the job you need to use
opendir - http://linux.die.net/man/3/opendir, readdir - http://linux.die.net/man/3/readdir to get the list of files
That use rename - http://linux.die.net/man/2/rename to change the filename of a file

Execute file in another directory [duplicate]

This question already has answers here:
system("c:\\sample\\startAll.bat") cannot run because of working directory?
(4 answers)
Closed 9 years ago.
Consider the following:
I have a c++ program in C:\Documents\myProgram.exe
With this code in it:
system("start C:\\somefolder\\start.bat");
That will start the target file (start.bat) in C:\Documents\ instead of `C:\somefolder\'.
My question is, how do I execute the file in it's own directory instead of myProgram's directory?
In theory this is what I want to accomplish using c++:
cd C:\somefolder\,
start start.bat
If you're on windows anyway, use ShellExecute, you can set more things and launch even documents, links etc.
To do this you can do one of two things (that I found).
A) You can use chdir() in unistd.h; see http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
or
B) You can use something called the File System Interface, from the GNU library, for more advanced stuff; see http://www.gnu.org/software/libc/manual/html_node/File-System-Interface.html#File-System-Interface.
Anyway, best of luck, I hope you find something that will work!