views:

237

answers:

3

About the following code, it is written by MFC, but in test time, i run the simple program, i will not got a normal case, the program will return a failed message to me, the message is "Can't play file:[PATH]".

but if me fixed the File (not select form the the dialog), it is work, i can not find out the problem, i hope some people can help me to fix..

please excuse my poor English. thanks a lot..

About the code:

    // Check Bass Version
if (HIWORD(BASS_GetVersion()) != BASSVERSION) {
 this->MessageBox(_T("An incorrect version of BASS.DLL was loaded"), _T("Error!"), MB_ICONSTOP | MB_OK);
 return;
}

// Start Bass
if (!BASS_Init(-1, 44100, 0, NULL, NULL)) {
 this->MessageBox(_T("Can't initialize device"), _T("Error!"), MB_ICONSTOP | MB_OK);
 return;
}

CString filePath = _T("");
CFileDialog dialog(true, _T("mp3"), _T("*.mp3"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER, _T("*.*|All File|*.mp3|MP3 File||"), NULL);
if (dialog.DoModal() == IDOK) {
 filePath = dialog.GetPathName();
}

if (filePath == "") {
 this->MessageBox(_T("Please select file first"), _T("Error!"), MB_ICONSTOP | MB_OK);
 return;
}

//char * filePathChar = (char *) (LPCTSTR) filePath;

DWORD channel;
if (!(channel = BASS_StreamCreateFile(false, filePath, 0, 0, 0))
 && !(channel = BASS_MusicLoad(false, filePath, 0, 0, BASS_MUSIC_RAMPS | BASS_MUSIC_POSRESET | BASS_MUSIC_PRESCAN, 0))) {
  this->MessageBox(_T("Can't play file:\n" + filePath), _T("Error!"), MB_ICONSTOP | MB_OK);
  BASS_Free();
  return;
}

BASS_ChannelPlay(channel,false);

About the BASS_StreamCreateFile (bass.dll):

HSTREAM BASS_StreamCreateFile(
    BOOL mem,
    void *file,
    QWORD offset,
    QWORD length,
    DWORD flags
);

About the BASS_MusicLoad (bass.dll):

HMUSIC BASS_MusicLoad(
    BOOL mem,
    void *file,
    QWORD offset,
    DWORD length,
    DWORD flags,
    DWORD freq
);
A: 
HMUSIC BASS_MusicLoad(
    BOOL mem,
    void *file, // <--------
    QWORD offset,
    DWORD length,
    DWORD flags,
    DWORD freq
);

The file parameter is a pointer to void.

When you pass filePath which is CString, you don't actually pass a string because the compiler don't know that it has to convert it to char*.

If file was declared as const char* you wouldn't have that problem.
Try passing filePath.GetBuffer(1).

Nick D
filePath.GetString() would be a better choice, IMO.
Goz
@Goz, it depends on what MFC version the user has. I didn't know that method.
Nick D
A: 

CFileDialog has known Problems under VC6 with the structures it needs. This was fixed in VC 7.x.

Kai
A: 
// ...

if (filePath.IsEmpty()) {
    this->MessageBox(_T("Please select file first"), _T("Error!"), MB_ICONSTOP | MB_OK);
    return;
}

LPTSTR filename = filePath.GetBuffer(0);

//char * filePathChar = (char *) (LPCTSTR) filePath;

DWORD channel;
if (!(channel = BASS_StreamCreateFile(false, filename, 0, 0, 0))
        && !(channel = BASS_MusicLoad(false, filename, 0, 0, BASS_MUSIC_RAMPS | BASS_MUSIC_POSRESET | BASS_MUSIC_PRESCAN, 0))) {
                this->MessageBox(_T("Can't play file:\n" + filePath), _T("Error!"), MB_ICONSTOP | MB_OK);
                BASS_Free();
                return;
}

filePath.ReleaseBuffer();

// ...
boxoft