想弹出对话框 可以多选,MP3文件 用下列代码,编译可以通过,但是点击按钮就出现停止工作(win7)
char szfile[256]="\0"; //文件
char szfilename[256]="\0"; //file name
char szfilter[]= "MP3(*.MP3)\0*.MP3\0";  //文件过滤
char sztitle[]="打开MP3文件"; //过滤器名称
OPENFILENAME lpofn;
lpofn.lStructSize=sizeof(OPENFILENAME);
lpofn.hwndOwner=hwnd;
// lpofn.hInstance=
lpofn.lpstrFilter=szfilter;
lpofn.lpstrCustomFilter=NULL;
//lpofn.nMaxCustFilter
lpofn.nFilterIndex=0;
lpofn.lpstrFile=szfile;
lpofn.nMaxFile=sizeof(szfile);
lpofn.lpstrFile=szfilename;
lpofn.nMaxFileTitle=sizeof(szfilename);
lpofn.lpstrInitialDir=NULL;
lpofn.lpstrTitle=sztitle;
lpofn.Flags=OFN_ALLOWMULTISELECT;
// lpofn.nFileOffset=0;
// lpofn.nFileExtension=0;
// lpofn.lpstrDefExt=0;
// lpofn.lCustData=0;
GetOpenFileName(&lpofn);

解决方案 »

  1.   

    先初始化结构,因为有些成员你没赋值,会出现野指针情况。
    memset(&lpofn, 0, sizeof(OPENFILENAME));另外,lpstrFile要足够大,因为多选时选中的所有文件的路径都在这块内存中(以\0或空格分隔),256太小了。还有,你给lpofn.lpstrFile赋值了两次,给lpstrFileTitle没赋值,但nMaxFileTitle不为0(崩溃应该就是因为这里)。
          lpofn.lpstrFile=szfile;
         lpofn.nMaxFile=sizeof(szfile);
         lpofn.lpstrFile=szfilename;
      

  2.   

    其实多选时没有必要给lpstrFileTitle申请内存,让它为NULL就好了。lpstrFileTitle为NULL时nMaxFileTitle被忽略。
      

  3.   

    MSDN上的例子程序:OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HWND hwnd;              // owner window
    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. if (GetOpenFileName(&ofn)==TRUE) 
        hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
            0, (LPSECURITY_ATTRIBUTES) NULL,
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
            (HANDLE) NULL);
      

  4.   

                lpofn.nMaxFile=sizeof(szfile);
                lpofn.nMaxFileTitle=sizeof(szfilename);
    sizeof用法有问题吧?
    lpofn.nMaxFile=256;
    lpofn.nMaxFileTitle=256;