CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2003
    Location
    India
    Posts
    12

    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.

  2. #2
    Join Date
    Jul 2003
    Posts
    147
    See if Playfile(m_iResults.GetString(),2) works for you.

  3. #3
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776
    Playfile((LPCTSTR)m_iResults,2);

  4. #4
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515

    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.

  5. #5
    Join Date
    Nov 2003
    Posts
    13
    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

  6. #6
    Join Date
    Jul 2003
    Posts
    147
    Funny, this compiles just fine in my project:

    CString sound("C:\\sound.wav");
    ::PlaySound(sound.GetString(),0,SND_ASYNC);

    *shrugs*

  7. #7
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    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.

  8. #8
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    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.

  9. #9
    Join Date
    Jun 2001
    Location
    California USA
    Posts
    214

    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

  10. #10
    Join Date
    Nov 2003
    Location
    India
    Posts
    12

    Thumbs up

    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.

  11. #11
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    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?

  12. #12
    Join Date
    Nov 2003
    Posts
    13
    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
  •  





Click Here to Expand Forum to Full Width

Featured