Monday, September 15, 2008

Programming GeoMedia: CreateService Method

If you are developing a driving GeoMedia custom application using .NET, you may find that some values returned by a GeoMedia object or service may appear odd or incorrect. For example, in the C# code listing below I am trying to use the GeoMathService to calculate a geometry range.

GeoMath.GeoMathService gmathSvc;
GeoMath.point lowerLeftPoint, upperRightPoint;

lowerLeftPoint = new GeoMath.pointClass();
upperRightPoint = new GeoMath.pointClass();

gmathSvc = (GeoMath.GeoMathService) this._application.CreateService("GeoMedia.GeoMathService");
gmathSvc.GetRange(objGeom, lowerLeftPoint, upperRightPoint);

Using the Visual Studio debugger to view the values of lowerLeftPoint.X and lowerLeftPoint.Y, I get the following:

X = 28671.723701195806
Y = 8.6309974485990875E+182

The Y value is overly large and incorrect.

As values are being passed across process boundaries - from managed .NET application code to GeoMedia's COM object and back, memory management problems could arise. The way to avoid the cross process memory allocation problems is to use the GeoMedia application to allocate and manipulate the memory with its CreateService method instead of C#'s new operator.

The good C# code listing is shown below:

GeoMath.GeoMathService gmathSvc;
GeoMath.point lowerLeftPoint, upperRightPoint;

lowerLeftPoint = (GeoMath.point) this._application.CreateService("GeoMedia.point");
upperRightPoint = (GeoMath.point) this._application.CreateService("GeoMedia.point");

gmathSvc = (GeoMath.GeoMathService) this._application.CreateService("GeoMedia.GeoMathService");
gmathSvc.GetRange(objGeom, lowerLeftPoint, upperRightPoint);

Running in debug mode, the values of lowerLeftPoint can be viewed as the following:
X = 28671.723701195806
Y = 28628.034157585753

The values look reasonable.

2 comments:

Itstata said...

Hello,

i'm suprised to see that it is possible to debug geomedia. Can you please tell me how you managed this? Their is a lack of tutorials for geomedia - nice blog!

dominoc925 said...

Hi Itstata, if you are developing a standalone program using GeoMedia objects (not the application framework) it is much easier to debug GeoMedia - similar to any programs done in Visual Studio. If you are writing a GeoMedia Custom Command, then it is a little more difficult because GeoMedia needs to link to the custom command and you need to unload the custom command dll before you can recompile etc.