Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:19:31+00:00 2026-05-16T22:19:31+00:00

I seem to be having a bit of a TEXT / UNICODE problem when

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:19:31+00:00Added an answer on May 16, 2026 at 10:19 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with my Seam code and I can't seem to figure
I am having a bit of trouble. It does not seem to be a
I'm having a bit of trouble with John Resig's Micro templating. Can anyone help
As you can tell from the title, Im having a bit of issue regarding
Seem to be having an issue with std::auto_ptr and assignment, such that the object
i'm having an issue with creating a query in oracle which doesnt seem to
I having a bit of a quandry trying to come up with a good
I'm having a bit of trouble getting a Python regex to work when matching
I'm currently working on some infrastructure costing, and I'm having a bit of trouble
I would really appreciate to get your help on this, as I can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.