Raphael Rosa
Raphael Rosa

Reputation: 45

Handle multiple file selection

I have the following code:

void OpenJpgFile(HWND hWnd)
{
OPENFILENAME ofn;
wchar_t szFileName[17*MAX_PATH] = L"";

ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrTitle = L"Selecione as fotos que deseja adicionar...";
ofn.lpstrFilter = L"Arquivos JPEG (*.jpg)\0*.jpg\0Todos os Arquivos (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT;
ofn.lpstrDefExt = L"jpg";
if(GetOpenFileName(&ofn))
{
            //Test
    MessageBox(hWnd,ofn.lpstrFileTitle,L"TESTE",NULL);


            if(gpBitmap)
    {
        delete gpBitmap;
    }
    gpBitmap = new Bitmap(szFileName);

        InvalidateRect(hWnd, NULL, TRUE);
        UpdateWindow(hWnd);
    }
}

What I wanted to know is how can I get the name of all files the user has selected...

All I can get is the path to the folder... is there an array that keeps the file names?

Upvotes: 0

Views: 398

Answers (1)

Michael Urman
Michael Urman

Reputation: 15905

Per the documentation of OFN_ALLOWMULTISELECT when OFN_EXPLORER is also specified, lpstrFile will contain the directory followed by the names of the files, separated by null characters, and terminated with two adjacent null characters. When you call new Bitmap(szFileName) you are treating szFileName incorrectly - that is you pass it to a constructor that expects a standard single-null terminated string. Instead you have to process it more carefully to read past the initial null, and probably recreate your full file paths.

This string format happens to be the same format as used for REG_MULTI_SZ so, aside from not having to worry about missing final terminators, advice related to handling values of type REG_MULTI_SZ can help you here.

Note that to be fully general, you may also have to handle cases that require a larger buffer. See GetOpenFileName() with OFN_ALLOWMULTISELECT flag set for another angle on that

Upvotes: 2

Related Questions