|
-
May 3rd, 1999, 07:01 PM
#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
-
May 3rd, 1999, 07:15 PM
#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.
-
May 3rd, 1999, 10:58 PM
#3
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
-
May 4th, 1999, 02:43 AM
#4
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!!!
-
May 4th, 1999, 02:49 AM
#5
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
-
May 4th, 1999, 04:15 AM
#6
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
-
May 4th, 1999, 05:38 AM
#7
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
-
May 4th, 1999, 08:36 AM
#8
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
-
May 4th, 1999, 08:25 PM
#9
Re: how do i convert const char* to a char*???
CString cstrTest;
char *pTest = (char*)(LPCTSTR)cstrTest;
-
May 4th, 1999, 11:29 PM
#10
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
-
May 5th, 1999, 04:34 AM
#11
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
-
May 5th, 1999, 02:48 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|