|
-
November 22nd, 2003, 04:11 PM
#1
CString to string problem
Hi,
I m using a dll call to play a wav file .
It is like this Playfile("c:\temp\chimes.wav",2);
now i have used GetPathName property to get the path name of the selected file into the CString m_iResults.
Now if i use the syntax as Playfile(m_iResults,2); it gives me error saying - cannot convert parameter 1 from 'class CString' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
please provide me the solution or a way to solve this problem.
rutu.
-
November 22nd, 2003, 04:56 PM
#2
See if Playfile(m_iResults.GetString(),2) works for you.
-
November 22nd, 2003, 05:12 PM
#3
Playfile((LPCTSTR)m_iResults,2);
-
November 22nd, 2003, 09:49 PM
#4
Re: CString to string problem
Originally posted by patil_ruturaj
Hi,
I m using a dll call to play a wav file .
It is like this Playfile("c:\temp\chimes.wav",2);
now i have used GetPathName property to get the path name of the selected file into the CString m_iResults.
Now if i use the syntax as Playfile(m_iResults,2); it gives me error saying - cannot convert parameter 1 from 'class CString' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
please provide me the solution or a way to solve this problem.
rutu.
You cannot "convert" a CString to a char*. A const char*, yes, but a char* - no. You can copy from a CString to a char buffer. You can also use GetBuffer/ReleaseBuffer, but I personally avoid these.
See if Playfile(m_iResults.GetString(),2) works for you.
This will not work. GetString returns a const PCXSTR. Plus, the method is not available in MFC42 / MSVC 6.0.
Playfile((LPCTSTR)m_iResults,2);
This will not work. The Playfile function requires a char*. Explicitly casting it to a const pointer will not cause it to behave any differently since CString has an operator LPCTSTR that allows for implicit casts.
Here's what I would do:
Code:
/* Contains filename to pass to Playfile, presumably obtained by calling CFileDialog::GetPathName */
CString m_iResults;
char szFileToPlay[MAX_PATH + 1] = {0};
strncpy( szFileToPlay, m_iResults, MAX_PATH );
Playfile( szFileToPlay, 2 );
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
November 22nd, 2003, 10:25 PM
#5
I use this all the time.
CString cs;
(LPCTSTR)cs.GetBuffer(cs.GetLength());
cs.ReleaseBuffer();
Is there any issue with this code so you avoid it?
csheng
-
November 22nd, 2003, 10:44 PM
#6
Funny, this compiles just fine in my project:
CString sound("C:\\sound.wav");
::PlaySound(sound.GetString(),0,SND_ASYNC);
*shrugs*
-
November 22nd, 2003, 11:42 PM
#7
Originally posted by tmecham
Funny, this compiles just fine in my project:
CString sound("C:\\sound.wav");
::PlaySound(sound.GetString(),0,SND_ASYNC);
*shrugs*
Ummm..
1) You're apparently using VC++ 7.x/MFC7x. CString::GetString does not exist in VC++ 6.0/MFC4.2. Plus, you don't even need to call GetString for PlaySound. See #2.
2) You are calling PlaySound, not Playfile.
PlaySound, from MSDN:
Code:
BOOL PlaySound(
LPCSTR pszSound,
HMODULE hmod,
DWORD fdwSound
);
Note that the first param is a LPCSTR (const char*), not a char*. Playfile is obviously a different function, especially since the compiler says the first param should be a char*.
Last edited by vicodin451; November 22nd, 2003 at 11:47 PM.
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
November 22nd, 2003, 11:46 PM
#8
Originally posted by csheng
I use this all the time.
CString cs;
(LPCTSTR)cs.GetBuffer(cs.GetLength());
cs.ReleaseBuffer();
Is there any issue with this code so you avoid it?
csheng
Why would you do such a thing? You are using a function to retrieve the (non-const) pointer to the internal char buffer, and then casting it to a const pointer. CString has an implicit LPCTSTR conversion operator - "use" that instead. It's more intuitive and makes the code more readable.
I avoid GetBuffer/ReleaseBuffer because I don't like the idea of messing with the internal buffer that's managed by the CString object. Seems like a hack. I know how to use the functions and what they are provided for, but choose not to use them. I've seen use of GetBuffer abused far too many times...
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
November 23rd, 2003, 01:18 AM
#9
Try this
CEdit * pedit = (CEdit*)GetDlgItem(IDC_EDIT_SOUND);
pedit->GetWindowText(m_sWaveFileToPlay);
sndPlaySound(NULL, SND_SYNC);
sndPlaySound(m_sWaveFileToPlay, SND_SYNC);
Best Wishes,
Maggie
-
November 23rd, 2003, 09:16 AM
#10
Hi,
Thanks to all !
Since all of you had intension to help me i tried every suggestion given by you.But the solution given by vicodin451
--------------------------------------------------------------------------------
char szFileToPlay[MAX_PATH + 1] = {0};
strncpy( szFileToPlay, m_iResults, MAX_PATH );
Playfile( szFileToPlay, 2 );
--------------------------------------------------------------------------------
worked fine and was easy.Yes i use VC6.
Thanks again.Hope it continues.
Rutu.
-
November 23rd, 2003, 10:05 AM
#11
Just another thought: Where does this Playfile() function come from? Did you provide it? If yes, then note that you should rather change the first parameter to a const char* (or LPCTSTR, for that matter) instead of a char*. After all, Playfile() doesn't seem to modify the string you pass, so why should you make that parameter non-const?
-
November 23rd, 2003, 05:40 PM
#12
Good point.
If wanted to get char * from cstring and gettin getting the internal buffer from cstring is not such a bad thing, at least not for me,
casting it to const is also logical as PlayFile won't change it anyways.
csheng
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|