title: char array to LPCTSTR conversion in c
body: <p>Does anyone know how to convert a char array to a LPCTSTR in c?</p>
  
  <p>Edit:</p>
  
  <p>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().</p>
  
  <p>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.</p>
  
  <pre><code>CreateFile(_T("\\\\.\\COM11")... //hardcoded for com port 11
  </code></pre>
  
  <p>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.</p>
  
  <p>All of these examples assume that portNum is an int that is already assigned a valid value</p>
  
  <h1>1</h1>
  
  <pre><code>char portName[12] = { 0 };
  
  sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );
  
  CreateFile(portName...
  </code></pre>
  
  <p>I've also tried #1 with a LPCSTR case for what it's worth...</p>
  
  <h1>2</h1>
  
  <pre><code>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...
  </code></pre>
  
  <h1>3</h1>
  
  <pre><code>const char * portName = "";
  sprintf_s( portName, sizeof( portName ), "\\\\.\\COM%i", portNum );
  
  LPCSTR lpPortName = portName;
  
  CreateFile(lpPortName...
  </code></pre>