user3161006
user3161006

Reputation: 41

how to convert char array to LPCTSTR

I have function like this which takes variable number of argument and constructs the string and passes it to another function to print the log .

logverbose( const char * format, ... )
{
char buffer[1024];
va_list args;
va_start (args, format);
vsprintf (buffer,format, args);
va_end (args);    

LOGWriteEntry( "HERE I NEED TO PASS buffer AS LPCTSTR SO HOW TO CONVERT buffer to LPCTSTR??");

}

Instead of using buffer[1024] is there any other way? since log can be bigger or very smaller . All this am writing in C++ code please let me know if there is better way to do this .....

Upvotes: 2

Views: 2292

Answers (2)

Kaz
Kaz

Reputation: 58617

A good way to proceed might be from among these alternatives:

  • Design the logverbose function to use TCHAR rather than char; or
  • Find out if the logging API provides a char version of LOGWriteEntry, and use that alternative.
  • If no char version of LOGWriteEntry exists, extend that API by writing one. Perhaps it can be written as a cut-and-paste clone of LOGWriteEntry, with all TCHAR use replaced by char, and lower-level functions replaced by their ASCII equivalents. For example, if LOGWriteEntry happens to call the Windows API function ReportEvent, your LOGWriteEntryA version could call ReportEventA.
  • Really, in modern applications, you should just forget about char and just use wchar_t everywhere (compatible with Microsoft's WCHAR). Under Unicode builds, TCHAR becomes WCHAR. Even if you don't provide a translated version of your program (all UI elements and help text is English), a program which uses wide characters can at least input, process and output international text, so it is "halfway there".

Upvotes: 1

wallyk
wallyk

Reputation: 57784

You can probably just pass it:

LOGWriteEntry (buffer);

If you are using ancient memory models with windows, you might have to explicitly cast it:

LOGWriteEntry ((LPCTSTR) buffer);

correction:

LPCTSTr is Long Pointer to a Const TCHAR STRing. (I overlooked the TCHAR) with the first answer.

You'll have to use the MultiByteToWideChar function to copy buffer to another buffer and pass that to the function:

w_char buf2 [1024];
MultiByteToWideChar (CP_ACP, 0, buffer, -1, buf2, sizeof buf2);
LOGWriteEntry (buf2);

Upvotes: 1

Related Questions