Hi
#include<windows.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MessageBox(NULL,"test","test",MB_OK);
return 0;
}
a.c
.\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'
.\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'
how to solve that problem

incompatible types - from 'char [4]' to 'LPCWSTR'
AustinNetherwood
Each windows function like MessageBox is actually a macro which emits MessageBoxA when UNICODE is not defined, and, MessageBoxW when UNICODE is defined.
To solve the above problem, you have to write
MessageBox(NULL, L"test", L"test", MB_OK);But after somedays you want to create a ansi version of the code and you undefine UNICODE, then you will have to change the text in all places.There is a better alternative available to this problem, all you have to do is write the statement in the following way
MessageBox(NULL, TEXT("test"), TEXT("test"), MB_OK);Here, the TEXT macro will put an L if UNICODE is defined else write the text as it is.I hope this will answer your query.
NicoRi
Marian, did you change "test" to L"test" in both places and get a correct compile and execution
I was able to do that. I also documented how I confirmed that was what the error message was about.
- Dennis
mnoon
I see, thanks for that. I haven't tried VC++ Express Edition yet.
Cheers,
-chris
Joe C...
How are you compiling your program I mean, your program looks fine. Although not neccessary, you can try casting your literal string to LPTSTR, as follows:
#include<windows.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MessageBox(NULL, (LPTSTR)"test" , (LPTSTR)"test", MB_OK);
return 0;
}
Regards,
-chris
AndyPick
thanks :)
HoundOfHell
but....
: error C2065: 'TEXT' : undeclared identifier
dotNetFan
nLella
Hey thanks for the help. I'm just starting out with forgers win32api tutorial, using 2005 Express Edition. Your suggestions seem to have done the trick.
Thanks guys
cpavlov
I am offering two answers.
First, try L"test" in both places that you have "test". (This sitll may not be enough, I'm guessing).
Secondly, here is how to handle compiler errors in general. The first part of the message names the location of the file and the line of the file that the warning is about.
You can double-click on the line in the error window and VC++ will bring up the file and move to the correct line.
Because char[4] is most likely what VC++ says the type of string "test" is, it looks like both of them do not agree with what is required for the two parameters. (See below about this.)
To find out what is required for LPCWSTR, you can search for that in VC++ Help Search, and you can search for MessageBox too. In my LPCWSTR search, I found a link to a previous question, "HELP with LPCWSTR!" that provided the correct answer. Some of this material is a little obscure, and I found the API page for MessageBox by using a Web Search and coming up with this helpful page.
I got a different error message than you, and I wonder if your compiler settings are the same as mine (and whether you are using VC++ 2005). Here's what happened. (I have the Platform SDK installed, so I am able to work your example.)
1. I created a project by clicking New | Project ...
2. I selected project type Win32 and the Win32 Console Application project template. I named the project MessageTest. I unchecked Create directory for solution.
3. In the application settings dialog, I selected the Windows Application application type and checked Empty project.
4. After the solution was opened, I right-clicked the Source Files folder in Solution Explorer and selected Add | New Item ... . In the Add New Item dialog I selected category Visual C++ and the C++ file (.cpp) installed template. I named the item MessageTest.
5. When the blank MessageTest.cpp page opened in VC++, I pasted in the 7 lines of your program.
6. When I compiled it (F7 key), the error message that I got was as follows (in one line):
7. In the Output window there is the additional message:
This is not all that helpful, but the other information (6) is more accurate than what you reported.
8. I have my Project Settings with Warning Level W3 but I do not have warnings treated as errors.
I'm curious, what compiler are you using My Visual C++ 2005 Express Edition seems more uptodate.
- Dennis
AirBear
Chris, I think the problem is that the program is being compiled for Unicode (wide strings), the VC++ 2005 Express Edition default for a Win32 application. Use of the char[5] array "test" is not compatible. (LPTSTR) might be accepted for an LPCWSTR, but I hope not.
- Dennis
PS: Bad news. The (LPTSTR) cast works but the program produces garbage because the cast is not the truth. I will leave as an exercise why exactly two garbage characters are produced and not some random long sequence of trash instead.
jMerliN
Thanks,
Chintan.
junfanjohn
Hi: make sure you add:
#include <tchar.h>
to your includes.