Click to See Complete Forum and Search --> : Marshalling problem in Managed C++ Wrapper


jjones7947
April 16th, 2010, 06:57 AM
Doing Managed C++ Wrapper in VS2008. Have a native defined data structure as follows:

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.

//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?

Alex F
April 17th, 2010, 01:37 AM
C2663 is about const. What is categories variable declaratin? Please post the whole function.

jjones7947
April 19th, 2010, 07:13 AM
The complete function so far is:

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);
}

jjones7947
April 23rd, 2010, 10:02 AM
Have read where the proper declaration for a parameter of a List of Managed Strings is

List<String^>^% categoryList

when I index into the list for an item, apparently the return is a String pointer:

String * mstr = categoryList[i];

Since I'm trying to make it acceptable to the insert method in the unmanaged dll:
Create the unmanaged class:

TF_StringList * categories = new TF_StringList();

Then convert mstr to a const char*

const char* str = (char*)(*void)Marshal::StringToHGlobalAnsi(mstr);

then pass str to the categories->insert(const char*);
Still get C2663 error

jjones7947
May 11th, 2010, 06:37 AM
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.

Ajay Vijay
May 11th, 2010, 01:27 PM
String * mstr = categoryList[i];
const char* str = (char*)(*void)Marshal::StringToHGlobalAnsi(mstr);Doesn't it should be:

const char* str = (char*)(*void)Marshal::StringToHGlobalAnsi(*mstr);