|
Hi all,
I am using strcpy function to copy strings, but its giving a error error C2664: 'strcpy' : cannot convert parameter 2 from 'CString' to 'const char *'. i tried it using sprintf function but its also not working...
How can i do it. thanks in advance
|
|
|
|
|
If you are using VC6, the project won't be unicode by default, and CString has an implicit conversion to LPCTSTR, which would be const char * .
For later versions (which are unicode by default), that would be an implicit conversion to const wchar_t * .
Try using lstrcpy - it will switch between char and wchar_t automatically. If you change your char string to a TCHAR one, things will carry on nicely.
If this is all making you go "huh?", then you need a good google search on TCHAR, and do a bunch of reading.
Iain.
|
|
|
|
|
try this
CString mystring("bla bla");
mystring.GetBuffer() -> gives the pointer of buffer
try to copy this one..
|
|
|
|
|
dehseth wrote: mystring.GetBuffer()
That's the worst thing you could have suggested ! Did you read the documentation about this function ?
Anyway, if the cast operator doesn't work, the GetBuffer function won't work neither.
|
|
|
|
|
well i have used it all the time...
and it's bad because?
|
|
|
|
|
You have used it all the time: that's not a good reason to go suggest it to everyone on the internet.
dehseth wrote: and it's bad because?
http://vcfaq.mvps.org/mfc/9.htm[^] plus, read the doc about this function.
[Add] Cedric has said it far better. [/Add]
It is a crappy thing, but it's life -^ Carlo Pallini
modified on Tuesday, October 21, 2008 7:07 AM
|
|
|
|
|
But VCProgrammer wants to save it to another location..
If he is using one thread he can get buffer and copy and that's no harm at all.
He wants to use strcpy function to copy the content of CString to another location, and he can do it by getting the buffer pointer. After copying contents it's no more he's concern about the CString's buffer.
I do not suggest to get buffer pointer and store it and use it. That'd have been really wrong.
As I said I do this in a common DLL I ahve written and use it all my app, and still don't have any problem about losing strings or crashing any application..
|
|
|
|
|
dehseth wrote:
I do not suggest to get buffer pointer...and use it.
Actually you did.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
dehseth wrote: and it's bad because?
Read the function documentation, you'll see why it is very bad to use it without knowing what it does.
EDIT: by the way, the GetBuffer returns a LPTSTR and the CString supports a cast operator to a TCHAR string also. So, if the conversion to a char* is not 'automatic', using GetBuffer won't solve the problem.
For more info, reaf the article I suggested in my other post.
modified on Tuesday, October 21, 2008 7:03 AM
|
|
|
|
|
dehseth wrote: GetBuffer()
GOD, another one who doesn't know what he's talking about !!!
|
|
|
|
|
As usual, a link to the must read article[^]
|
|
|
|
|
|
thanx for articles guys..
|
|
|
|
|
Try this:
//CString to char pointer
1.
CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;
2.
char *pBuffer = new char[1024];
CString strBuf = "Test";
pBuffer = strBuf.GetBuffer(sizeof(pBuffer));
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
"_$h@nky_" wrote: strBuf.GetBuffer(sizeof(pBuffer));
bloody hell, that's even worst than what was proposed earlier !!!
to make it short : NEVER USE GetBuffer() FOR A CAST PURPOSE !!
you made it good with (LPCTSTR) cast operator, stick with it !
and please, read the other answers before replying
|
|
|
|
|
"_$h@nky_" wrote: 1.
CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;
This is wrong: if UNICODE is not defined, you don't need any casts. If UNICODE is defined, the results will be completely wrong. Please read the article from Michael Dunn (see one of my previous replies) before answering such questions.
"_$h@nky_" wrote: 2.
char *pBuffer = new char[1024];
CString strBuf = "Test";
pBuffer = strBuf.GetBuffer(sizeof(pBuffer));
This is plainly wrong too, see the other replies.
Please read the previous answers before posting !
|
|
|
|
|
"_$h@nky_" wrote: //CString to char pointer
1.
CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;
2.
char *pBuffer = new char[1024];
CString strBuf = "Test";
pBuffer = strBuf.GetBuffer(sizeof(pBuffer));
Why not
3.
char *pBuffer;
CString strBuf = "Test";
pBuffer = (char *) &strBuf;
to go out with a bang?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Here's a solution I've been using to this problem:
#define STOC(X) ((char *)(const char *)(X))
STOC stands for "String TO Char". You can't cast a CString directly to a char*, but you can do it in two steps. This has been working for me.
|
|
|
|
|
Alan Balkany wrote: Here's a solution I've been using to this problem:
Alan, the problem is not the conversion between char* and const char* .
The problem is that he's building with support for unicode and he's using the ansi version of strcpy() , which of course expects a const char* , but the CString object holds a wchar_t* .
The wide-string version of CString automatically converts to a const wchar_t* , which would be expected by the wide-string version of strcpy() : wcscpy() .
Iain explained this in the first reply.
Regarding your suggested macro, I'm surprised to find that the stunt slips through the compiler; converting a const pointer to a non-const pointer....
I tried and actually succeeded to convert a CString object via the CSimpleString::operator PCXSTR() , which returns a const pointer to the string, to a wchar_t* and modify the string.
I didn't think the compiler would let me do that and I'll treat it as a compiler bug. I mean, the whole idea behind a const type is that you're not allowed to modify it....
It's even worse than the const_cast<> keyword.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Dear friends,
My doubt is regarding the exception setting in Windows firewall.
I want to check (by code) whether a specified application is set as Firewall exception. I am trying to do it in VC6.
Is there any API for checking this?
Regards,
Krishnakumar
|
|
|
|
|
If you install Windows SDK there are lot samples for manipulating windows firewall. You can also try here[^].
-Saurabh
|
|
|
|
|
|
Dears friends,
I want to develop an application.
The input for the application is a COM dll filename.
The application will display (output of the application) whether the input COM dll is a registered one or not.
Could you please tell me how can i do this?
I mean how to identify the DLL is a registered one or not.
Regards,
Krishnakumar
|
|
|
|
|
It's not going to be easy. A dll get's registered by writing a registry entry with a GUID that points to the DLL. I'm not aware of any information that is the other way around.
For the ATL wizard DLLS, you could load the resources, search for GUIDs, and look them up, but that only works if people have used the ATL classes. Not always true...
About the only way I can see is to brute force it, by enumerating all the HKCR\CLSID subkeys, and see if any of them end up pointing to your DLL.
It can also be a nit more complex... They may not specify a full path, and may instead use just "mydll.dll", and let windows fill in the path for it.
Not much joy for you, sorry.
Iain.
|
|
|
|