CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Location
    North Carolina
    Posts
    52

    Marshalling problem in Managed C++ Wrapper

    Doing Managed C++ Wrapper in VS2008. Have a native defined data structure as follows:
    Code:
    class API TF_StringList
    {
    public:
      TF_StringList(const char* encoding = "cp_1252");
        
      ~TF_StringList();
    
      TF_String* first_string() const {return start_string;}
      TF_String* last_string() const {return end_string;}
    
      void insert(const TF_String& aString);
      void insert(const char* aString);  
    
      // If the length parameter is larger than the size of the string,
      // The behavior is uncertain.
      void insert(const tf_Byte* aString, size_t aByteLength);
    
      void clear();
    
      size_t size() const { return list_size; }
    
      char* character_encoding;
    
    private:
      
      void set_first(TF_String* stringPtr) {start_string = stringPtr;}
      void set_last (TF_String* stringPtr) {end_string = stringPtr;}
    
      TF_String* start_string;
      TF_String* end_string;
      TF_String* curr_pos;
      size_t list_size;
    
    
      // Do not expose the copy constructor and the assignment operator.
      TF_StringList(const TF_StringList& tmp);
      TF_StringList& operator=(const TF_StringList& tmp);
    
    };
    In one of my managed functions I have parameter of List<String^> categoryList and I want to load an object of type TF_StringList with the elements of that parameter.
    Code:
    //Instantiate the TF_StringList 
    categories = new TF_StringList(); 
    for(int i=0; i<categoryList.Count; i++) 
    { 
       const char* str = (const char*)(Marshal::StringToHGlobalAnsi(categoryList[i])).ToPointer(); 
       categories->insert(str); 
    }
    On the last line, I get the error:C2663: 'TF_StringList::insert' : 3 overloads have no legal conversion for 'this' pointer
    First, The error would lead me to believe that str is not "seen" by the TF_StringList instance as a const char* since the insert(const char* aString) does not accept it.

    Second, the list does not index out a Managed String (e.g. that String^ st = categoryList[i] is not valid). This seems unlikely since the declaration is List categoryList.

    How can I check to find out what the problem actually is?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Marshalling problem in Managed C++ Wrapper

    C2663 is about const. What is categories variable declaratin? Please post the whole function.

  3. #3
    Join Date
    Sep 2009
    Location
    North Carolina
    Posts
    52

    Re: Marshalling problem in Managed C++ Wrapper

    The complete function so far is:
    Code:
    TFWrapper::TFWrapper(String^ mlexDir, String^ mlic, String^ mkey, List<String^> categoryList, Boolean^ m_useConj, String^ m_lang, String^ m_encoding)
    {
    	const char* c_lexDir = (char*)(Marshal::StringToHGlobalAnsi(mlexDir)).ToPointer();
    	const char* c_lic = (char*)(Marshal::StringToHGlobalAnsi(mlic)).ToPointer();
    	const char* c_key = (char*)(Marshal::StringToHGlobalAnsi(mkey)).ToPointer();
    	finder = new TF_Finder(c_lexDir, c_lic, c_key);
    	catalog = new TF_NameCatalog();
    	categories = new TF_StringList();
    	for(int i=0; i<categoryList.Count; i++)
    	{
    	    const char* str = (const char*)(Marshal::StringToHGlobalAnsi(categoryList[i])).ToPointer();
    	    categories->insert(str);
    	}
    	customEntities = new TF_StringList();
    	c_useConj = (bool*)Convert::ToBoolean(m_useConj);
    }

  4. #4
    Join Date
    Sep 2009
    Location
    North Carolina
    Posts
    52

    Re: Marshalling problem in Managed C++ Wrapper

    Have read where the proper declaration for a parameter of a List of Managed Strings is
    Code:
    List<String^>^% categoryList
    when I index into the list for an item, apparently the return is a String pointer:
    Code:
    String * mstr = categoryList[i];
    Since I'm trying to make it acceptable to the insert method in the unmanaged dll:
    Create the unmanaged class:
    Code:
    TF_StringList * categories = new TF_StringList();
    Then convert mstr to a const char*
    Code:
    const char* str = (char*)(*void)Marshal::StringToHGlobalAnsi(mstr);
    then pass str to the categories->insert(const char*);
    Still get C2663 error

  5. #5
    Join Date
    Sep 2009
    Location
    North Carolina
    Posts
    52

    Re: Marshalling problem in Managed C++ Wrapper

    The problem here was that I had declared the List instance as const. The error is thrown because I'm trying to change a const object by inserting into it.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Marshalling problem in Managed C++ Wrapper

    Code:
    String * mstr = categoryList[i];
    const char* str = (char*)(*void)Marshal::StringToHGlobalAnsi(mstr);
    Doesn't it should be:

    Code:
    const char* str = (char*)(*void)Marshal::StringToHGlobalAnsi(*mstr);
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

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