Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Colleagues,

I'm attempting to use a dialog box to select the filename of an AVI file that I want to use in OpenCV. The OpenCV function is called: cvCaptureFromAVI. To create the Open File dialog box, I am using the Windows function:
GetOpenFileName

I suspect that the trouble I am having has something to do with the fact that my whole project is written in Unicode, and the OpenCV function expects const char*. If I hard-code the AVI filename as a char* variable, and I don't open the dialog box, everything seems to work. However, as soon as I try to use the dialog box, the cvCaptureFromAVI function won't work. In other words, even if I don't use the filename stored from the dialog box, I still won't be able to use my AVI file. Just opening the dialog box itself seems to screw it up.

Here is what works:

MIDL
char filename[40] = "C:\\Users\\weylspinor\\Desktop\\test.avi";
CvCapture *G_movie_file = cvCaptureFromAVI(filename);


Now if I add the following, it doesnt work. (Note, I'm not even using the filename stored in the OPENFILENAME structure):

C++
OPENFILENAME ofn;       // common dialog box structure
char szFile[260] = "";       // buffer for file name
HANDLE hf;              // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner   = h_main;
ofn.lpstrFile   = LPWSTR(szFile);

// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile       = LPWSTR(szFile);
ofn.nMaxFile        = sizeof(szFile);
ofn.lpstrFilter     = TEXT("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.nFilterIndex    = 1;
ofn.lpstrFileTitle  = NULL;
ofn.nMaxFileTitle   = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags           = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;


// Display the Open dialog box.
if (GetOpenFileName(&ofn) == TRUE) {
    hf = CreateFile(ofn.lpstrFile,
        GENERIC_READ,
        0,
        (LPSECURITY_ATTRIBUTES) NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        (HANDLE) NULL);
}

char filename[40] = "C:\\Users\\weylspinor\\Desktop\\test.avi";
CvCapture *G_movie_file = cvCaptureFromAVI(filename);


I can get it to work again by just commenting out the GetOpenFileName function.

C++
OPENFILENAME ofn;       // common dialog box structure
char szFile[260] = "";       // buffer for file name
HANDLE hf;              // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner   = h_main;
ofn.lpstrFile   = LPWSTR(szFile);

// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile       = LPWSTR(szFile);
ofn.nMaxFile        = sizeof(szFile);
ofn.lpstrFilter     = TEXT("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.nFilterIndex    = 1;
ofn.lpstrFileTitle  = NULL;
ofn.nMaxFileTitle   = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags           = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;


// Display the Open dialog box.
/*if (GetOpenFileName(&ofn) == TRUE) {
    hf = CreateFile(ofn.lpstrFile,
        GENERIC_READ,
        0,
        (LPSECURITY_ATTRIBUTES) NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        (HANDLE) NULL);
}*/


MIDL
char filename[40] = "C:\\Users\\weylspinor\\Desktop\\test.avi";
CvCapture *G_movie_file = cvCaptureFromAVI(filename);


I'd appreciate it if anyone can point me to my error.

Cheers,
-J
Posted
Updated 29-Jan-11 16:16pm
v2
Comments
[no name] 30-Mar-12 13:42pm    
can anyone help me with windows explorer code in win32.
it shud contain search option and various other options which are handled in windows explorer..
please help me out

1 solution

You need to convert the returned filename from Unicode to Ansi via one of the wcstomb()[^] converters.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900