c# interop question-- QueryInterface trouble

J

Jim Bancroft

Hi everyone,

I've recently ported a COM component into .Net using a runtime callable
wrapper. The component in question is SQL Server's DTS package, if that
helps.

The import worked well enough....now I'm having some trouble using the
component. Specifically, this code below from VB6, which works:

Dim oTask As DTS.Task
Dim oCustomTask0 As DTS.ExecuteSQLTask2
Set oTask = goPackage.Tasks.New("DTSExecuteSQLTask")
Set oCustomTask0 = oTask.CustomTask


......doesn't work in my C# app. Here's how I've ported it:

DTS.Task oTask;
DTS.ExecuteSQLTask2 oCustomTask0;
oTask = goPackage.Tasks.New("DTSExecuteSQLTask");
oCustomTask0 = (DTS.ExecuteSQLTask2) oTask.CustomTask; //<----blows up here

On the last line above, where I do a cast, I get a runtime error telling me
QueryInterface failed. I'm not sure why it would fail in C#, when the
equivalent code in VB6 worked. Would anyone have an idea what I've done
wrong? Thanks!
 
J

Jianwei Sun

Jim said:
Hi everyone,

I've recently ported a COM component into .Net using a runtime callable
wrapper. The component in question is SQL Server's DTS package, if that
helps.

The import worked well enough....now I'm having some trouble using the
component. Specifically, this code below from VB6, which works:

Dim oTask As DTS.Task
Dim oCustomTask0 As DTS.ExecuteSQLTask2
Set oTask = goPackage.Tasks.New("DTSExecuteSQLTask")
Set oCustomTask0 = oTask.CustomTask


.....doesn't work in my C# app. Here's how I've ported it:

DTS.Task oTask;
DTS.ExecuteSQLTask2 oCustomTask0;
oTask = goPackage.Tasks.New("DTSExecuteSQLTask");
oCustomTask0 = (DTS.ExecuteSQLTask2) oTask.CustomTask; //<----blows up here

On the last line above, where I do a cast, I get a runtime error telling me
QueryInterface failed. I'm not sure why it would fail in C#, when the
equivalent code in VB6 worked. Would anyone have an idea what I've done
wrong? Thanks!

Why do you need cast in [ oCustomTask0 =
(DTS.ExecuteSQLTask2)oTask.CustomTask;], based on your VB code, I don't
see you need a cast here. Is "CustomTask" defined in the default
interface DTS.Tas implement?
 
D

Dave

The fact that it's not a casting exception leads me to believe that your not doing anything wrong (unless you modified the type
library before importing) and that the wrapper is at fault.

Another possibility:

Your casting to a class it seems, and not an interface. In my experience I've found that marshalled members, in some cases, must be
cast to an interface and not a class. Try breaking on that line in a debugger and see what Type oTask.CustomTask is and try
explicitly casting to that. I'd imagine it will be an interface instead.

GL
 

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