Passing a string to Inventor API methods in .NET 2.0 throws a NullReferenceException

By Wayne Brill

This is an old issue but still could help someone. In my tests using .NET 4.0 with Inventor 2013 does not have this problem.

Issue

Passing a string to the Inventor API methods in .NET 2.0 always throws a NullReferenceException. For example, the following code snippet produces a NullReferenceException:

CustomTable oCustomTable = invSheet.CustomTables.Add ("My Table", locationPoint, 3, 3, ref columnTitles2,  content, columnsWidth,rowHeight, "");
 

Solution

This is a known issue with Microsoft.Net 2.0. You may refer to MSDN for more information. In .NET 2.0, during COM Interop, BSTRs are treated differently.The workaround for the problem is to use InvokeMember to call the Add method of CustomTables instead of directly calling it. Following code uses the InvokeMember to add the custom table.

// Initialize a ParameterModifier with the number of parameters.ParameterModifier p = new ParameterModifier(9);// Pass the fifth(ColumnTitles) parameter by reference.p[4] = true;// The ParameterModifier must be passed as the single element// of an array.ParameterModifier[] mods = { p };CustomTable oCustomTable = (CustomTable)invSheet.CustomTables.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod,null, invSheet.CustomTables, new Object[] { "My Table", locationPoint, 3, 3, oTemp, content, columnsWidth, rowHeight, "" },mods,null,null);

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading