CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1

    how do i convert const char* to a char*???


    void CInstallorUninstallDlg::RunAnother(CString FileName, CString Param)
    {// begin RunAnother
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si,sizeof(si));
    si.cb = sizeof(si);

    //LPCTSTR str = SetLPCSTR(Param);
    if(!CreateProcess(FileName,Param,NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
    {
    AfxMessageBox("CreateProcess failed");
    return;
    }

    }// end RunAnother

    void CInstallorUninstallDlg::InstallSoundFiles()
    {// begin InstallSoundFiles
    LPCTSTR par = "/r data\\*.drs modpack\\" + name +"\\sou?????.wav /s";
    RunAnother("drsbuild.exe",par);
    }// end InstallSoundFiles




    My problem is that the second Parameter of the CreateProcess function takes a char* and not a const char* and i want to use it in a function to run a command line program that the folder CString name changes every time the function is called, but do not know how to convert it to a char*. Any help is greatly apperciated. Thanks ahead of time.
    Anish Mistry


  2. #2
    Join Date
    Apr 1999
    Posts
    2

    Re: how do i convert const char* to a char*???

    Why not just copy the string into a standard character pointer string?


    char szTemp[100];

    strcpy(szTemp, (LPCTSTR)param);

    Then use szTemp in place of the string.


  3. #3
    Join Date
    May 1999
    Location
    New Jersey, USA
    Posts
    10

    Re: how do i convert const char* to a char*???

    Use const_cast operator on the pointer before passing it to the function.

    const_cast is used to remove the 'constness' of a pointer. Let me know if this works

    Regards
    Ram


  4. #4
    Join Date
    Apr 1999
    Location
    Malaysia
    Posts
    224

    Re: how do i convert const char* to a char*???

    cast if from const char* to char* example,

    CreateProcess(FileName,(char*)Param,NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))

    Hello World!!!

  5. #5
    Join Date
    Apr 1999
    Location
    Norway
    Posts
    19

    Re: how do i convert const char* to a char*???

    The Easiest way is just to call "char* CString::GetBuffer(int nMinSize)".
    You use like this:

    CreateProcess(FileName,Param.GetBuffer(0),.....),
    NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))

    Hussam


  6. #6
    Join Date
    Apr 1999
    Location
    Chennai, INDIA
    Posts
    27

    Re: how do i convert const char* to a char*???

    hi,
    could you please give an example of const_cast operator. In VC 6.0 help, example is not given and i could not understand how it has to be used.

    Thanks in advance,
    Kalyan


  7. #7
    Join Date
    May 1999
    Posts
    4

    Re: how do i convert const char* to a char*???

    You could use const_cast or a (char*) cast but if CString returns a const char*, there is reason: the class must be the only one to access the internal of the string otherwise it can't know what happens and for example it will not know it size anymore...
    So you have two possiblities:
    use GetBuffer, but don't forget to call ReleaseBuffer just after
    copy the string


    Ithier

  8. #8
    Join Date
    Apr 1999
    Posts
    383

    Re: how do i convert const char* to a char*???

    VC++6 help says:

    const_cast < type-id > ( expression )

    This is used as follows:

    const char* pFoo = "Hello";
    char* pBar = const_cast < char* > ( pFoo );

    I wouldn't use this for your example if I were you, because your example is
    trying to convert from CString to char*, not const char* to char*.

    To cast directly from CString to char*, you'll need a double cast - first to const char*, and then to char*, as CString doesn't have a char* conversion, and C++ doesn't allow direct casting on implicit conversions. It's easier to use CString::GetBuffer().

    Dave



  9. #9
    Guest

    Re: how do i convert const char* to a char*???

    CString cstrTest;

    char *pTest = (char*)(LPCTSTR)cstrTest;


  10. #10
    Join Date
    Apr 1999
    Location
    Chennai, INDIA
    Posts
    27

    Re: how do i convert const char* to a char*???

    hi,
    I have a doubt in the const_cast operator. I coded,
    const char *p = "Testing";
    char *q = const_cast<char*> (p);
    *q = 't';

    I got an application error in the statement *q = 't'; Why is it giving error. actually as per the vc++ documentation, const_cast operator removes the const, volatile attributes. so the above code should change "Testing" to "testing". can anyone explain this.

    thanks in advance,
    Kalyan


  11. #11
    Join Date
    Apr 1999
    Posts
    383

    Re: how do i convert const char* to a char*???

    The error shows that the cast worked. The assignment *q = 't' in your example has undefined behaviour as it's trying to modify the value of a string literal.

    Try it on a stack variable:

    char buf[] = "Testing";
    const char *p = buf;
    char *q = const_cast<char*> (p);
    *q = 't';

    Dave


  12. #12
    Join Date
    Apr 1999
    Posts
    11

    Re: how do i convert const char* to a char*???

    Use Param->GetBuffer(1)

    Rishi


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