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 );
}
|