System.InvalidCastException when calling a COM function

  • Thread starter Thread starter Anders Eriksson
  • Start date Start date
A

Anders Eriksson

I have a COM Server, written in C++ by a third party, and on one
function I get an System.InvalidCastException

According to the documentation and the interop "table" it should return
a LaserObject, which is a special object for the Com Server.

Since I haven't yet gotten any response from the developers I thought I
ask here.

Is there someway I can get what return object the function actually returns?

This is the exception:

System.InvalidCastException was unhandled
Message=Return argument has an invalid type.
Source=mscorlib
StackTrace:
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 laserengineLib.LaserImportedClass.getObject(Int32 p_nIndex)
at LighterTest1.Form1.ImportButton_Click(Object sender,
EventArgs e) in C:\Users\ame\Documents\Visual Studio
2010\Projects\TEST\LighterTest1\LighterTest1\Form1.cs:line 83
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at LighterTest1.Program.Main() in C:\Users\ame\Documents\Visual
Studio 2010\Projects\TEST\LighterTest1\LighterTest1\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


// Anders
 
I have a COM Server, written in C++ by a third party, and on one function I
get an System.InvalidCastException

According to the documentation and the interop "table" it should return a
LaserObject, which is a special object for the Com Server.

Since I haven't yet gotten any response from the developers I thought I
ask here.

Is there someway I can get what return object the function actually
returns?

What code are you using to call into this COM server? Are you immedately
attempting to cast the return, like

LaserObject obj = (LaserObject)MyComClass.GetLaserObject([...])

If so, try getting an object (i.e., System.Object) first (don't cast
anything just yet) and then examine it.
 
What code are you using to call into this COM server? Are you immedately
attempting to cast the return, like

LaserObject obj = (LaserObject)MyComClass.GetLaserObject([...])
Yes, this is how I call.
But I have tried to just call only the right part, not assigning the
result to an variable and I still get the same exception.
e.g. MyComClass.GetLaserObject([...])
If so, try getting an object (i.e., System.Object) first (don't cast
anything just yet) and then examine it.

Tried this but same result...

So I have to wait for some response from the developers.

Thank you for your suggestion!

// Anders
 
What code are you using to call into this COM server? Are you immedately
attempting to cast the return, like

LaserObject obj = (LaserObject)MyComClass.GetLaserObject([...])
Yes, this is how I call.
But I have tried to just call only the right part, not assigning the
result to an variable and I still get the same exception.
e.g. MyComClass.GetLaserObject([...])
If so, try getting an object (i.e., System.Object) first (don't cast
anything just yet) and then examine it.

Tried this but same result...

So I have to wait for some response from the developers.

Are you saying that you get System.InvalidCastException
when casting to object??

That sounds weird.

But you could try and print out:

MyComClass.GetLaserObject([...]).GetType().FullName

even without casting.

Arne
 
Are you saying that you get System.InvalidCastException
when casting to object??
Well, I get InvalidCastExecption when I call the function. Even if I
don't assign a variable to the result I get this exception.
That sounds weird.

But you could try and print out:

MyComClass.GetLaserObject([...]).GetType().FullName

I tried
Debug.WriteLine(imp.getObject(0).GetType().FullName);

and got the same exception
the message is:
"Return argument has an invalid type."
The same message that I have always gotten here...

So I'm guessing that the error is in the COM

// Anders
 
MyComClass.GetLaserObject([...]).GetType().FullName
I tried
and got the same exception

the message is:

"Return argument has an invalid type."

So I'm guessing that the error is in the COM
Looks like the GetLaserObject function has an inner error.
 
Back
Top