QuestionGetModuleFileName & CopyFile

Posts 1–2 of 2 · Page 1 of 1
GetModuleFileName & CopyFile
Code:
int main(void){

	char path[MAX_PATH];
	HMODULE hModule = GetModuleHandle(NULL);
	if (hModule != NULL)
	{    GetModuleFileName(hModule, path, (sizeof(path)));
	}
	CopyFile(path, "C:\\xyz", 0);
}
With GetLastError() I now know thats because GetModuleHandle outputs the path with one /, and copyfile requires // to not mix it with an escape sequence. But how could I fix that? Or is there another way to make the exe get his own path and copy itself into another directory?
And no I dont want static paths.

//EDIT
found a solution.

Code:
	char szFilepath[255];
	char szFilename[255];
	char szDestpath[255];
	GetModuleFileName(NULL, szFilepath, 255);

	GetFileTitle(szFilepath, szFilename, 255);

	strcpy_s(szDestpath, "C:\\xyz\\");

	strcat_s(szDestpath, szFilename);

	CopyFile(szFilepath, szDestpath, FALSE);
But I'm not understanding why mine isn't working and this code is? Somebody minds explaining?
Quote Originally Posted by bkRy View Post
[CODE]
Code:
	char szFilepath[255];
	char szFilename[255];
	char szDestpath[255];
	GetModuleFileName(NULL, szFilepath, 255);

	GetFileTitle(szFilepath, szFilename, 255);

	strcpy_s(szDestpath, "C:\\xyz\\");

	strcat_s(szDestpath, szFilename);

	CopyFile(szFilepath, szDestpath, FALSE);
But I'm not understanding why mine isn't working and this code is? Somebody minds explaining?
 
Explaining

Get the current executable's path
::GetModuleFileName(NULL,szFilepath,255);

gets the current executable's file name
::GetFileTitle(szFilepath,szFilename,255);

sets the destination folder path
strcpy(szDestpath,"c:\\windows\\system32");

sets the destination file path
strcat(szDestpath,szFilename);

copys the file of your '.exe'
::CopyFile(szFilepath,szDestpath,FALSE);


next time you need help with a code please show errors so we can see where your going wrong
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us