I am trying to write a small c# application to test a COM interface (also written in c#). The interface contains a method which accepts a single string as a parameter.
I’ve included a couple code samples below. I use the following to perform the invocation:
public class CreateObject
{
private Type comType;
public object comObject;
public CreateObject(string ProgID)
{
comType = Type.GetTypeFromProgID(ProgID);
comObject = Activator.CreateInstance(comType);
}
public void Execute(string Method, params object[] Parameters)
{
comType.InvokeMember(Method, BindingFlags.InvokeMethod, null, comObject, Parameters);
}
}
Which I then execute using:
String sParam = "test";
CreateObject obj = new CreateObject("Namespace.Class");
obj.Execute("Method", sParam );
Inside the COM, interface looks like this:
[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("xxxxxx-xxxx-xxxx-xxxx-xxxxxx")]
public interface Interface
{
void Method(String sParam);
}
Simplified implementation of Method(String):
[ClassInterface(ClassInterfaceType.None), Guid("xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"), ProgId("Namespace.Class")]
public class Class: Interface
{
public void Method(String sParam)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(sParam);
XmlWriter writer = XmlWriter.Create("result.xml");
*** other code used to create the xml ***
}
}
Even though there are no errors returned, the COM doesn’t actually seem to execute. However when I take out the string parameter from both the test app and the COM, I do get the correct output (the COM interface creates an XML file on disc). Can anyone see a fault in my use of the parameter?
I’ve found out that class libraries don’t use app.config, which means my COM had no access to any of its settings (connection strings, etc.) and threw errors I couldn’t see until I returned them as part of the try/catch.
So the answer is that use of parameters in this case was correct.