CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    10

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    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!

  3. #3
    Join Date
    Jun 2002
    Posts
    395
    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);

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    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
  •  





Click Here to Expand Forum to Full Width

Featured