我一直在 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;
}
}
}