views:

126

answers:

1

Hi,

I have a problem with GetCommandLine API.

It usually returns the executable name followed by a space and arguments. As documentation says, the first token may not have complete path to image and blah blah blah.

I never had problems until now that I used CreateProcess with lpApplicationName not NULL

If I use:

CreateProcess(NULL, "\"c:\myexe.exe\" param1 param2", ...)

GetCommandLine returns "c:\myexe.exe param1 param2" as expected.

But if I use:

CreateProcess("c:\myexe.exe", "param1 param2")

GetCommandLine returns only "param1 param2".

So the question is, how do I know if executable name is given in cmd-line if another app launches mine?

Also, MFC startup code assumes that the first token in cmdline is the executable name and skips it. But if you launch a MFC app with the second CreateProcess api example, MFC's code will skip the first argument.

Regards, Mauro.

A: 

Mauro,

First of all, sorry for my English.

Do you have any progress?

I have the problem too but I would not able to find any information in the web. Nevertheless you described this problem very well.

I have a workaround which can be helpful in a case like this. I guess we always be able to check how our module was been started. In this case we should check first argument.

I will write code because I have some problem with English. Here two ways:

The first case. we can compare module name with first command line argument. something like this:

const TCHAR* csCommandLine = ::GetCommandLine();

// Attention!!! the first symbol can be quete

if (*csCommandLine == _T('\"'))
    csCommandLine++;

TCHAR sModuleFileName[MAX_PATH];

DWORD dwModuleFileName = ::GetModuleFileName(NULL, sModuleFileName, MAX_PATH);

if (dwModuleFileName && !_tcsncmp(csCommandLine, sModuleFileName, dwModuleFileName)) {

    // The command line contains the module name.
}

The second case. we can try to get file attributes for the first command line argument something like this:

// Attention!!! don't use it case if you are going to pass a file path in command line arguments.

int nArgc;

LPTSTR* szArglist = ::CommandLineToArgvW(::GetCommandLine(), &nArgc);

if (nArgc && ::GetFileAttributes(szArglist[0]) != INVALID_FILE_ATTRIBUTES) {

    // The command line contains the module name.
}

::LocalFree(szArglist);

I hope it can be helpful someone.

Regards, Vladimir

Vladimir