Specified cast is not valid exception when moved from v1.0 to v1.1

G

Guest

I have a c# program in which I used cast. That worked perfectly on .Net
platform 1.0. When I moved to .Net 2003, I am getting the execption
"Specified cast is not valid". I have not made any changes to the code. Is
there any difference in project settings or something else between v1.0 and
v1.1?

Thanks in advance.
 
J

Jon Skeet [C# MVP]

Raj said:
I have a c# program in which I used cast. That worked perfectly on .Net
platform 1.0. When I moved to .Net 2003, I am getting the execption
"Specified cast is not valid". I have not made any changes to the code. Is
there any difference in project settings or something else between v1.0 and
v1.1?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Guest

Jon,

Thanks for your reply. Here is a code snippet. It is not complete but it is
small.

IJDPosition, iLogicalDistPort are COM interfaces defined in VC++ idl files.
DPosoition is COM class ( an ATL object ).

AutoMath.IJDPosition pos = null;
pos = new AutoMath.DPositionClass();

iLogicalDistPort.SetCenterLocation((NozzleEntities.IJDPosition)pos); //This
throws Specified cast in not valid exception.

Why cast is needed :
The iLogicalDistPort is in defined in the idl file of
NozzleEntities VC++ project. Following is the idl file:

import "AutoMath.idl";

interface IJLogicalDistPort : IDispatch
{
[id(2), helpstring("method GetCenterLocation")] HRESULT
GetCenterLocation([out,retval] IJDPosition ** ppPosition);
[id(4), helpstring("method SetCenterLocation")] HRESULT
SetCenterLocation([in] IJDPosition * pPostion);
};

library NozzleEntities
{
coclass DistribPorts
{
[default] interface IDispatch;
interface IJLogicalDistPort;
};
}

Since the object is in the type library of NozzleEntities, the method is
expecting NozzleEntities.IJDPosition as argument.

I can only create AutoMath.DPositionClass(), since that is where my
DPosition class is implemented.

I hope the scenario is clear. If not I can provide more details. Basically
I am creating an unmanaged object and calling a method on it in managed code.

My main question is, this used to work in v1.0. I haven't make any changes
except moving to v1.1. Now I get the exception. Is there any known problem
or known solution for this problem.

Thanks,
Raj
 
J

Jon Skeet [C# MVP]

Raj said:
Thanks for your reply. Here is a code snippet. It is not complete but it is
small.

Unfortunately, that still doesn't really help me, as I still can't
reproduce the problem.

Given that it's COM stuff though, I suggest you ask in the interop
group - you're more likely to get an answer there. (I'm not
particularly hot on interop, I'm afraid.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top