Winterdom

If you start using the stack allocation or ref-types feature in C++/CLI, you'll notice that, in if you need to pass the stack-allocated object to a function taking a managed handle (^), you'll get a compiler error. Consider this example:


void PrintIt(String^ s)
{
   Console::WriteLine(s);
}

int main()
{
   String s = "Hello!";
   PrintIt(s);
}

t.cpp(12) : error C2664: 'PrintIt' : cannot convert parameter 1 from 'System::String' to 'System::String^'                                                     
        No user-defined-conversion operator available, or                       
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called                                        

What you need to do in this cases is to create what it known as a tracking reference to the stack allocated object, which is done with the % operator, like this:


   String s = "Hello!";
   PrintIt(%s);
.: tomasr | 2004-10-10 18:49:25 :.