Pregunta

Hello there I'm working on a windows application using the Windows API with MS VC++ 2010 and I have the following code for selecting folders:

BOOL BrowseFolder(TCHAR *result)
{
    BROWSEINFO brwinfo = { 0 };
    brwinfo.lpszTitle = _T("Select Your Source Directory");
    brwinfo.hwndOwner = hWnd;
    LPITEMIDLIST pitemidl = SHBrowseForFolder (&brwinfo);

    if (pitemidl == 0) return FALSE; 

    // get the full path of the folder
    TCHAR path[MAX_PATH];
    if (SHGetPathFromIDList (pitemidl, path)) result = path;

    IMalloc *pMalloc = 0;
    if (SUCCEEDED(SHGetMalloc(&pMalloc)))
    {
        pMalloc->Free(pitemidl);
        pMalloc->Release();
    }

    ::MessageBox(hWnd, result, "input", MB_OK);
    ::MessageBox(hWnd, inputFolder, "input", MB_OK); // Reference Test
    return TRUE;
}

So, it opens up a browse folder dialog, saves the selected folder string in the reference parameter "result" and returns true if everything is ok.

Later I call:

BrowseFolder(inputFolder);

And when i try to print out the content of "inputFolder", it shows blank (inputFolder is a global variable TCHAR* inputFolder)

As you can see in the BrowseFolder definition I send two Message Boxes one for "result" and the other for "inputFolder" (this last one shows blank)

So my question is.. If I call: BrowseFolder(inputFolder); Shouldn't "inputFolder" be modified by reference? Why does it show empty?

Thanks in advance.

¿Fue útil?

Solución

if (SHGetPathFromIDList (pitemidl, path)) result = path;

This line is your problem. result isn't a class like std::string, it's simply a pointer to a buffer of one or more TCHAR. Assigning it like that simply changes the pointer to point to path, it doesn't copy path into the buffer that result points to.

If you don't want to change to use a string class then you need to call a function to copy the string into the supplied buffer. For example:

StringCchCopy(result, MAX_PATH, path);

(this assumes the buffer is MAX_PATH characters in size).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top