| | |
error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 21
Reputation:
Solved Threads: 0
error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#1 Aug 10th, 2009
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 <windows.h>
#include <iostream>
#include <string>
using namespace std;
void set_com_pin(bool value);
int main(int argc, char* argv[])
{
if(argc != 2)
{
cerr<<"bad usage !"<<endl;
return -1;
}
bool onoff;
if(string(argv[1])==string("-on"))onoff=true;
else if(string(argv[1])==string("-off"))onoff=false;
else
{
cerr<<"bad parameters !"<<endl;
return -1;
}
set_com_pin(onoff);
return 0;
}
void set_com_pin(bool value)
{
// open port for I/O
HANDLE hPort = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hPort == INVALID_HANDLE_VALUE) {
cout<<"Failed to open port" <<endl;
} else {
// set timeouts
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
if(!SetCommTimeouts(hPort,&cto))
cout<<"SetCommTimeouts failed" <<endl;
// set DCB
DCB dcb;
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 19200;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
// dcb.fOutxCtsFlow = 1;
// dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(hPort,&dcb))
cout<<"SetCommState failed" << endl;
char buf[7];
DWORD read=0;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port
WriteFile(hPort, buf, write, &write, NULL);
ReadFile(hPort, buf, sizeof(buf), &read, NULL);
DWORD i;
for(i=0; i<read; i++)
cout<<"%i", (unsigned char)buf[i];
CloseHandle(hPort);
}
}
Thanks in advance
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 <windows.h>
#include <iostream>
#include <string>
using namespace std;
void set_com_pin(bool value);
int main(int argc, char* argv[])
{
if(argc != 2)
{
cerr<<"bad usage !"<<endl;
return -1;
}
bool onoff;
if(string(argv[1])==string("-on"))onoff=true;
else if(string(argv[1])==string("-off"))onoff=false;
else
{
cerr<<"bad parameters !"<<endl;
return -1;
}
set_com_pin(onoff);
return 0;
}
void set_com_pin(bool value)
{
// open port for I/O
HANDLE hPort = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hPort == INVALID_HANDLE_VALUE) {
cout<<"Failed to open port" <<endl;
} else {
// set timeouts
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
if(!SetCommTimeouts(hPort,&cto))
cout<<"SetCommTimeouts failed" <<endl;
// set DCB
DCB dcb;
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 19200;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
// dcb.fOutxCtsFlow = 1;
// dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(hPort,&dcb))
cout<<"SetCommState failed" << endl;
char buf[7];
DWORD read=0;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port
WriteFile(hPort, buf, write, &write, NULL);
ReadFile(hPort, buf, sizeof(buf), &read, NULL);
DWORD i;
for(i=0; i<read; i++)
cout<<"%i", (unsigned char)buf[i];
CloseHandle(hPort);
}
}
Thanks in advance
•
•
Join Date: Jan 2009
Posts: 106
Reputation:
Solved Threads: 5
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#2 Aug 10th, 2009
As the error says, the first parameter must be LPCWSTR and not directly "COM1", so you can try writing the next(before the createfile call):
and rewrite the createfile to :
C++ Syntax (Toggle Plain Text)
LPCWSTR abc = "COM1";
C++ Syntax (Toggle Plain Text)
HANDLE hPort = CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
•
•
Join Date: Aug 2009
Posts: 21
Reputation:
Solved Threads: 0
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#3 Aug 10th, 2009
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#4 Aug 10th, 2009
try the below one
Hope the above help, you didn't ask what is LPCWSTR ok
. So I am providing the solution only
.
C++ Syntax (Toggle Plain Text)
//HANDLE hPort = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); HANDLE hPort = CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Hope the above help, you didn't ask what is LPCWSTR ok
. So I am providing the solution only
. •
•
Join Date: Jan 2009
Posts: 106
Reputation:
Solved Threads: 5
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#5 Aug 10th, 2009
Just a curiosity, what Visual Studio version are you using ?
Because i tryied both :
and
And both worked under Visual Studio 2008 Professional.
Because i tryied both :
C++ Syntax (Toggle Plain Text)
LPCWSTR abc = "COM1"; CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
C++ Syntax (Toggle Plain Text)
CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
•
•
Join Date: Aug 2009
Posts: 21
Reputation:
Solved Threads: 0
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#6 Aug 10th, 2009
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#7 Aug 10th, 2009
•
•
Join Date: Aug 2009
Posts: 21
Reputation:
Solved Threads: 0
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#8 Aug 10th, 2009
Hello jan140,
I am using Microsoft Visual Studio 2008.
LPCWSTR abc = "COM1";
CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
This shows an error that i mentioned before, but
CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
This is OK. I don´t know why!!!
Thanks for your help.
I am using Microsoft Visual Studio 2008.
LPCWSTR abc = "COM1";
CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
This shows an error that i mentioned before, but
CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
This is OK. I don´t know why!!!
Thanks for your help.
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
0
#9 Aug 10th, 2009
http://msdn.microsoft.com/en-us/libr...h6(VS.71).aspx
Visual Studio 6 for example defaults to ANSI
Visual Studio 2008 defaults to UNICODE
The Win32 API functions (like CreateFile) are already wrapped, but many standard functions in the CRT are not.
Also, you need to use the TEXT() or _T() macro for ALL your string constants.
Read the rest of the MSDN link.
Visual Studio 6 for example defaults to ANSI
Visual Studio 2008 defaults to UNICODE
The Win32 API functions (like CreateFile) are already wrapped, but many standard functions in the CRT are not.
Also, you need to use the TEXT() or _T() macro for ALL your string constants.
Read the rest of the MSDN link.
![]() |
Similar Threads
- error C2664: 'F_Ite' : cannot convert parameter 1 from 'double *' to 'double' (C++)
- error C2664: 'setStartupDirectory' : cannot convert parameter (C++)
- Help Need for Resolving a C++ error C2664 (C++)
- Having trouble calling certain functions (C++)
- error C2664: 'strcpy' : cannot convert parameter 2 from 'const char' to 'const char * (C++)
- VS compiler error??? (C++)
- Neophyte requests assistance... (C++)
- Why program works in Dev-C++ and not in VC++ 2005? (C++)
Other Threads in the C++ Forum
- Previous Thread: Unique ID
- Next Thread: Ugh... Need some help with allegro and stuff
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






