Error C2664 Const Char To Lpcwstr
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more cannot convert parameter 2 from const char * to lpcwstr about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges c++ const char to lpcwstr Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each cannot convert from const char to lpctstr other. Join them; it only takes a minute: Sign up cannot convert parameter 1 from 'char' to 'LPCWSTR' up vote 21 down vote favorite 10 I keep getting this error: cannot convert parameter 1 from 'char' to 'LPCWSTR'
Cannot Convert From 'const Char [14]' To 'lpcwstr'
int main(int argc, char argv[]) { // open port for I/O HANDLE h = CreateFile(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if(h == INVALID_HANDLE_VALUE) { PrintError("E012_Failed to open port"); can someone help? c++ windows createfile lpcwstr share|improve this question edited Jun 8 '15 at 22:40 Joseph Stine 9551921 asked Oct 13 '10 at 14:45 sebastian 53361433 add a comment| 5 Answers 5 active oldest votes up vote 7 down vote accepted It should be int main(int argc, char* argv[]) And HANDLE h = CreateFileA(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); const wchar_t *' to 'lpcstr' share|improve this answer answered Oct 13 '10 at 14:51 ybungalobill 37.6k1074136 1 I don't thing using the ANSI version of CreateFile is the right way to go, next thing you know and he'll start changing all the function calls to use the ANSI version. –Nikola Smiljanić Oct 13 '10 at 15:00 1 @Nikola: Of course it's the wrong way! I just think that it's not appropriate to explain the difference between char, wchat_t, TCHAR and how to write correct unicode applications to an absolute beginner. –ybungalobill Oct 13 '10 at 15:04 add a comment| up vote 85 down vote Go to the Properties for your Project and under Configuration Properties/General, change the Character Set to "Not Set". This way, the compiler will not assume that you want Unicode characters, which are selected by default: share|improve this answer answered Oct 13 '10 at 14:58 Michael Goldshteyn 37.4k1381139 add a comment| up vote 4 down vote This is the main function that Visual Studio creates by default: int _tmain(int argc, _TCHAR* argv[]) Where _TCHAR is defined to be char or wchar_t depending if _UNICODE is defined or not. The same thing happens with API functions. I would advise you against using explicit CreateFileA. Change your main and use CreateFile. share|improve this answer answered Oct 13 '10 at 14:59 Nikola Smiljanić 21.6k43452 add a comment| up vote 1 down vote
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us cannot convert parameter 1 from 'char *' to 'lpcwstr' Learn more about Stack Overflow the company Business Learn more about hiring developers or
Cannot Convert Argument 1 From 'char *' To 'lpcwstr'
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow
Const Char Is Incompatible With Lpcwstr
Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up cannot convert parameter 1 from 'const char *' http://stackoverflow.com/questions/3924926/cannot-convert-parameter-1-from-char-to-lpcwstr to 'LPCWSTR' up vote 3 down vote favorite 1 Basically I have some simple code that does some things for files and I'm trying to port it to windows. I have something that looks like this: int SomeFileCall(const char * filename){ #ifndef __unix__ SomeWindowsFileCall(filename); #endif #ifdef __unix__ /**** Some unix only stat code here! ****/ #endif } the line SomeWindowsFileCall(filename); causes the compiler error: cannot convert parameter 1 http://stackoverflow.com/questions/10727568/cannot-convert-parameter-1-from-const-char-to-lpcwstr from 'const char *' to 'LPCWSTR' How do I fix this, without changing the SomeFileCall prototype? c winapi cross-platform share|improve this question asked May 23 '12 at 20:45 john-charles 5852821 LPCWSTR is const wchar_t * - which is a wide character string. –Mysticial May 23 '12 at 20:48 See Working with Strings for more details. –Adam Rosenfield May 23 '12 at 20:50 possible duplicate of cannot convert parameter 1 from 'char *' to 'LPCWSTR' –outis Jul 9 '12 at 19:42 add a comment| 4 Answers 4 active oldest votes up vote 8 down vote accepted Most of the Windows APIs that take strings have two versions: one that takes char * and one that takes WCHAR * (that latter is equivalent to wchar_t *). SetWindowText, for example, is actually a macro that expands to either SetWindowTextA (which takes char *) or SetWindowTextW (which takes WCHAR *). In your project, it sounds like all of these macros are referencing the -W versions. This is controlled by the UNICODE preprocessor macro (which is defined if you choose the "Use Unicode Character Set" project option in Visual Studio). (Some of Microsoft's C and C++ run time librar
Studio Languages , Windows Desktop Development > C++ Standards, Extensions, and Interop General discussion 5 Sign in to vote Note: This is a FAQ, not a question being asked. Question I'm trying to compile a piece of code https://social.msdn.microsoft.com/Forums/vstudio/en-US/c1b08c0a-a803-41c3-ac8c-84eba3be1ddb/faq-cannot-convert-from-const-char-to-lpctstr?forum=vclanguage such as: MessageBox("Hello world!"); ... when I compile the project, the compiler yields: error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR' What am I doing wrong? Problem This error message means that https://www.daniweb.com/programming/software-development/threads/210365/error-c2664-createfilew-cannot-convert-parameter-1-from-const-char-5-to-lpcwstr you are trying to pass a multi-byte string (const char [12]) to a function which expects a unicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*, where TCHAR is char when you compile for multi-byte and wchar_t for const char unicode. Since the compiler doesn't accept the char array, we can safely assume that the actual type of TCHAR, in this compilation, is wchar_t. Resolution You will have to do one of two things: Change your project configuration to use multibyte strings. Press ALT+F7 to open the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set". Indicate that the string literal, in this case "Hello world!" is of a specific const char to encoding. This can be done through either prefixing it with L, such as L"Hello world!", or surrounding it with the generic _T("Hello world!") macro. The latter will expand to the L prefix if you are compiling for unicode (see #1), and nothing (indicating multi-byte) otherwise. Variations Another error message, indicating the same problem, would be: cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR' Where LPCWSTR maps to a wchar_t pointer, regardless of your build configuration. This problem can be resolved primarily by using solution #2, but in some cases also #1. A lot of the Microsoft provided libraries, such as the Platform SDK, have got two variations of each function which takes strings as parameters. In case of a unicode build, the actual functions are postfixed W, such as the MessageBoxW seen above. In case of multi-byte, the function would be MessageBoxA (ASCII). Which of these functions is actually used when you compile your application, depends on the setting described in resolution #1 above. References and recommended reads Support for Unicode Generic-Text Mappings in Tchar.h Unicode and Character Sets Conventions for Function Prototypes Monday, October 23, 2006 2:27 PM Moderator Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.Would you like to participate
7 Years Ago Hello, I am doing simple read/write program for serial port. I am faceing a error that i don´t understand. Error is: "error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR' " My whole program: #include