2012-02-13 20 views
0

我是一名新成员,并在加入本网站后加入了这个网站,当时我遇到了一些编程问题。我想为我的大学项目编写一个媒体播放器(Win32 SDK VC++ 6.0),并且我被卡住了。我搜索了各种论坛和msdn,最后登陆了函数GetShortPathName,它使我能够通过名称中有空格的文件夹和文件进行播放。我会在这里粘贴代码,这样我就可以更清楚地知道我正在做什么。通过打开的对话框接受它们后播放文件

case IDM_FILE_OPEN : 
    ZeroMemory(&ofn, sizeof(ofn)); 
    ofn.lStructSize = sizeof(ofn); 
    ofn.hwndOwner = hwnd; 
    ofn.lpstrFilter = "Media Files (All Supported Types)\0*.avi;*.mpg;*.mpeg;*.asf;*.wmv;*.mp2;*.mp3\0" 
             "Movie File (*.avi;*.mpg;*.mpeg)\0*.avi;*.mpg;*.mpeg\0" 
             "Windows Media File (*.asf;*.wmv)\0*.asf;*.wmv\0" 
             "Audio File (*.mp2;*.mp3)\0*.mp2;*.mp3\0" 
             "All Files(*.*)\0*.*\0"; 
        ofn.lpstrFile = szFileName; 
        ofn.nMaxFile = MAX_PATH; 
        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_CREATEPROMPT; 
        ofn.lpstrDefExt = "mp3"; 

        if(GetOpenFileName(&ofn)) 
        { 

         length = GetShortPathName(szFileName, NULL, 0); 
         buffer = (TCHAR *) malloc (sizeof(length)); 
         length = GetShortPathName(szFileName, buffer, length); 

         for(i = 0 ; i < MAX_PATH ; i++) 
         { 
          if(buffer[i] == '\\') 
           buffer[i] = '/'; 
         } 

         SendMessage(hList,LB_ADDSTRING,0,(LPARAM)buffer); 
         mciSendString("open buffer alias myFile", NULL, 0, NULL); 
         mciSendString("play buffer", NULL, 0, NULL); 
        } 

        return 0; 

使用GetShortPathName功能我得到的路径为:d:/Mp3z/DEEPBL~1/03SLEE~1.mp3 直接播放按钮的情况下把这个路径

mciSendString("open D:/Mp3jh/DEEPBL~1/03SLEE~1.mp3 alias myFile", NULL, 0, NULL); 
mciSendString("play myFile", NULL, 0, NULL); 

文件打开并打得很好。但只要我尝试通过打开的文件对话框打开并播放,就没有任何反应。任何输入赞赏。

+0

为什么你认为你需要一个简短的路径名? – 2012-02-13 09:17:40

+1

请勿使用短名称。只需通过在文件名中添加引号来避免名称中的空格:''open \“path/to/file/with space in.mp3 \”alias myFile“' – 2012-02-13 09:49:51

回答

0

它看起来像问题是,你传递的名称buffer变量的mciSendString功能为,而不是传递缓冲区的内容。

你需要连接你想传递(openalias myFile)与内容buffer的论点。

代码也可以通过用自动数组替换malloc来简化。你不需要malloc它,因为你不需要它在块范围之外。 (你不应该使用在C++代码malloc无妨;使用new[]代替。)

这里是你的问题所示的代码的修改的摘录:
(警告:改变只用我的眼睛,编译器制造!小心处理。)

if(GetOpenFileName(&ofn)) 
{ 
    // Get the short path name, and place it in the buffer array. 
    // We know that a short path won't be any longer than MAX_PATH, so we can 
    // simply allocate a statically-sized array without futzing with new[]. 
    // 
    // Note: In production code, you should probably check the return value 
    // of the GetShortPathName function to make sure it succeeded. 
    TCHAR buffer[MAX_PATH]; 
    GetShortPathName(szFileName, buffer, MAX_PATH); 

    // Add the short path name to your ListBox control. 
    // 
    // Note: In C++ code, you should probably use C++-style casts like 
    // reinterpret_cast, rather than C-style casts! 
    SendMessage(hList, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(buffer)); 

    // Build the argument string to pass to the mciSendString function. 
    // 
    // Note: In production code, you probably want to use the more secure 
    // alternatives to the string concatenation functions. 
    // See the documentation for more details. 
    // And, as before, you should probably check return values for error codes. 
    TCHAR arguments[MAX_PATH * 2]; // this will definitely be large enough 
    lstrcat(arguments, TEXT("open")); 
    lstrcat(arguments, buffer); 
    lstrcat(arguments, TEXT("alias myFile")); 
    // Or, better yet, use a string formatting function, like StringCbPrintf: 
    // StringCbPrintf(arguments, MAX_PATH * 2, TEXT("open %s alias myFile"), 
    //    buffer); 

    // Call the mciSendString function with the argument string we just built. 
    mciSendString(arguments, NULL, 0, NULL); 
    mciSendString("play myFile", NULL, 0, NULL); 
} 

请注意,由于上面的代码显示,与C风格字符串(字符数组)的工作是在屁股真正的痛苦。 C++提供了更好的替代方案,其形式为std::string类。您应该强烈考虑使用它。要调用Windows API函数,您仍然需要一个C风格的字符串,但可以使用std::string类的c_str方法获得其中的一个。

+0

谢谢各位。我用strcpy和strcat,它工作。感谢科迪格雷的代码。这与我所做的相似。 – Salil 2012-02-13 11:27:39