Move running EXE to new location without waiting for reboot - c++

I am trying to move the running EXE, but this gives access denied because of running state. I could use MoveFileEx(MOVEFILE_DELAY_UNTIL_REBOOT), but that only acts after rebooting.
Is there any way to do this without waiting for the reboot?
int _tmain(int argc, _TCHAR* argv[]){
WCHAR szFilepath[MAX_PATH];
std::wstring wFilepath;
std::wstring wFilename;
std::wstring wDestpath;
/* Get the current executable's full path */
wFilepath = std::wstring(szFilepath, GetModuleFileNameW(NULL, szFilepath, MAX_PATH));
std::wcout << L"filepath: " << wFilepath << std::endl;
/* Extract just the name */
wFilename = wFilepath.substr(wFilepath.find_last_of(L"\\/") + 1);
std::wcout << L"filename: " << wFilename << std::endl;
/* Set the destination folder path and file name */
wDestpath = L"D:\\" + wFilename;
std::wcout << L"dest path: " << wDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(wFilepath.c_str(), wDestpath.c_str(), FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}

Related

How to fix 'file keeps the original location and after being copied doesnt copy extension'?

I am trying to create a program which copy itself while it is running to new location without keeping the orignal file location . once it is copied I got the file without extension , but how can I overcome this ?
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szFilepath[MAX_PATH];
TCHAR szFilename[MAX_PATH];
TCHAR szDestpath[MAX_PATH];
/* Get the current executable's full path */
GetModuleFileName(NULL, szFilepath, MAX_PATH);
std::wcout << "filepath: " << szFilepath << std::endl;
/* Extract just the name */
GetFileTitle(szFilepath, szFilename, MAX_PATH);
std::wcout << "filename: " << szFilename << std::endl;
//Set the destination folder path
_tcscpy(szDestpath, L"D:\\");
//Set the destination file path
_tcscat(szDestpath, szFilename);
std::wcout << "dest path: " << szDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFile(szFilepath, szDestpath, FALSE)) {
std::cout << "couldnt copy the file";
}
else {
std::cout << "copied";
}
return 0;
}
Per the documentation for GetFileTitle():
GetFileTitle returns the string that the system would use to display the file name to the user. The display name includes an extension only if that is the user's preference for displaying file names. This means that the returned string may not accurately identify the file if it is used in calls to file system functions.
You should be using a more suitable function to get the actual filename, such as PathFindFileName():
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR szFilepath[MAX_PATH];
LPWSTR lpszFilename;
WCHAR szDestpath[MAX_PATH];
/* Get the current executable's full path */
GetModuleFileNameW(NULL, szFilepath, MAX_PATH);
std::wcout << L"filepath: " << szFilepath << std::endl;
/* Extract just the name */
lpszFilename = PathFindFileNameW(szFilepath);
std::wcout << L"filename: " << lpszFilename << std::endl;
/* Set the destination folder path and file name */
PathCombineW(szDestpath, L"D:\\", lpszFilename);
std::wcout << L"dest path: " << szDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(szFilepath, szDestpath, FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}
Or, you could simply parse the filename yourself using normal C++ string operations, eg:
#include <windows.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR szFilepath[MAX_PATH];
std::wstring wFilepath;
std::wstring wFilename;
std::wstring wDestpath;
/* Get the current executable's full path */
wFilepath = std::wstring(szFilepath, GetModuleFileNameW(NULL, szFilepath, MAX_PATH));
std::wcout << L"filepath: " << wFilepath << std::endl;
/* Extract just the name */
wFilename = wFilepath.substr(wFilepath.find_last_of(L"\\/")+1);
std::wcout << L"filename: " << wFilename << std::endl;
/* Set the destination folder path and file name */
wDestpath = L"D:\\" + wFilename;
std::wcout << L"dest path: " << wDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(wFilepath.c_str(), wDestpath.c_str(), FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}

CreateProcess and redirecting output

There are 2 apps.
AppCMD is a command line app and AppMAIN starts AppCMD with some command line args.
Unfortunately AppMAIN does not seem to handle the output off AppCMD very well and something is going wrong.
I'd like to log the calls to AppCMD and its output to see what is going on.
In order to do so I want to replace AppCMD with another binary AppWRAP that forwards the calls to a renamed AppCMD and logs it's output.
AppWRAP should act like a transparent Man-In-The-Middle.
For testing purposes I wrote a simple AppCMD that just outputs it's command line args:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << "#### Hello, I'm the test binary that wants to be wrapped." << endl;
if (argc < 2) {
cout << "#### There where no command line arguments." << endl;
}
else {
cout << "#### These are my command line arguments:";
for (int i = 1; i < argc; ++i) cout << " " << argv[i];
cout << endl;
}
cout << "#### That's pretty much everything I do ... yet ;)" << endl;
return 0;
}
I followed MSDN: Creating a Child Process with Redirected Input and Output to implement AppWrap but I got stuck since it does not return and I cant figure out why:
#include <iostream>
#include <sstream>
#include <Windows.h>
using namespace std;
const string TARGET_BINARY("TestBinary.exe");
const size_t BUFFSIZE = 4096;
HANDLE in_read = 0;
HANDLE in_write = 0;
HANDLE out_read = 0;
HANDLE out_write = 0;
int main(int argc, char *argv[])
{
stringstream call;
cout << "Hello, I'm BinTheMiddle." << endl;
//-------------------------- CREATE COMMAND LINE CALL --------------------------
call << TARGET_BINARY;
for (int i = 1; i < argc; ++i) {
call << " " << argv[i];
}
cout << "Attempting to call '" << call.str() << "'" << endl;
//------------------------------ ARRANGE IO PIPES ------------------------------
SECURITY_ATTRIBUTES security;
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.bInheritHandle = NULL;
security.bInheritHandle = TRUE;
security.lpSecurityDescriptor = NULL;
if (!CreatePipe(&out_read, &out_write, &security, 0)) {
cout << "Error: StdoutRd CreatePipe" << endl;
return -1;
}
if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) {
cout << "Stdout SetHandleInformation" << endl;
return -2;
}
if (!CreatePipe(&in_read, &in_write, &security, 0)) {
cout << "Stdin CreatePipe" << endl;
return -3;
}
if (!SetHandleInformation(in_write, HANDLE_FLAG_INHERIT, 0)) {
cout << "Stdin SetHandleInformation" << endl;
return -4;
}
//------------------------------ START TARGET APP ------------------------------
STARTUPINFO start;
PROCESS_INFORMATION proc;
ZeroMemory(&start, sizeof(start));
start.cb = sizeof(start);
start.hStdError = out_write;
start.hStdOutput = out_write;
start.hStdInput = in_read;
start.dwFlags |= STARTF_USESTDHANDLES;
ZeroMemory(&proc, sizeof(proc));
// Start the child process.
if (!CreateProcess(NULL, (LPSTR) call.str().c_str(), NULL, NULL, TRUE,
0, NULL, NULL, &start, &proc))
{
cout << "CreateProcess failed (" << GetLastError() << ")" << endl;
return -1;
}
// Wait until child process exits.
WaitForSingleObject(proc.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(proc.hProcess);
CloseHandle(proc.hThread);
//----------------------------------- OUTPUT -----------------------------------
DWORD dwRead;
CHAR chBuf[127];
while (ReadFile(out_read, chBuf, 127, &dwRead, NULL)) {
cout << "Wrapped: " << chBuf << endl;
}
return 0;
}
It seems like it is waiting for ReadFile to return. Can anybody spot what I'm doing wrong?
I call the binary this way:
> shell_cmd_wrapper.exe param1 param2
This is the console output but the binary does not return.
Hello, I'm BinTheMiddle.
Attempting to call 'TestBinary.exe param1 param2'
Wrapped:#### Hello, I'm the test binary that wants to be wrapped.
#### These are my command line arguments: param1 param2
#### That'sD
Wrapped: pretty much everything I do ... yet ;)
s to be wrapped.
#### These are my command line arguments: param1 param2
#### That'sD
(Please ignore that I don't clear the buffer)
Close the out_write and in_read handles after calling CreateProcess. Otherwise ReadFile on out_read will block when the pipe is empty because there's still a potential writer even after the child has exited -- the out_write handle in the current process.
Also, as noted by Harry Johnston in a comment, waiting for the process to exit before reading from the pipe can potentially cause a deadlock. The child will block on WriteFile if the pipe fills up.

Creating file with CreateFile function results in write protected file

I have been trying to write a file in path returned by SHGetFolderPath . But as File Is created, it has been given Write Protected. Following is my Code:
#include <Windows.h>
#include <Shlobj.h>
#include <iostream>
#include <Shlwapi.h>
int main()
{
HANDLE hfile;
TCHAR szPath[MAX_PATH];
char dataBuffer[] = "Some data to write here";
DWORD dwBytesWritten = 0;
if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_COMMON_APPDATA,NULL,0, szPath)))
{
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("lpa"));
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("config"));
std::cout << szPath << std::endl;
CreateDirectory(szPath, NULL);
PathAppend(szPath, TEXT("lpa.config"));
std::cout << szPath << std::endl;
hfile = CreateFile(szPath,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
/*WriteFile(hfile,dataBuffer,(DWORD)strlen(dataBuffer),&dwBytesWritten,NULL);
std::cout << szPath <<std::endl;*/
}
}
The code creates a file in C:\ProgramData\lpa\config\lpa.config but editing the file pops up dialog saying WriteProtection. What am I doing wrong here?
Yes, When you run VS instance with Admin privileges, special permissions are assigned to the created file. Either run as the program as Non-Admin or use SECURITY_DESCRIPTOR so that other user can access the file.
Creating a Security Descriptor for a New Object in C++
the example in the link shows how a registry key is created, you can put CreateFile() instead.
I solved the issue by using CSIDL_LOCAL_APPDATA instead of CSIDL_COMMON_APPDATA . I have the modified code given as below:
#include <Windows.h>
#include <Shlobj.h>
#include <iostream>
#include <Shlwapi.h>
int main()
{
HANDLE hfile;
TCHAR szPath[MAX_PATH];
char dataBuffer[] = "Some data to write here";
DWORD dwBytesWritten = 0;
if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_LOCAL_APPDATA,NULL,0, szPath)))
{
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("lpa"));
if(!CreateDirectory(szPath, NULL))
{
std::cout << "Create directory failed" <<std::endl;
}
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("config"));
if(!CreateDirectory(szPath, NULL))
{
std::cout << "Create directory failed" <<std::endl;
}
std::cout << szPath << std::endl;
PathAppend(szPath, TEXT("lpa.config"));
std::cout << szPath << std::endl;
hfile = CreateFile(szPath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hfile,dataBuffer,(DWORD)strlen(dataBuffer),&dwBytesWritten,NULL);
std::cout << szPath <<std::endl;
}
}

Visual C++ DLL that checks if file exists

I made this dll file that tries to check if a file exists. But even if I manually create the file, my dll still can't find it.
My dll retrieves the process id of the running program and looks for a file that is named after the pid.
Can anyone please tell me what I'm missing :(
Code:
#include <Windows.h>
#include <winbase.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int clientpid = GetCurrentProcessId();
ifstream clientfile;
string clientpids, clientfilepath;
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}
Supposing you are working with UNICODE
I think the problems goes in the following line:
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl; The tempcvar is a tchar, and maybe you are working with unicode, so it means tempcvar is a widechar.
The result that you get inserting tempcvar in ostr is not what you are expecting (You are mixing multibyte with widechar too). A solution to this problem is converting tempcvar into a multi byte string (const char* or char*...)
Look at this example based on your code (Look at the convertion between tchar to multibyte char)
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
// Convertion between tchar in unicode (wide char) and multibyte
wchar_t * tempcvar_widechar = (wchar_t*)tempcvar;
char* to_convert;
int bytes_to_store = WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,NULL,0,NULL,NULL);
to_convert = new char[bytes_to_store];
WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,to_convert,bytes_to_store,NULL,NULL);
// Using char* to_convert that is the tempcvar converted to multibyte
ostr << to_convert << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}
You can search more about the wide string to multibyte string convertion if this example does not works to you.
Check if you are working with Unicode, if you are, maybe this is your problem.
If you are not working with unicode, the problem in your code can be opening the file.
Hope it helps!

Concat command line arguments for FindFirstFile parameter in C++ VS 2010 OpenCV

Has anyone used the FindFirstFile function to scan multiple files of the same type?
int main(int argc, char** argv){
if(argc != 3)
{
cout <<" Usage: Run [dir of images folder] [file format]" << endl;
cout <<" Example: Run \\imgs\\\\ .jpg " << endl;
return 0;
}
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
string dir = argv[1]; // store user input dir
string type = argv[2]; // store user input file type
stringstream sst;
sst << dir << "*" << type;
string folderDir = sst.str();
sst.str("");
cout << "Scanning all " << type << " files in "<< dir << endl;
cout << folderDir << endl;
/* LOADING IMAGES IN FOLDER */
I tried folderDir.c_str() instead of "\imgs\*.jpg" but I can't make it work;
hFind = FindFirstFile("\imgs\\*.jpg", &FindFileData); //images must be in .vcxproj dir
if (hFind != INVALID_HANDLE_VALUE){
int i = 0;
do {
char loc[50] = "\imgs\\"; // Obvsly, I couldn't assign argv[1] here
images.push_back(imread(strcat(loc,FindFileData.cFileName))); //pushes images into vector
img_fname[i] = FindFileData.cFileName; // stores file names into string array
cout << "Successfully loaded " << FindFileData.cFileName << endl; //prints loaded file names
i++;
}while(FindNextFile(hFind, &FindFileData));
}
Also, may I ask help in assigning string dir to char loc[50] = "\imgs\\";?
If only char loc[50] = dir; is possible...
I tried strcpy(loc, dir.c_str()); after instantiating loc but it still failed. Gave me an error (Unrecognized or unsupported array type) in unknown function
i think, it should be only one backslash there:
"imgs\*.jpg" instead of "\imgs\\*.jpg".
this works fine for me ( and gives me the filelist ):
std::vector<std::string> readdir( const char * dmask )
{
std::vector<std::string> vec;
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
if ((hFind = FindFirstFile(dmask, &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
vec.push_back( FindFileData.cFileName );
} while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
return vec;
}
std::vector<std::string> files = readdir("imgs\*.jpg");