Monday, July 11, 2011

x86 and x64 Development

If you are developing  an application for use on both Civil 3D 64 bit and 32 bit environment you may come across an issue with ObjectId object. If you compile it on a 32 bit machine the code won’t work on a 64 bit machine. This is due to the fact that the ObjectId object will have the wrong IntPtr.

I recently came across an easy fix for this using p/Invoke. Now I’m not an expert on using p/Invoke, but the way I understand it is that you can call an object without knowing what that object is. In this case we know what it is, we are just able to get the correct IntPtr type at run time instead of when we create the code. Below is the code (c#) Jeff Mishler posted in the discussion group.

object[] oArgs = new object[2];
oArgs[0] = acadObj.GetType().InvokeMember("ObjectID", System.Reflection.BindingFlags.GetProperty, null, acadObj, null);
oArgs[1] = flStyle;
AeccLandFeatureLines flines = site.FeatureLines;
AeccLandFeatureLine fline = flines.GetType().InvokeMember("AddFromPolyline", System.Reflection.BindingFlags.InvokeMethod, null, flines, oArgs) as AeccLandFeatureLine;

If you want more information on how p/Invoke works I’d suggest doing a Google search on it.


Sinc said...

Note that this is only a problem when using the COM libraries, and developing for C3D 2011 or later. Prior to C3D 2011, we only had 32-bit C3D, and did not encounter this issue.

The .NET API is improving, and hopefully it will soon be possible to do all C3D customization from .NET, eliminating the COM references. Then this issue will go away.

Joshua Smith said...
This comment has been removed by a blog administrator.


Blog Widget by LinkWithin