Click to See Complete Forum and Search --> : how do i convert const char* to a char*???


Anish Mistry
May 3rd, 1999, 07:01 PM
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

sshelton
May 3rd, 1999, 07:15 PM
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.

Ramkumar Ganesan
May 3rd, 1999, 10:58 PM
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

balak yap
May 4th, 1999, 02:43 AM
cast if from const char* to char* example,

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

Hello World!!!

Hussam
May 4th, 1999, 02:49 AM
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

pkraman
May 4th, 1999, 04:15 AM
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

ithier
May 4th, 1999, 05:38 AM
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

Dave Lorde
May 4th, 1999, 08:36 AM
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

May 4th, 1999, 08:25 PM
CString cstrTest;

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

pkraman
May 4th, 1999, 11:29 PM
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

Dave Lorde
May 5th, 1999, 04:34 AM
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

Rishi
May 5th, 1999, 02:48 PM
Use Param->GetBuffer(1)

Rishi