returning IUnknow from COM to C#

S

Scott Holman

Framework 1.1

I've have com interop reference that has a method that returns IUnknown.
How do I cast the IUnknown reference to the specific interface.

I've tried the code below but the call to myInteropRef.Foo() raises a
NullReferenceException. What am I doing wrong?

//instantiate com object
MyInteropRefClass myInteropRef = new MyInteropRefClass();
System.Object fooObj = myInteropRef.Foo();
IMyInterface myIntf = (IMyInterface) fooObj;

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

If you are getting this error, then it means one of two things:

a) myInteropRef is null
b) There is code in the implementation of Foo which is causing a
NullReferenceException to be thrown.

The code you have looks fine, it looks like you arent even getting to
the cast.
 
S

Scott Holman

Thanks Nicholas,

The reference to myInteropRef is not null and I can instantiate this com
object in unmanaged code and foo() returns a non-null reference to another
com object. I'm assuming that the Interop definition of Foo() as
'System.Object Foo()' is correct for an function that returns IUnknown?



Nicholas Paldino said:
Scott,

If you are getting this error, then it means one of two things:

a) myInteropRef is null
b) There is code in the implementation of Foo which is causing a
NullReferenceException to be thrown.

The code you have looks fine, it looks like you arent even getting to
the cast.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
Framework 1.1

I've have com interop reference that has a method that returns IUnknown.
How do I cast the IUnknown reference to the specific interface.

I've tried the code below but the call to myInteropRef.Foo() raises a
NullReferenceException. What am I doing wrong?

//instantiate com object
MyInteropRefClass myInteropRef = new MyInteropRefClass();
System.Object fooObj = myInteropRef.Foo();
IMyInterface myIntf = (IMyInterface) fooObj;

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

Yes, it is, but there should be an attribute on the return type like
this (if you are defining it in code):

[return:MarshalAs(UnmanagedType.IUnknown)]


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
Thanks Nicholas,

The reference to myInteropRef is not null and I can instantiate this com
object in unmanaged code and foo() returns a non-null reference to another
com object. I'm assuming that the Interop definition of Foo() as
'System.Object Foo()' is correct for an function that returns IUnknown?



Nicholas Paldino said:
Scott,

If you are getting this error, then it means one of two things:

a) myInteropRef is null
b) There is code in the implementation of Foo which is causing a
NullReferenceException to be thrown.

The code you have looks fine, it looks like you arent even getting to
the cast.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
Framework 1.1

I've have com interop reference that has a method that returns IUnknown.
How do I cast the IUnknown reference to the specific interface.

I've tried the code below but the call to myInteropRef.Foo() raises a
NullReferenceException. What am I doing wrong?

//instantiate com object
MyInteropRefClass myInteropRef = new MyInteropRefClass();
System.Object fooObj = myInteropRef.Foo();
IMyInterface myIntf = (IMyInterface) fooObj;

Thanks
 
S

Scott Holman

I'm relying on the interop dll generated by tlbimp.

Nicholas Paldino said:
Scott,

Yes, it is, but there should be an attribute on the return type like
this (if you are defining it in code):

[return:MarshalAs(UnmanagedType.IUnknown)]


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
Thanks Nicholas,

The reference to myInteropRef is not null and I can instantiate this com
object in unmanaged code and foo() returns a non-null reference to
another com object. I'm assuming that the Interop definition of Foo() as
'System.Object Foo()' is correct for an function that returns IUnknown?



Nicholas Paldino said:
Scott,

If you are getting this error, then it means one of two things:

a) myInteropRef is null
b) There is code in the implementation of Foo which is causing a
NullReferenceException to be thrown.

The code you have looks fine, it looks like you arent even getting to
the cast.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Framework 1.1

I've have com interop reference that has a method that returns
IUnknown. How do I cast the IUnknown reference to the specific
interface.

I've tried the code below but the call to myInteropRef.Foo() raises a
NullReferenceException. What am I doing wrong?

//instantiate com object
MyInteropRefClass myInteropRef = new MyInteropRefClass();
System.Object fooObj = myInteropRef.Foo();
IMyInterface myIntf = (IMyInterface) fooObj;

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Then you should see that if you look at the interop dll through
reflector.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
I'm relying on the interop dll generated by tlbimp.

Nicholas Paldino said:
Scott,

Yes, it is, but there should be an attribute on the return type like
this (if you are defining it in code):

[return:MarshalAs(UnmanagedType.IUnknown)]


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
Thanks Nicholas,

The reference to myInteropRef is not null and I can instantiate this
com object in unmanaged code and foo() returns a non-null reference to
another com object. I'm assuming that the Interop definition of Foo()
as 'System.Object Foo()' is correct for an function that returns
IUnknown?



in message Scott,

If you are getting this error, then it means one of two things:

a) myInteropRef is null
b) There is code in the implementation of Foo which is causing a
NullReferenceException to be thrown.

The code you have looks fine, it looks like you arent even getting
to the cast.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Framework 1.1

I've have com interop reference that has a method that returns
IUnknown. How do I cast the IUnknown reference to the specific
interface.

I've tried the code below but the call to myInteropRef.Foo() raises a
NullReferenceException. What am I doing wrong?

//instantiate com object
MyInteropRefClass myInteropRef = new MyInteropRefClass();
System.Object fooObj = myInteropRef.Foo();
IMyInterface myIntf = (IMyInterface) fooObj;

Thanks
 
S

Scott Holman

Thanks Nicholas,

The problem was in the COM dll. Apparently, I was just lucky that the code
worked when I tested via the unmanaged delphi application.


Nicholas Paldino said:
Then you should see that if you look at the interop dll through
reflector.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Scott Holman said:
I'm relying on the interop dll generated by tlbimp.

Nicholas Paldino said:
Scott,

Yes, it is, but there should be an attribute on the return type like
this (if you are defining it in code):

[return:MarshalAs(UnmanagedType.IUnknown)]


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks Nicholas,

The reference to myInteropRef is not null and I can instantiate this
com object in unmanaged code and foo() returns a non-null reference to
another com object. I'm assuming that the Interop definition of Foo()
as 'System.Object Foo()' is correct for an function that returns
IUnknown?



"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message Scott,

If you are getting this error, then it means one of two things:

a) myInteropRef is null
b) There is code in the implementation of Foo which is causing a
NullReferenceException to be thrown.

The code you have looks fine, it looks like you arent even getting
to the cast.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Framework 1.1

I've have com interop reference that has a method that returns
IUnknown. How do I cast the IUnknown reference to the specific
interface.

I've tried the code below but the call to myInteropRef.Foo() raises
a NullReferenceException. What am I doing wrong?

//instantiate com object
MyInteropRefClass myInteropRef = new MyInteropRefClass();
System.Object fooObj = myInteropRef.Foo();
IMyInterface myIntf = (IMyInterface) fooObj;

Thanks
 
W

Willy Denoyette [MVP]

If the exception is thrown on:
myInteropRef.Foo();
it's because myInteropRef is null.
If you want to convince yourself, add a check for null.

Willy.

| Thanks Nicholas,
|
| The reference to myInteropRef is not null and I can instantiate this com
| object in unmanaged code and foo() returns a non-null reference to another
| com object. I'm assuming that the Interop definition of Foo() as
| 'System.Object Foo()' is correct for an function that returns IUnknown?
|
|
|
in
| message | > Scott,
| >
| > If you are getting this error, then it means one of two things:
| >
| > a) myInteropRef is null
| > b) There is code in the implementation of Foo which is causing a
| > NullReferenceException to be thrown.
| >
| > The code you have looks fine, it looks like you arent even getting to
| > the cast.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - (e-mail address removed)
| >
| > | >> Framework 1.1
| >>
| >> I've have com interop reference that has a method that returns
IUnknown.
| >> How do I cast the IUnknown reference to the specific interface.
| >>
| >> I've tried the code below but the call to myInteropRef.Foo() raises a
| >> NullReferenceException. What am I doing wrong?
| >>
| >> //instantiate com object
| >> MyInteropRefClass myInteropRef = new MyInteropRefClass();
| >> System.Object fooObj = myInteropRef.Foo();
| >> IMyInterface myIntf = (IMyInterface) fooObj;
| >>
| >> Thanks
| >>
| >
| >
|
|
 

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