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: jp071 has a little shameless behaviour in the past 
Solved Threads: 0
jp071 jp071 is offline Offline
Newbie Poster

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

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):
  1. LPCWSTR abc = "COM1";
and rewrite the createfile to :
  1. HANDLE hPort = CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 21
Reputation: jp071 has a little shameless behaviour in the past 
Solved Threads: 0
jp071 jp071 is offline Offline
Newbie Poster

Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'

 
0
  #3
Aug 10th, 2009
Thanks,
But it still have new error. That is
"error C2440: 'initializing' : cannot convert from 'const char [5]' to 'LPCWSTR' "
Please chack it again.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'

 
0
  #4
Aug 10th, 2009
try the below one
  1. //HANDLE hPort = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  2. 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 .
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

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 :
  1. LPCWSTR abc = "COM1";
  2. CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
and
  1. CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
And both worked under Visual Studio 2008 Professional.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 21
Reputation: jp071 has a little shameless behaviour in the past 
Solved Threads: 0
jp071 jp071 is offline Offline
Newbie Poster

Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'

 
0
  #6
Aug 10th, 2009
Thanks, Now it didn´t show error.
I want to read/write data through "COM1". Could you check my program, am i in correct way? This is my first program for serial port/Embedded programming.

Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'

 
0
  #7
Aug 10th, 2009
yes Its seems fine with a cursory look.

Good luck (Y).
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 21
Reputation: jp071 has a little shameless behaviour in the past 
Solved Threads: 0
jp071 jp071 is offline Offline
Newbie Poster

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: fyp is an unknown quantity at this point 
Solved Threads: 0
fyp fyp is offline Offline
Newbie Poster
 
-1
  #10
Oct 20th, 2009
Hi,
I'm trying simple programme to open the visual com port and send data on it. could someone help me to advice the source code or the way to start? I'm using VC++ Express edition.

Thanks in advance.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC