简体   繁体   中英

Converting a string to LPCWSTR for CreateFile() to address a serial port

I seem to be having a bit of a TEXT / UNICODE problem when using the windows CreateFile function for addressing a serial port. Can someone please help point out my error?

I'm writing a Win32 console application in VC++ using VS 2008.

I can create a handle to address the serial port like this:

#include <iostream>    
#include <windows.h>
#include <string>

int main()
{

   HANDLE hSerial;

   hSerial = CreateFile( L"\\\\.\\COM20",
                         GENERIC_READ | GENERIC_WRITE,
                         0,
                         0,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);`

   return 0;
}

That works just fine (the \\\\\\\\.\\\\ bit is required for comports greater than COM9 and works for those up to COM9 also). The problem is that my comport will not always be COM20, so I'd like to have the user specify what it is.

Here are some things I've tried:

#include <iostream>    
#include <windows.h>
#include <string>

int main()
{
   std::string comNum;
   std::cout << "\n\nEnter the port (ex: COM20): ";
   std::cin >> comNum;
   std::string comPrefix = "\\\\.\\";
   std::string comID = comPrefix+comNum;

   HANDLE hSerial;

   hSerial = CreateFile( comID,
                         GENERIC_READ | GENERIC_WRITE,
                         0,
                         0,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);`

   return 0;
}

This does not compile and returns the error: error C2664: 'CreateFileW' : cannot convert parameter 1 from 'std::string' to 'LPCWSTR'

I thought maybe specifying CreateFileA would work then, but that gave basically the same error.

I also tried :

/*
everything else the same
*/   

hSerial = CreateFile( TEXT(comID),
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL,
                      0);`

which also does not compile and returns: error C2065: 'LcomID' : undeclared identifier

I am not much of an expert but I have been working on this for a while now. Can someone tell me how to replace L"\\\\\\\\.\\\\COM20" in such a way that the user can specify the comport and so that CreateFile will still work? Thanks!

You can either use std::wstring and std::wcin , std::wcout to perform input directly in "unicode strings", or you can look into Microsoft's conversion functions .

If you go for the 1st option (recommended), you still need to use the c_str() function to gain access to a LPCWSTR value (pointer to const wchar_t ).

Sample solution (also not use of CreateFileW syntax, to prevent issues with UNICODE macro):

#include <iostream>    
#include <windows.h>
#include <string>

int main()
{
   std::wstring comNum;
   std::wcout << L"\n\nEnter the port (ex: COM20): ";
   std::wcin >> comNum;
   std::wstring comPrefix = L"\\\\.\\";
   std::wstring comID = comPrefix+comNum;

   HANDLE hSerial;

   hSerial = CreateFileW( comID.c_str(),
                     GENERIC_READ | GENERIC_WRITE,
                     0,
                     0,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_NORMAL,
                     0);`

   return 0;
}

如果你想继续使用std::string ,那么在调用CreateFileA()时使用它的c_str()方法,例如:

hSerial = CreateFileA( comID.c_str(), ...);

As an additional reference, have a look at the ATL conversions macros (eg: CA2CW, etc.). These (if used properly) can handle pretty much all simple conversion cases inline, with little fuss.

In your example, you can use std::string, then use:

CreateFile( CA2CT( comID.c_str() ), ... );

If (for some reason) you decide to keep working with ANSI strings, then take a look at the MultiByteToWideChar function. But ANSI strings are pretty much obsolete.

I ran into this recently. I simply disabled Unicode, as Unicode was wholly out of scope for this application.

有关字符串转换的一般信息,请访问: http//www.codeproject.com/KB/string/cppstringguide2.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM