我一直在使用64位Windows 7机器上使用Visual C++ 2010 Express的简单Windows程序。到目前为止,我有简单的菜单和可编辑的文本区域。我试图让用户选择一个媒体文件(电影或音乐文件)并使用默认程序播放它。GetOpenFileName和系统函数调用运行时错误C++ win32 api
当用户从菜单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;
}
}
}
什么所有的系统调用呢?我不明白这一点的必要性。尝试在标志中包含“OFN_NOCHANGEDIR”。如果你想在默认播放器中播放一个文件,使用'ShellExecute'。 – 2011-12-19 00:30:33