c++ - Converting a string to LPCWSTR for CreateFile() to address a serial port -
i seem having bit of text / unicode problem when using windows createfile function addressing serial port. can please point out error?
i'm writing win32 console application in vc++ using vs 2008.
i can create handle address serial port 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 fine (the \\\\.\\ bit required comports greater com9 , works com9 also).  problem comport not com20, i'd have user specify is.
here things i've tried:
#include <iostream>     #include <windows.h> #include <string>  int main() {    std::string comnum;    std::cout << "\n\nenter 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 not compile , returns error: error c2664: 'createfilew' : cannot convert parameter 1 'std::string' 'lpcwstr'
i thought maybe specifying createfilea work then, gave same error.
i tried :
/* else same */     hserial = createfile( text(comid),                       generic_read | generic_write,                       0,                       0,                       open_existing,                       file_attribute_normal,                       0);`   which not compile , returns: error c2065: 'lcomid' : undeclared identifier
i not of expert have been working on while now.  can tell me how replace l"\\\\.\\com20" in such way user can specify comport , createfile still work?  thanks!
you can either use std::wstring , std::wcin, std::wcout perform input directly in "unicode strings", or can microsoft's conversion functions.
if go 1st option (recommended), still need use c_str()  function gain access lpcwstr value (pointer const wchar_t).
sample solution (also not use of createfilew syntax, prevent issues unicode macro):
#include <iostream>     #include <windows.h> #include <string>  int main() {    std::wstring comnum;    std::wcout << l"\n\nenter 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; }      
Comments
Post a Comment