Friday, July 4, 2008

Wrapping progress

Compiled the Standard and MMgr packages and integrated the dll into a C# application. Could execute successfully in C# a code like:

// build a managed object passing a managed string
OCStandard_Type standardType = new OCStandard_Type("test");
// call managed methods
UInt32 t = standardType.IsImported();
Int32 sz = standardType.Size();
// call methods that return managed Strings built from native char*
String s = standardType.Name();

Made marshaling for the String^ to be received as parameter and also to be returned by functions. It remained to add to the code generator some constructor that builds a managed object receiving as parameter a native class and that should cover the functionality needed for the wrappers and will start wrapping. Another improvement will be added: the native types will be changed with managed types like: Standard_Boolean -> bool.

There is one problem that couldn't be solved completely: although the marshaling from native char* to managed String^ works ok, the marshaling from String^ parameters to native char* creates problems because it is needed to allocate a char* buffer (because the managed String holds internally a wchar) and some of the native classes that receive char* parameters store internally the pointers. If the allocated buffer is released then the internal pointers points to an invalid area:

// Constructor of the Standard_Type wrapper
OCStandard_Type::OCStandard_Type(String^ aName)
// the managed pointer is pinned
pin_ptr tmp = PtrToStringChars(aName);
// the wchar is converted to char* and passed to the native constructor
nativeHandle = new Handle_Standard_Type(new Standard_Type(TCollection_AsciiString((Standard_ExtString)tmp).ToCString()));

// the allocated char* buffer is released when returning, if the native class
// doesn't copy the char* internally it will point to an invalid area

// found some cases of native classes implemented incorrectly that hold internally
// the char* pointer passed as parameter

Tomorrow will continue with:
- finish and test with C# the constructor that receives a native handle as parameter,
- modify the generator to replace the native types with their correspondent from .Net, the marshaling for strings is made,
- made some code cleanup at the generator to prepare it for uploading on SVN,
- generate again the gp package and the 4 packages it depends on to see if more problems are encountered.

If everything goes with no problems will start generating wrappers for more packages.

No comments: