2

I am attempting to invoke a method from a product COM API in my application.

I can successfully invoke the method with a single parameter using the code below but I need to pass some extra parameters (which are added as overloads).

Type _apiType = Type.GetTypeFromProgID("TheAPI.TheServer");
object _api = Activator.CreateInstance(_apiType);

_apiType.InvokeMember(
    "Connect",
    System.Reflection.BindingFlags.InvokeMethod,
    null,
    _api,
    new object[] { 2 } // new object[] {2, "", "" }
);

I have tried adding additional parameters into the code I have above but I get the following error:

"Number of parameters specified does not match the expected number."

I can't seem to find any answer to this, I'm starting to think it's not possible. All the examples I have found suggest using Type.GetMethod() but this doesn't seem to work on COM Objects.

7
  • 1
    Number of parameters specified does not match the expected number So your method "Connect" doesn't expect 1 parameter. Commented Aug 11, 2014 at 19:05
  • 1
    COM does not support method overloads. Double-check your COM interface to make sure the method name hasn't been changed for the second "overload" (e.g. Connect_3) Commented Aug 11, 2014 at 19:06
  • The method 'Connect' has multiple other overloaded methods: void Connect(); void Connect(TheClientType nType); void Connect(TheClientType nType, string strUser, string strPwd); void Connect(TheClientType nType, string strUser, string strPwd, string strNode, string strAlias, bool bAllowUI); Commented Aug 11, 2014 at 19:07
  • As @DStanley suggested...See here: msdn.microsoft.com/en-us/library/28w1w83f(v=vs.110).aspx ... if you created your COM Server ("TheAPI.TheServer") using .NET and created a C# interface with overloaded methods...the unmanaged/published COM signatures of those methods (i.e. in the typelibrary) are decorated... see the section where it mentions "Unmanaged signature". To confirm this, you could use "View | Object Browser | Edit Custom Component Set | (use COM or Browse to choose your COM component)...then you can see what methods are defined in the coclasses and interfaces. Commented Aug 11, 2014 at 20:58
  • @colinsmith and DStanley - Thank you both for your response! Using the Object Browser as colinsmith suggested had shown that the method I was attempting to use was actually called ConnecEx. I have modified my code to use the 'ConnectEx' method and it works like a charm! Thank you! Commented Aug 11, 2014 at 22:46

1 Answer 1

2

COM does not support method overloads. The unmanaged/published COM signatures of those methods (i.e. TypeLibrary) are decorated - see the section that mentions "Unmanaged Signature" in the MSDN article below. http://msdn.microsoft.com/en-us/library/28w1w83f(v=vs.110).aspx

To find the actual method name do the following in Visual Studio: View | Object Browser | Edit Custom Component Set | (Browse to COM Component)

This will show all of the actual method names.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.