سؤال

Given a message:

char *appStrt = "Application has already been started in a different window.";

I am trying to use it in the following function:

MessageBox(NULL, appStrt, // (LPCTSTR) appStrt cast doesn't work here
           appRun,
           MB_ICONWARNING | MB_OK);

how can I convert appStrt to LPCSTR so MessageBox doesn't complain? I have the same problem in the following peice of code:

DWORD dwStyle;
m_hWnd = CreateWindowEx(dwStyleEx,
                    m_pszClassName,
            dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, // complains here about dwStyle
            0,
                        etc.

CreateWindowEx is also throwing an error stating that dwStyle should be of type LPCWSTR. I searched online but only found conversions such as: LPCWSTR newWord = L"OldWord" but I need something that would convert a variable of type DWORD or char* to LPCWSTR.

هل كانت مفيدة؟

المحلول

Use TCHAR instead of char:

TCHAR appStrt[] = _T("Application has already been started in a different window.");

As for your second problem, you might want to check a reference of CreateWindowEx as you are missing an argument in the call before the styles.

نصائح أخرى

There's no simple conversion, just start with the right thing in the first place

TCHAR *appStrt = _T("Application has already been started in a different window.");

For the second example you've got your parameters in the wrong order. For CreateWindowEx the third parameter is the window title, the fourth parameter is the window style. You've put the window style where the window title should go.

As a general principle, don't take compiler error messages too literally. If the compiler says cannot convert xxx to yyy, it doesn't always mean that you do need to convert xxx to yyy. Both these examples illustrate that.

You can use MessageBoxA() instead, then you do not have to convert your char* data (especially useful if the data is dynamically allocated at runtime instead of statically like in your example - otherwise, use MultiByteToWideChar() and then call MessageBoxW()).

As for CreateWindowEx(), you are passing your window style value in the lpWindowName parameter instead of the dwStyle parameter, that is why the compiler is complaining about LPCWSTR.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top