tags:

views:

252

answers:

3

hi

in the below code i am getting the c:\windows\Microsoft.Net\Framework\v2.0.057 to the buffer.Now i wnat to store the above value in doble quotes " c:\windows\Microsoft.Net\Framework\v2.0.057" and i want to pass this for the process. how to make this path in double quotes....

  HINSTANCE hDLL = LoadLibrary(TEXT("mscoree.dll"));
  FNPTR_GET_COR_SYS_DIR   GetCORSystemDirectory = NULL;
GetCORSystemDirectory = (FNPTR_GET_COR_SYS_DIR) GetProcAddress   (hDLL,"GetCORSystemDirectory"); 
if(GetCORSystemDirectory!=NULL)
    {   
    WCHAR buffer[MAX_PATH + 1];
    DWORD length; 
       HRESULT hr = GetCORSystemDirectory(buffer,MAX_PATH,&length);
    std::string tbuf="\"buffer\"";
    // std::string tbuf=" \""+(string)buffer+"\\InMageSQL.dll\" /codebase /tlb /silent";

    if(S_OK==hr)
    {
    wcscat( buffer,L"RegAsm.exe" );
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) ); //(LPSTR)getcurrentpath.c_str()
    if(!CreateProcess((LPCTSTR)buffer,(LPTSTR)strInMageSqlDll.c_str(),NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) )
    {
      cout<<"CreateProcess failed "<<GetLastError()<<endl;
    }
A: 

Try the following

std::wstring tbuf="\"";
tbuf+= buffer;
tbuf+= "\"";
JaredPar
You can't append wchar_t strings to basic_string<char>
Steve M
@Steve, good catch. Switched to wstring
JaredPar
@JaredPar: But you can't assign a `const char[]` to a `std::wstring` either.
sbi
A: 

JaredPar's method works, here is one alternative:

std::wostringstream oss
oss << '"' << buffer << '"';
std::wstring strBuff(oss.str());

edit:

switched to the w* versions.

luke
This doesn't work, either. http://codepad.org/xdSH1Taj
Steve M
Made the same mistake. Using wostringstream and wstring should work just fine.
luke
@luke: It has, however, the disadvantage of constructing and destructing a string stream for an operation that could easily (and likely cheaper) be done using solely strings.
sbi
It's just one alternative. The situation doesn't seem to be a grave performance concern.
luke
+1  A: 

I'm going out on a limb here, since I haven't used much of the Windows API in recent years and I never used WCHAR.

For one, you could widen the buffer a bit and poke the quotes right into it:

WCHAR buffer[MAX_PATH + 1 + 2];
buffer[0] = L'"';
buffer[sizeof(buffer)/sizeof(WCHAR)-1] = L'"';
DWORD length; 
HRESULT hr = GetCORSystemDirectory(buffer,MAX_PATH+1,&length);

However, this seems inelegant at best. (And I'd need to think twice whether I got that sizeof(buffer)/sizeof(WCHAR)-1 right -- which I'm too lazy for.)

Another way would be to use the std::basic_string<> template. Note that std::string is a typedef for std::basic_string<char> . There's little preventing you from instantiating it for other character types, too:

WCHAR buffer[MAX_PATH + 1];
DWORD length; 
HRESULT hr = GetCORSystemDirectory(buffer,MAX_PATH,&length);
std::basic_string<WCHAR> tbuf= L"\"" + buffer + L"\"";
// use 'tbuf.c_str()' to read 'tbuf' as a C-string (there's no safe way to write to it)

Note that this presumes that WCHAR expands to wchar_t. If this isn't the case (I have never worked with it, so I wouldn't know), you'd have to cast the literals:

std::basic_string<WCHAR> tbuf = static_cast<WCHAR>(L'"')
                              + buffer 
                              + static_cast<WCHAR>(L'"');
sbi