|
-
April 29th, 2004, 09:36 AM
#1
Compilation Error VC++
While compiling a VC++ 5.0 code in VC++ .NET 2003, I get the following error
______________________________________________
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\cstringt.h(875) : error C2664: 'PtrToStringChars' : cannot convert parameter 1 from 'unsigned char *' to 'const System::String __gc *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
ServiceRegistry.cpp(90) : see reference to function template instantiation 'ATL::CStringT::CStringT(SystemString *)' being compiled
with
[
BaseType=char,
StringTraits=StrTraitMFC_DLL,
SystemString=unsigned char
]
________________________________________________
Here is the function referred to by this error in ServiceRegistry.cpp
______________________________________________
CString CServiceRegistry::GetString(const char * name) const
{
unsigned long type;
unsigned char buf[1024];
unsigned long size=1023;
long res=RegQueryValueEx( Key, name, 0, &type, buf, &size);
if(res==ERROR_SUCCESS ) return buf; //Line 90
else return "";
}
______________________________________________
Can somebody please help?
Thanks
-
April 29th, 2004, 09:43 AM
#2
CString CServiceRegistry::GetString(const char * name) const
{
unsigned long type;
unsigned char buf[1024];
unsigned long size=1023;
long res=RegQueryValueEx( Key, name, 0, &type, buf, &size);
if(res==ERROR_SUCCESS ) return buf; //Line 90
else return "";
}
Which type do Key, name have?
PS:
You are trying to return the pointer to local char array buf.
But it will be out of scope after your GetString returns, and the rusult will be unpredictable!
You should create CString variable from this buf and return this CString!
-
April 29th, 2004, 10:03 AM
#3
Originally posted by VictorN
Which type do Key, name have?
PS:
You are trying to return the pointer to local char array buf.
But it will be out of scope after your GetString returns, and the rusult will be unpredictable!
You should create CString variable from this buf and return this CString!
This is incorrect. Since the return type of the function is CString, the compiler is trying to convert buf to a CString. If it succeeds, it copies the buffer.
Apparently the 5.0 version of CString allowed this (it was non-template), but the current version barfs.
It's not 100% clear to me how to fix it, but you could try
return (const unsigned char *) buf;
return (const char *) buf;
return CString(buf);
return CString((const char *) buf);
-
April 29th, 2004, 11:36 AM
#4
cannot convert parameter 1 from 'unsigned char *' to 'const System::String __gc *'
Going by the error message, this looks like a managed C++ application, and not a regular MFC/C++ app. If the non-managed CString no longer takes a const char *, then a whole lot of unmanaged code would become broken.
I believe that managed C++ is handled in the Visual C++ Net forum.
Regards,
Paul McKenzie
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
|