Martin
Martin

Reputation: 3472

How to #define hard coded strings when L prefix (Visual C++ compiler) is used?

How to #define the path L"C:\Windows\System32\taskmgr.exe" for handling wide char

#define TASK_MGR "C:\\Windows\\System32\\taskmgr.exe"

KillProcess(TASK_MGR); //this works

HINSTANCE resurrect = ShellExecute(NULL, L"open", L"C:\\Windows\\System32\\taskmgr.exe", NULL, NULL, SW_MINIMIZE); 

Upvotes: 1

Views: 1231

Answers (3)

Simon Richter
Simon Richter

Reputation: 29618

You can use string concatenation:

#define TASK_MGR "C:\\Windows\\System32\\taskmgr.exe"

/* ... */

HINSTANCE resurrect = ShellExecute(NULL, L"open", L"" TASK_MGR, NULL, NULL, SW_MINIMIZE);

Personally, I'd go with

static TCHAR const TASK_MGR[] = _T("C:\\Windows\\System32\\taskmgr.exe");

The usual rant on hard-coded path names also applies.

Upvotes: 3

Cody Gray
Cody Gray

Reputation: 244843

You need to use multiple macros. Fortunately, the Windows headers already define such a macro that widens a string literal when necessary, TEXT(), so there's no good reason to write your own.

The following code works fine:

#define TASK_MGR "C:\\Windows\\System32\\taskmgr.exe"

KillProcess(TASK_MGR);  // Not sure what KillProcess is or why it takes a narrow
                        // string, regardless of whether Unicode is defined...
                        // The Win32 function is named TerminateProcess.

HINSTANCE resurrect = ShellExecute(NULL, L"open", TEXT(TASK_MGR), NULL, NULL,
                                   SW_MINIMIZE); 

...well, except for the fact that you hard-coded a path to Task Manager and it's not going to be found at that location on all machines (like mine, for example). But I trust that this is just for example purposes only and you already know well not to hard-code paths.

Upvotes: 4

Reci
Reci

Reputation: 4274

Which version of Visual C++ are you using? This works on Visual Studio 2008:

#define PATH L"C:\\Windows\\System32\\taskmgr.exe";
void func()
{
    const wchar_t *test = PATH;
}

If, as Xeo commented, you want to widen the char array, use MultiByteToWideChar.

Upvotes: 3

Related Questions