cannot convert parameter 1 from 'char' to 'LPCWSTR'
Asked Answered
S

5

32

I keep getting this error: cannot convert parameter 1 from 'char' 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?

Stanislas answered 13/10, 2010 at 14:45 Comment(0)
A
6

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);
Alvy answered 13/10, 2010 at 14:51 Comment(2)
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.Amphipod
@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.Alvy
I
129

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:

alt text

Incomputable answered 13/10, 2010 at 14:58 Comment(2)
Scouring around for hours till I found this thanksBandwidth
Now under Configuration Properties / Advanced.Gateway
A
6

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);
Alvy answered 13/10, 2010 at 14:51 Comment(2)
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.Amphipod
@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.Alvy
C
5

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.

Cristinecristiona answered 13/10, 2010 at 14:59 Comment(0)
F
2

Depending on your compiler setting for CharacterSet, you may need to perform a multibyte / widechar conversion, or change the CharacterSet if you don't care what it is.

For converting with MultiByteToWideChar, see the following...

http://www.codeguru.com/forum/showthread.php?t=231165

Fy answered 13/10, 2010 at 15:8 Comment(0)
C
0

I guess you're compiling with Unicode enabled. Then with char argv[], argv is a char array, so argv[1] is a char, and CreateFile wants a const wchar_t* as first parameter, not a char.

That said, your main definition is also broken, it should have char* argv[]. With that change, you can call CreateFileA.

Crucible answered 13/10, 2010 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.