简体   繁体   中英

How to do type conversion for the following Scenario?

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 "RegAsm.exe" “ 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....

_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.

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*/ );

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM