Alex Rivera | Logout

char array to LPCTSTR conversion in c

Asked 2009-06-10T16:03:43.730
13

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...
Edit
Report

1 Answer

2

try like this.........

TCHAR *pcCommPort = TEXT("COM1");
HANDLE h = CreateFile(pcCommPort,other arguments);
answered 2011-09-29T19:26:57.370

Your Answer