Error: cannot convert parameter 2 from 'char *' to 'LPCTSTR'

Can you help to fix this problem?

1
2
3
4
5
6
7
8
9
10
try
{
control = new Controller (hwnd);
}
catch (WinException e)
{
MessageBox (0, e.GetMessage (),
_T("Exception"), 
MB_ICONEXCLAMATION | MB_OK);
}

main.cpp(133): error C2664: 'MessageBox' : cannot convert parameter 2 from 'char *' to 'LPCTSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Similar:
1
2
char buf [100];
wsprintf (buf, "Error x%x", GetLastError ());

error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char [100]' to 'LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2664: 'MessageBox' : cannot convert parameter 2 from 'char [100]' to 'LPCTSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1
2
// MessageBox (0, e.GetMessage (), _T("Exception"), MB_ICONEXCLAMATION | MB_OK);
MessageBoxA (0, e.GetMessage (), "Exception" , MB_ICONEXCLAMATION | MB_OK);


And
1
2
3
char buf [100];
// wsprintf (buf, "Error x%x", GetLastError ());
std::sprintf( buf, "Error x%x", GetLastError() );
You are the one, who attempts to use those 'wsprintfW' and 'MessageBox' that are not part of C++ standard, so you should read their documentation in order to know the parameters that they do take, and recursively read the description of types 'LPWSTR' and 'LPCTSTR' and how you create such variables.
JLBorges: Thanks, but still error:
main.cpp(137): error C2664: 'MessageBox' : cannot convert parameter 2 from 'char *' to 'LPCTSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(172): error C2664: 'MessageBox' : cannot convert parameter 2 from 'char [100]' to 'LPCTSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

How to convert std::string to LPCTSTR when _UNICODE not defined? I hope is there some function for it?
Last edited on
I don't see any std::string in your code.

Your questions are about proprietary Windows API, not about standard C++.
keskiverto: No need to be rude. His question is not about the API itself and how to use it, but about how to call functions and get back results from them, which are both in-topic for this forums.
Even if the questions were off-topic, that's still no excuse for bad manners.
It causes the e.GetMessage()

I dont see GetMessage returning LPCTSTR but boolean and I did not write the code
Last edited on
@OP:

I have never seen this WinException class before... nor can I find any documentation on MSDN about it. Furthermore, WinAPI is a C library so it doesn't even have exceptions.

Is this a class you have created? Or is it part of some other library?

If you have created it, can you post it here so I can see it? If not, can you link me to some online documentation?

EDIT:

Also... Controller has a ctor? Is this GDI+ or something? =x
Last edited on
Disch:
I have downloaded the code from this tutorial and I want to make it working to see how the program works:

http://www.relisoft.com/win32/windlg.html

1
2
3
4
5
6
7
8
9
10
11
12
class WinException
{
public:
    WinException (char* msg)
    : _err (GetLastError()), _msg(msg)
    {}
    DWORD GetError() const { return _err; }
    char* GetMessage () const { return _msg; }
private:
    DWORD _err;
    char* _msg;
};
Last edited on
In that case... JLBorges had the correct response:

MessageBoxA (0, e.GetMessage (), "Exception" , MB_ICONEXCLAMATION | MB_OK);

Note the usage of MessageBoxA and not MessageBox. Also note that there's no _T macro around the "Exception" string.


MessageBox - takes TCHAR strings
MessageBoxA - takes char strings
MessageBoxW - takes wchar_t strings


Since you have a char string from WinException, you will need the char form of the function: MessageBoxA
Last edited on
Thank you, I did not noticed the difference name of MessageBoxA
Last edited on
> In that case...

There couldn't have been any other case.

It is obvious prom the diagnostic
cannot convert parameter 2 from 'char *' to ...
that WinException::GetMessage() returns a (non const-correct) char*
Topic archived. No new replies allowed.