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 253079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:43:54+00:00 2026-05-11T21:43:54+00:00

Does anyone know how to convert a char array to a LPCTSTR in c?

  • 0

Does anyone know how to convert a char array to a LPCTSTR in c?

Edit:

For more reference, I need to add an integer to a string then convert that string to LPCTSTR for the first parameter of the windows function CreateFile().

This is the hardcoded example which I am currently using, but I need to be able to pass in any number to use as a port number.

CreateFile(_T("\\\\.\\COM11")... //hardcoded for com port 11

and here are several things I have tried, which I believe include the following suggestions for the next 2 answers of this post. They don’t work unfortunately. If anyone could point out something I’ve done wrong and could possibly solve my problem, I’d appreciate it.

All of these examples assume that portNum is an int that is already assigned a valid value

1

char portName[12] = { 0 };

sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );

CreateFile(portName...

I’ve also tried #1 with a LPCSTR case for what it’s worth…

2

LPCSTR SomeFunction(LPCSTR aString) {
    return aString;
}

main() {

char portName[12] = { 0 };
sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );

LPCSTR lpPortName = SomeFunction(portName);

CreateFile(lpPortName...

3

const char * portName = "";
sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );

LPCSTR lpPortName = portName;

CreateFile(lpPortName...
  • 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-11T21:43:54+00:00Added an answer on May 11, 2026 at 9:43 pm

    You can implicitly convert a char array to an LPCSTR without any casts:

    void SomeFunction(LPCSTR aString);
    ...
    char myArray[] = "hello, world!";
    SomeFunction(myArray);
    

    An LPCSTR is a Windows typedef for a long pointer to a constant string. Back in the dark days of Win16 programming, there were different types of pointers: near pointers and far pointers, sometimes also known as short and long pointers respectively. Near pointers could only point to a 64KB segment of memory determined by one of the x86 segment registers. Far pointers could point to anything. Nowadays in Win32 with virtual memory, there is no need for near pointers — all pointers are long.

    So, an LPSTR is a typedef for a char *, or pointer to a string. An LPCSTR is the const version, i.e. it is a typedef for a const char *. In C, arrays decay into pointers to their first elements, so a char[] decays into a char*. Finally, any type of “pointer to T” (for any type T) can be implicitly converted into a “pointer to const T”. Thus, combining these three facts, we see that we can implicitly convert a char[] into an LPCSTR.


    In response to your edit, I’m going to guess that you’re compiling a Unicode application. If you look carefully at the documentation for CreateFile(), you’ll notice that the filename parameter is actually an LPCTSTR, not an LPCSTR (note the T).

    For pretty much every Win32 function that takes an argument of some string type (perhaps indirectly, i.e. as a member of a structure passed as a parameter), there are actually two versions of that function: one which takes 8-bit ANSI strings, and one which takes 16-bit wide-character strings. To get the actual function names, you append an A or a W to the function name. So, the ANSI version of CreateFile() is named CreateFileA(), and the wide-character version is named CreateFileW(). Depending on whether or not you’re compiling with Unicode enabled (i.e. whether the preprocessor symbol _UNICODE is defined), the symbol CreateFile is #defined to either CreateFileA or CreateFileW as appropriate, and likewise for every other function that has an ANSI and a wide-character version.

    Along the same lines, the type TCHAR is typedefed to either char or wchar_t, depending on whether Unicode is enabled, and LPCTSTR is typedefed to a pointer to a const TCHAR.

    Thus, to make your code correct, you should replace the strings you’re using with TCHAR strings, and use the type-generic version of sprintf_s, _stprintf_s:

    TCHAR portName[32];
    _stprintf_s(portName, sizeof(portName)/sizeof(TCHAR), _T("\\\\.\\COM%d"), portNum);
    CreateFile(portName, ...);
    

    Alternatively, you can explicitly use the ANSI or wide-character versions of everything:

    // Use ANSI
    char portName[32];
    sprintf_s(portName, sizeof(portName), "\\\\.\\COM%d", portNum);
    CreateFileA(portName, ...);
    
    // Use wide-characters
    wchar_t portName[32];
    swprintf_s(portName, sizeof(portName)/sizeof(wchar_t), L"\\\\.\\COM%d", portNum);
    CreateFileW(portName, ...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The first class method looks like it performs a MySQL… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Here's a sample showing how to attach an event using… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer The "Application Data" directory is contained within the users "documents… May 11, 2026 at 11:55 pm

Related Questions

I have decided to take up f# as my functional language. My problem: Give
I have an SSIS package I am developing. I am attempting to write data
Does anyone know how to convert a string from ISO-8859-1 to UTF-8 and back
Does anyone know how to convert a string that represents a color into a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.