Tuesday, May 24, 2005

C# LateBound COM

I made a very simple wrapper for calling com objects in c# and I thought you may find it useful. Currently there are only a couple methods, but they are the most useful items.

Sample usage:
//Create object object myComObject = ComUtil.CreateObject("SomeObject.SomeClass");

//Call Method
object[] parameters = new Object[]{paramValue1, paramValue2, paramValue3, paramValue4};
ComUtil.CallMethod(myComObject, "MethodName", parameters);

//Get a property
Int16 returnValue = (Int16)ComUtil.GetProperty(myComObject, "PropertyName");

//Set a property
ComUtil.SetProperty(myComObject, "PropertyName", value);

//Call a method with a ByRef parameter (recordset)
ADODB.Recordset rs = null;

ParameterModifier byRefParamMod = new ParameterModifier(1);
byRefParamMod[0] = true;

ParameterModifier[] pmArray = {new ParameterModifier(1), byRefParamMod, new ParameterModifier(1),new ParameterModifier(1)};

object[] parameters = new Object[]{paramValue1, rs};
ComUtil.CallMethod(myComObject, "MethodName", parameters, pmArray);



//Call a method with a return value
ADODB.Recordset rs = (ADODB.Recordset)ComUtil.CallMethod(myComObject, "MethodName", null);

Here is the class:

1 Comments:

At 3/07/2005 12:39 PM , Anonymous said...

thanx a lot, very helpful

 

Post a Comment

Links to this post:

Create a Link

<< Home