2

我一直在 64 位 Windows 7 机器上使用 Visual C++ 2010 express 开发一个简单的 Windows 程序。到目前为止,我有一个简单的菜单和一个可编辑的文本区域。我试图让用户选择一个媒体文件(电影或音乐文件)并使用默认程序播放它。

当用户从菜单 File->Play->File from Computer 中选择时,它会运行以下代码。

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

问题是第一个系统调用按预期运行。第二个调用告诉我“cmd.exe 未被识别为内部或外部命令、可运行程序或批处理文件”。我尝试在 File Open 函数中放置第二个系统调用,它似乎在 GetOpenFileName 之前的任何地方都可以工作,但之后却不行。

我唯一真正需要的是文件路径,所以我想知道是否有人知道如何解决这个问题或更好的方法来完成这个问题?

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;
            }

        }
    }
4

2 回答 2

4

GetOpenFileName()函数在其操作过程中更改工作目录和驱动器。您的调用cd不会更改工作驱动器,并且cmd.exe仍然不在工作目录中。

解决方案取决于你到底想要做什么,但你可以指定完整路径cmd.exe(参见%COMSPEC%环境变量)而不依赖于命令解释器,或者传递OFN_NOCHANGEDIR标志告诉它不要破坏工作目录。

请注意,(GUI)应用程序没有任何真正的理由需要特定的工作路径。你应该完全符合你所能做的一切。

于 2011-12-19T00:35:32.767 回答
0

调用system()会启动一个新进程,因此即使您的cd命令有效(它们不是有效的),也没关系,因为您将更改另一个进程的工作目录,而不是您的应用程序的进程。要设置应用程序进程的工作目录,请使用SetCurrentDirectory()代替system(),例如:

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

但是,您不需要手动执行此操作,因为OFN_NOCHANGEDIR标志 会在GetOpenFileName()内部为您自动处理。无论调用进程的工作目录是什么,GetOpenFileName()都会在指定时保留它OFN_NOCHANGEDIR

试试这个:

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;  
        }  
    }  
}  
于 2011-12-20T20:25:49.947 回答