0

my project is about to control a device with the software I am working on. For doing this the vendor provides a COM library. I have a problem calling one method from this library:

In the (unfortunately very shortly written) documentation of the COM library the method I have to call is defined as this:

Xclass *GetDeviceInterface(BSTR p_SerialNumber)

But: Visual Studio always tells me that I have to put a string into this method:

public virtual Xclass GetDeviceInterface(string p_SerialNumber)

So I am only able to compile my program if I plug in the Device's serial number as a string and NOT as a BSTR. For test purposes I am calling this method in an if case (I just check if something is returned).

IntPtr serialBstr = Marshal.StringToBSTR(serialNo);
if (obj1.GetDeviceInterface(Marshal.PtrToStringBSTR(serialBstr)) != null)
                    {
                        MessageBox.Show("fine");
                    }

But if I do so I always get a InvalidCastException:

System.InvalidCastException: Return argument has an invalid type. at System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg(Object arg, Type paramType) at
System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParameters(IMessage msg, Object[] outArgs, Object returnValue)
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at theUsedCOMLib.class1.getInterface(String serialNo) at MyProject.Form1.initialize() in path/to/my/file.cs:line 00

I think it could work fine, if I would be able to compile it with the BSTR as argument. Can I somehow trick the compiling process? Or just ignore the information (that says I need a string for this method) that Visual Studio reads from the library?

Thank you for your help!

sjss
  • 21
  • 2
  • 2
    Why don't you want to use a simple `string`, as suggested by Visual Studio? If you use `string`, the .NET interop layer should automatically take care of any necessary conversions between managed strings and unmanaged BSTRs. – Heinzi Mar 24 '14 at 14:13
  • Try use this attribute at the declaration of the function [MarshalAs(UnmanagedType.AnsiBStr)] String p_SerialNumber – Jacob Mar 24 '14 at 14:19
  • 1
    It is complaining about the return type, Xclass. Pretty unclear how you managed to get the compiler to compile that code. Contact the vendor for support. – Hans Passant Mar 24 '14 at 14:38
  • Heinzi, if I just use a simple simple string I get my InvalidCastException. That's why I thought of directly using a BSTR. Jacob, I did not declare the function anywhere in my code. I made an object obj1. The class of this object is defined in the COM library and in this class the function is defined. Do I need to define the function anyway again in my code? – sjss Mar 24 '14 at 15:39
  • @sjss: As Hans says, the problem seems to be the return type, not the parameter. I suggest that you start a new question on this, in which you describe how you defined Xclass to begin with. I don't think this has anything to do with BSTR vs. string. – Heinzi Mar 24 '14 at 17:54

0 Answers0