How to derive const void pointer from CString?
Hi, I'd appreciate some help here.
I'm experimenting with the Bass24 audio library. One of its fundamental commands is BASS_StreamCreateFile, which takes several arguments including a file name to open. Now, I used MFC's CFileDialog to fetch the filename, which is a CString object. StreamCreateFile, however, needs the filename presented as a const void pointer, something I'm completely unfamiliar with. I'm pretty new to this and I can't work out how to derive a const void pointer from a CString. I probably need to use CString.GetBuffer() somehow. Can anybody help?
I'm using VC++2010 and Unicode.
Re: How to derive const void pointer from CString?
It's been a long time since I used MFC but if I don't remember wrong you can pass the CString as it is. The conversion method is called automatically.
Re: How to derive const void pointer from CString?
CString has an LPCTSTR (long pointer to constant 'T' string) operator which will return a pointer to the string data. All you need to do is cast that to a const void pointer.
For example:
CString fileName = _T("xxx.xxx");
const void *pVoid = (const void *)(LPCTSTR)fileName;
Now pVoid can be passed to the function.
In fact, most compilers should not even require the cast. As S M A suggested, you may even be able to use the CString directly as an argument to the function.
Note that the assumption is made that the string type (const char * or const wchar_t *) matches between the application and the dll. If not, you will need to fix that. If you are using the template version of CString, you can make the file name specifically either CStringA or CStringW as required.
Re: How to derive const void pointer from CString?
Thanks for the replies. In fact I solved the problem by rewriting the app without Unicode support; it was the double-width strings that the bass.dll commands didn't like. Sometimes you need to apply a bit of lateral thinking to problems like these.
Re: How to derive const void pointer from CString?
Bass does accept UNICODE fileNames, you have just to place the flag for it.
Code:
HSTREAM BASS_StreamCreateFile(
BOOL mem,
void *file,
QWORD offset,
QWORD length,
DWORD flags
);
flags Any combination of these flags.
.......
BASS_UNICODE file is in UTF-16 form. Otherwise it is ANSI on Windows and UTF-8 on OSX.