Copying youself?..... how?

Ok I have nearly created my program everything seems to be working ok (except one other thing but that will be a different post) I want my program to copy itself into the "C:\\Windows\\system32" directory but I cant find a way of making a program copy itself without having to input it's current name PLEASE HELP!
Last edited on
You have to use the GetModuleFileName Win32 API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <windows.h>
#include <string.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	char szFilepath[255];
	char szFilename[255];
	char szDestpath[255];
	//Get the current executable's path
	::GetModuleFileName(NULL,szFilepath,255);
	//Get the current executable's file name
	::GetFileTitle(szFilepath,szFilename,255);
	//Set the destination folder path
	strcpy(szDestpath,"c:\\windows\\system32\\");
	//Set the destination file path
	strcat(szDestpath,szFilename);
	//copy the file
	::CopyFile(szFilepath,szDestpath,FALSE);

	return 0;
}
thanks but Im getting four errors when I complie it

error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [255]' to 'LPWCH' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

error C2039: 'GetFileTitle' : is not a member of '`global namespace''

error C3861: 'GetFileTitle': identifier not found

error C2664: 'CopyFileW' : cannot convert parameter 1 from 'char [255]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Assuming your exe has write access to the Windows System directory (most don't). Microsoft recommends that you put stuff where GetWindowsDirectory() function indicates.

You are compiling with the UNICODE compile flag (hence "GetModuleFileNameW" etc), but you are using char strings in your program. The following works whether you want to stick with wide strings or not. There are also a number of other simple errors addressed by the following.
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
#include <commdlg.h>  /* you need this for GetFileTitle() */
#include <windows.h>

void complain();

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PLSTR lpCmdLine, int nCmdShow )
  {
  TCHAR szFilepath[ MAX_PATH ];
  TCHAR szFilename[ MAX_PATH ];
  TCHAR szDestpath[ MAX_PATH ];

  /* Get the current executable's full path */
  GetModuleFileName( NULL, szFilepath, MAX_PATH );

  /* Extract just the name */
  GetFileTitle( szFilepath, szFilename, MAX_PATH );

  /* Get the dest path */
  GetSystemDirectory( szDestpath, MAX_PATH );

  /* Tack-on the directory separator */
  lstrcat( szDestpath, TEXT( "\\" ) );

  /* And append the filename */
  lstrcat( szDestpath, szFilename );

  if (!CopyFile( szFilepath, szDestpath, FALSE )) complain();

  return 0;
  }

void complain()
  {
  LPVOID lpMsgBuf;
 
  FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL 
    );

  /* Display the string */
  MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );

  /* Free the string buffer returned by FormatMessage */
  LocalFree( lpMsgBuf );
  }

The Windows API provides functions for dealing with strings appropriately. They are prefixed with a lowercase L: lstrcpy(), lstrcat(), lstrcmp(), lstrcmpi(), etc.

Don't assume the system or windows directories are in any particular location.

And again, you may not have write access to the system directory. This is a security feature. The directory is for system DLLs. Not executables. Stick executables in the Windows directory or under Program Files.

Hope this helps.

[edit] BTW, I almost didn't look at this thread just for its title
Last edited on
Yeh sorry about the title, and thank you for the help and scince I've decided to change the directory to C:\\Program Files with a random named folder my chnage to theis question is how could I make it so I can input my own adress for the file destination and if I set a folder that doesn't excist will it create it?
Topic archived. No new replies allowed.