CopyFile()

Hi, i wanna know how to copy a file if you don't know its name. For example:
me.exe -> purple.exe.

i know how to do this with argv[0], but how do i do it if i have a int WINAPI WinMain(), where there is no char* argv[]?

Let me explain it better

1
2
3
4
5
int WINAPI WinMain(HINSTANCE hInstance,...)
{
    CopyFile([???],"purple.exe",0);

}


what do i put instead of [???] ?
You can pass arguments to a win32 app. The PWSTR pCmdLine is what gets these command-line arguments. If you want to get a Unicode copy of these arguments, you can call the GetCommandLine(); function. and to make it the argv[] style, call CommandLineToArgvW(); function
There are external variables __argc and __argv that you can use.

For example:
1
2
3
4
5
6
7
8
void doCopy()
{
    extern int __argc;
    extern char** __argv;

    if (__argc == 3)
        CopyFile(__argv[1], __argv[2]);
}
Compile this as Unicode and __argv will be a NULL pointer, so the program will crash. __argc will be correct.
In which case you need to use GetCommandLine to get the actual command line used to start the app (as a raw string), then use CommandLineToArgvW to generate wide argc/argv values.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391%28v=vs.85%29.aspx

Putting it all together, you get:
1
2
3
4
5
6
7
8
9
10
11
12
13
void doCopy()
{
#ifndef _UNICODE
    extern int __argc;
    extern char** __argv;
#else
    int __argc;
    LPWSTR* __argv = CommandLineToArgvW(GetCommandLine(), &argc);
#endif

    if (__argc == 3)
        CopyFile(__argv[1], __argv[2]);
}
Last edited on
Yes, it will be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR szCmdLine,
				   int iCmdShow)
{

LPWSTR *argv;
	int argc = 0;
	argv = CommandLineToArgvW(GetCommandLineW(), &argc);



// do your work here


// Free memory allocated for CommandLineToArgvW arguments.
	::LocalFree(argv);
return 0;
}
@kbw thanks for making me know about these variables
Topic archived. No new replies allowed.