tags:

views:

119

answers:

5

Hi

I am using

TCHAR buffer[MAX_SIZE];

after some set of steps i am gettig the relative path of folder say for ex:

c:\Microsoft.NET\Framework\v1.0.037\

Since the above path is in buffer of type TCHAR and i am trying to concatenate"RegAsm.exe"

After Appending i need to convert the path to the LPCTSTR since i need to pass it to CreateProcess() which takes LPCTSTR type as argument

then the compiler giving error.I have tried but vexed.

can any one help me in this aspect....

A: 

If you want to make your life easier I would always suggest using std::string, then when calling the CreateProcess() function just do:

std::string myPath = "somePath";
LPCTSTR lpBuf = reinterpret_cast<LPCTSTR>(myPath.c_str());

Not tested but that should probably work, if you are still getting an error, well posting the code would be beneficial.

Note that it may be more complicated if you are using Unicode.

DeusAduro
YUP its working.Hurrey ........but any thing harm using reinterpret_cast...
Cute
Never use this reinterpret cast.LPCTSTR lpBuf = myPath.c_str(); will also work if the system is configured right.
Totonga
This should never be done. If string::c_str() is ANSI doing this reinterpret_cast will surely lead to passing garbage into later called CreateProcess. It's not a fix to the problem, just a way to ask the compiler to not show the error message.
sharptooth
A: 

I assume from the fact that you get an error that you're working with Unicode... In which case you may want to look at the mbstowcs and wcstombs functions.

More information is really needed to be able to answer properly.

Matthew Iselin
+2  A: 
_tcscat_s

is the related method for TCHAR. It depends like TCHAR on the _UNICODE & _MBCS preprocessor swithch and will be resolved to strcat_s or wcscat_s.

TCHAR buffer[MAX_SIZE] = _T("c:\Microsoft.NET\Framework\v1.0.037\");
_tcscat_s(buffer, MAX_SIZE, _T("RegAsm.exe"));

but this is very good old C style. So while you are using TCHAR I would suggest that you also use MFC stuff. So using CString which also is affected by _UNICODE & _MBCS would also solve your issue.

CString buffer;
buffer = _T("c:\Microsoft.NET\Framework\v1.0.037\");
buffer += _T("RegAsm.exe");
CreateProcess(buffer, ..

std::string or std::wstring will not help because they do not change their behavior related to the preprocessor switched but if you use CreateProcessA or CreateProcessW you can also use std::string or std::wstring.

Totonga
+1 for suggesting to use CString
Naveen
A: 

When you do buffer + "RegAsm.exe", it will be like adding two pointers (as arrays decay into pointers and "RegAsm.exe" will be const TCHAR*) hence you get the compiler error. What you want is string concatanation. Use _tcscat or or the secure version (as claimed in MSDN) _tcscat_s function for doing the concatanation.

Naveen
+1  A: 

The problem is TCHAR and CreateProcess are macros that expand differently depending on whether you compile for Unicode or not. The caveat is that GetCORSystemDirectory() will only accept a Unicode buffer. To get rid of these ANSI/Unicode problems write this code part explicitly for Unicode.

Instead of TCHAR use WCHAR for the buffer. Instead of CreateProcess() use CreateProcessW() - it will happily accept the Unicode buffer. Use wcscat() for strings concatenation.

Something like this (error handling omitted):

 WCHAR buffer[MAX_PATH + 1];
 DWORD realLength;
 GetCORSystemDirectory( buffer, MAX_PATH, &realLength );
 *( buffer + realLength ) = 0;// Don't forget to null-terminate the string
 wcscat( buffer, L"regasm.exe" );
 CreateProcessW( /*pass buffer here*/ );
sharptooth
Cute
You should instead declare the variable of STARTUPINFOW type - that would be more correct.
sharptooth