Reputation: 21
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!
Upvotes: 0
Views: 626