Convert string to LPCWSTR for CreateFile () for serial port addressing

It seems I have a problem with TEXT / UNICODE when using the CreateFile function to address the serial port. Can someone please indicate my mistake?

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

I can create a descriptor for the serial port as follows:

#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;
}

This works very well (a bit \\\\.\\is required to build more than COM9 and works for those prior to COM9 as well). The problem is that my comport will not always be COM20, so I would like the user to 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;
}

: C2664: "CreateFileW": 1 "std::string" "LPCWSTR"

, , , , CreateFileA , .

:

/*
everything else the same
*/   

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

: C2065: "LcomID":

, . - , L"\\\\.\\COM20" , comport CreateFile ? !

+3
6

std::wstring std::wcin, std::wcout "unicode", Microsoft conversion .

1- (), c_str(), LPCWSTR ( const wchar_t).

( CreateFileW UNICODE):

#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;
}
+5

std::string, c_str() CreateFileA(), :

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

ATL (: CA2CW ..). ( ) inline, .

std::string, :

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

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

0
source

I came across this recently. I simply disabled Unicode, as Unicode was completely unavailable for this application.

0
source

General information about string conversions here: http://www.codeproject.com/KB/string/cppstringguide2.aspx

0
source

Source: https://habr.com/ru/post/1766312/


All Articles