943,928 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8181
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 10th, 2009
0

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

Expand Post »
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
Reputation Points: 4
Solved Threads: 0
Light Poster
jp071 is offline Offline
26 posts
since Aug 2009
Aug 10th, 2009
0

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

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):
C++ Syntax (Toggle Plain Text)
  1. LPCWSTR abc = "COM1";
and rewrite the createfile to :
C++ Syntax (Toggle Plain Text)
  1. HANDLE hPort = CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Aug 10th, 2009
0

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

Thanks,
But it still have new error. That is
"error C2440: 'initializing' : cannot convert from 'const char [5]' to 'LPCWSTR' "
Please chack it again.
Reputation Points: 4
Solved Threads: 0
Light Poster
jp071 is offline Offline
26 posts
since Aug 2009
Aug 10th, 2009
0

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

try the below one
C++ Syntax (Toggle Plain Text)
  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 .
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 10th, 2009
0

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

Just a curiosity, what Visual Studio version are you using ?
Because i tryied both :
C++ Syntax (Toggle Plain Text)
  1. LPCWSTR abc = "COM1";
  2. CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
and
C++ Syntax (Toggle Plain Text)
  1. CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
And both worked under Visual Studio 2008 Professional.
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Aug 10th, 2009
0

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

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.
Reputation Points: 4
Solved Threads: 0
Light Poster
jp071 is offline Offline
26 posts
since Aug 2009
Aug 10th, 2009
0

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

yes Its seems fine with a cursory look.

Good luck (Y).
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 10th, 2009
0

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

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.
Reputation Points: 4
Solved Threads: 0
Light Poster
jp071 is offline Offline
26 posts
since Aug 2009
Aug 10th, 2009
0

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

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 20th, 2009
-1
Re: error C2664:'CreateFileW':cannot convert parameter 1 from 'const char [5]'to'LPCWSTR'
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.
fyp
Reputation Points: 9
Solved Threads: 0
Newbie Poster
fyp is offline Offline
10 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Unique ID
Next Thread in C++ Forum Timeline: Ugh... Need some help with allegro and stuff





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC