سؤال

I've been working on a simple windows program using Visual C++ 2010 express on a 64bit Windows 7 machine. So far I have simple menu with and an editable text area. I'm trying to allow the user select a media file (movie or music file) and play it using the default program.

When the user selects from the menu File->Play->File from Computer it runs the following code.

    case ID_PLAY_FFC:
    {
        system("cd c:/windows/system32/&&cmd.exe");
        FileOpen(hWnd);
        system("cd c:/windows/system32/&&cmd.exe");
    }
    break;

The problem is that the first system call runs as expected. The second call tells me that "cmd.exe is not recognized as an internal or external command, operable program, or batch file". I've tried placing the second system call within the File Open function and it seems to work anywhere before GetOpenFileName but not after.

The only thing I really need to get the is file path so I was wondering if any one knew how to fix this problem or a better way to accomplish this?

code for FileOpen():

    void FileOpen(HWND hwnd)
    {
        OPENFILENAME ofn;           // common dialog box structure
        char szFile[MAX_PATH];      // buffer for file name MAX_PATH = 260
        HANDLE hf;                  // file handle

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

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

        // Display the Open dialog box.
        //system("cd c:/windows/system32/&&cmd.exe"); will execute here.

        if (GetOpenFileName(&ofn)==TRUE)
        {
            //system("cd c:/windows/system32/&&cmd.exe"); but not here.
            hf = CreateFile(ofn.lpstrFile,
                    GENERIC_READ,
                    0,
                    (LPSECURITY_ATTRIBUTES) NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    (HANDLE) NULL);
            if (hf == (HANDLE)-1)
            {
                MessageBox(NULL,"Could not open this file", "File I/O Error", MB_ICONSTOP);
                return;
            }

        }
    }
هل كانت مفيدة؟

المحلول

The GetOpenFileName() function changes the working directory and drive as part of it's operation. Your call to cd does not change the working drive and cmd.exe is still not in the working directory.

The solution depends on what exactly you're trying to do in the end, but you can specify the full path to cmd.exe (See the %COMSPEC% environment variable) and not rely on a command interpreter, or pass the OFN_NOCHANGEDIR flag to tell it not to clobber the working directory.

Note that there isn't any real reason for a (GUI) app to require a specific working path. You should fully qualify everything you can.

نصائح أخرى

Calling system() starts a new process, so even if your cd commands were valid (which they are not), it would not matter because you would be changing the working directory of another process, not your app's process. To set the working directory of your app's process, use SetCurrentDirectory() instead of system(), eg:

case ID_PLAY_FFC:     
{     
    SetCurrentDirectory(TEXT("c:/windows/system32/"));     
    FileOpen(hWnd);     
    SetCurrentDirectory(TEXT("c:/windows/system32/"));     
}     
break;

However, you don't need to do that manually because the OFN_NOCHANGEDIR flag of GetOpenFileName() automatically handles that internally for you. Whatever the calling process's working directory is, GetOpenFileName() will preserve it when OFN_NOCHANGEDIR is specified.

Try this:

case ID_PLAY_FFC: 
{
    FileOpen(hWnd); 
}
break; 


void FileOpen(HWND hwnd)
{
    OPENFILENAME ofn;           // common dialog box structure
    TCHAR szFile[MAX_PATH+1];   // buffer for file name MAX_PATH = 260

    // Zero out szFile so that GetOpenFileName does
    // not use the contents to initialize itself.
    ZeroMemory(szFile, sizeof(szFile));

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = TEXT("All\0*.*\0Text\0*.TXT\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;

    // Display the Open dialog box.
    if (GetOpenFileName(&ofn)==TRUE)  
    {  
        int ret = (int) ShellExecute(
                          hwnd,
                          NULL,
                          ofn.lpstrFile,
                          NULL,
                          TEXT("c:/windows/system32/"),
                          SW_SHOWNORMAL);
        if (ret <= 32)
        {  
            MessageBox(NULL, TEXT("Could not open this file"), TEXT("File I/O Error"), MB_ICONSTOP);  
            return;  
        }  
    }  
}  
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top