954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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
#include
#include

using namespace std;

void set_com_pin(bool value);


int main(int argc, char* argv[])
{
if(argc != 2)
{
cerr<<"bad usage !"<

jp071
Light Poster
29 posts since Aug 2009
Reputation Points: 4
Solved Threads: 0
 

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):

LPCWSTR abc = "COM1";

and rewrite the createfile to :

HANDLE hPort = CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
 

Thanks,
But it still have new error. That is
"error C2440: 'initializing' : cannot convert from 'const char [5]' to 'LPCWSTR' "
Please chack it again.

jp071
Light Poster
29 posts since Aug 2009
Reputation Points: 4
Solved Threads: 0
 

try the below one

//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 :P.

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 

Just a curiosity, what Visual Studio version are you using ?
Because i tryied both :

LPCWSTR abc = "COM1";
	CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

and

CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

And both worked under Visual Studio 2008 Professional.

jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
 

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.

jp071
Light Poster
29 posts since Aug 2009
Reputation Points: 4
Solved Threads: 0
 

yes Its seems fine with a cursory look.

Good luck (Y).

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 

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.

jp071
Light Poster
29 posts since Aug 2009
Reputation Points: 4
Solved Threads: 0
 

http://msdn.microsoft.com/en-us/library/7dzey6h6(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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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
Newbie Poster
10 posts since Oct 2009
Reputation Points: 9
Solved Threads: 0
 

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.

I think, it will helpful to you.

// Open the serial port.
HANDLE hPort = CreateFile (TEXT("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hPort == INVALID_HANDLE_VALUE) {
	cout<<"hPort INVALID" <<endl;
} else{
	cout<< "Handle Port Success" <<endl; 


DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;					// Current baud 
PortDCB.fBinary = TRUE;						// Binary mode; no EOF check 
PortDCB.fParity = TRUE;						// Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;				// No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;				// No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;	// DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;			// DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;			// XOFF continues Tx 
PortDCB.fOutX = FALSE;						// No XON/XOFF out flow control 
PortDCB.fInX = FALSE;						// No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;					// Disable error replacement 
PortDCB.fNull = FALSE;						// Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;	// RTS flow control 
PortDCB.fAbortOnError = FALSE;				// Do not abort reads/writes on error
PortDCB.ByteSize = 8;						// Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;					// 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;				// 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not configure the serial port.
  cout<<"SetCommState failed" <<endl;
  return FALSE;
}


 
// Retrieve the timeout parameters for all read and write operations on the port. 
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 50;  
CommTimeouts.ReadTotalTimeoutMultiplier = 50;  
CommTimeouts.ReadTotalTimeoutConstant = 50;    
CommTimeouts.WriteTotalTimeoutMultiplier = 50;  
CommTimeouts.WriteTotalTimeoutConstant = 50;    

// Set the timeout parameters for all read and write operations on the port. 
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
  cout<<"SetCommTimeouts failed" <<endl;
  return FALSE;
}
}

char buf[20];
DWORD read = 0;
DWORD CommModemStatus;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port

while (hPort != INVALID_HANDLE_VALUE) 
{
WaitCommEvent(hPort, &CommModemStatus, 0);
//cout<<"Wait Completed" << endl;
//cout<< CommModemStatus;
if (CommModemStatus!=0)
{

WriteFile(hPort, buf, write, &write, NULL); // write is updated with the number of bytes written

ReadFile(hPort, buf, sizeof(buf), &read, NULL); // read is updated with the number of bytes read

//write code to Print date

}
}

CloseHandle(hPort);
jp071
Light Poster
29 posts since Aug 2009
Reputation Points: 4
Solved Threads: 0
 

thank you,jp071. i got it :-)

fyp
Newbie Poster
10 posts since Oct 2009
Reputation Points: 9
Solved Threads: 0
 

I think, it will helpful to you.

// Open the serial port.
HANDLE hPort = CreateFile (TEXT("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hPort == INVALID_HANDLE_VALUE) {
	cout<<"hPort INVALID" <<endl;
} else{
	cout<< "Handle Port Success" <<endl; 


DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;					// Current baud 
PortDCB.fBinary = TRUE;						// Binary mode; no EOF check 
PortDCB.fParity = TRUE;						// Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;				// No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;				// No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;	// DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;			// DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;			// XOFF continues Tx 
PortDCB.fOutX = FALSE;						// No XON/XOFF out flow control 
PortDCB.fInX = FALSE;						// No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;					// Disable error replacement 
PortDCB.fNull = FALSE;						// Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;	// RTS flow control 
PortDCB.fAbortOnError = FALSE;				// Do not abort reads/writes on error
PortDCB.ByteSize = 8;						// Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;					// 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;				// 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not configure the serial port.
  cout<<"SetCommState failed" <<endl;
  return FALSE;
}


 
// Retrieve the timeout parameters for all read and write operations on the port. 
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 50;  
CommTimeouts.ReadTotalTimeoutMultiplier = 50;  
CommTimeouts.ReadTotalTimeoutConstant = 50;    
CommTimeouts.WriteTotalTimeoutMultiplier = 50;  
CommTimeouts.WriteTotalTimeoutConstant = 50;    

// Set the timeout parameters for all read and write operations on the port. 
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
  cout<<"SetCommTimeouts failed" <<endl;
  return FALSE;
}
}

char buf[20];
DWORD read = 0;
DWORD CommModemStatus;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port

while (hPort != INVALID_HANDLE_VALUE) 
{
WaitCommEvent(hPort, &CommModemStatus, 0);
//cout<<"Wait Completed" << endl;
//cout<< CommModemStatus;
if (CommModemStatus!=0)
{

WriteFile(hPort, buf, write, &write, NULL); // write is updated with the number of bytes written

ReadFile(hPort, buf, sizeof(buf), &read, NULL); // read is updated with the number of bytes read

//write code to Print date

}
}

CloseHandle(hPort);

>>>>Hi,
if i want to transfer .txt file instead of message,how could i do?

fyp
Newbie Poster
10 posts since Oct 2009
Reputation Points: 9
Solved Threads: 0
 

Dear All,
I want to read ini file for configuration. Who can send me a simple code?
Thank you in advance
Hoshang

shahinkey
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
Dear All, I want to read ini file for configuration. Who can send me a simple code? Thank you in advance Hoshang

Sorry to say this but we're not a coding service meaning we don't provide code but only helps members from theirs

If you need further help start a new thread (instead of replying to an old one) if your troubled by a problematic code

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You