ComImport and visibility of interfaces to COM

dotnet framework

    Next

  • 1. New functions in .NET 2.0 ?
    Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short) Return End Fixed What is the "FIXED and END FIXED" and how the syntax error can be "fixed" ??? Thx, Federico
  • 2. Calling .NET classes from Pure C++ COM (no managed extentions)
    I have an Assembly which have a lot of useful classes. I want to use those class in my C++ COM project. For doing so I created a wrapper project. But my project is not getting compiled. I have made the following setting to the project in VS 2003 Basic Runtime Checks: Default Not Using precomiled header Command Line: /CLR I have inherited the properties to each file in this project. When I declare a pointer to an interface in the assembly I get the following error error C3265: cannot declare a managed 'm_pUpdate2' in an unmanaged 'WsusServerUpdates' for this code: IUpdate *m_pUpdate; Please let me know what all settings i am missing or where I am wrong (I dont want to use managed extentions ....Pure C++ COM.....) I also tried #pragma unmanaged but nothing seems to work :(
  • 3. Cast problem
    Hi All, I have made an add-in for Outlook is working fine when you have Office 2003, if I have 2000 or XP I receive this error Unable to cast object type System.__ComObject in type Outlook.ApplicationClass If somebody can help I appreciate. Best Regards Lino
  • 4. regasm-ing nested .net assemblies?
    Hi i have a vb6 application trying to access a .net assembly, "A.DLL". i have regasm-ed and tlbexp-ed(with codebase swich) "A.DLL" so that it is visible to the vb6 app. "A.DLL" is referencing other .net assemblies "B.DLL" and "C.DLL" and "B.DLL" is referencing .net assembly "D.DLL". A, B, C and D reside in the same folder. My question is - do i have to tlbexp and regasm B,C and D so that they are visible to the vb6 app. Thanks Sriram

ComImport and visibility of interfaces to COM

Postby Peter Kenyon » Wed, 31 May 2006 07:51:15 GMT

Hi,

Many of the interfaces implemented by .net controls for use by COM (eg 
IOleContainer, IOleClientSite) are hidden in a class called 
UnsafeNativeMethods, which is declared internal. However, when I tried to 
use a similar methodology to implement COM interfaces, but keep them hidden, 
I found that their methods were never being called. Only by making the 
interfaces public were they ever discovered by COM clients.
After much wracking my brains I discovered that adding the ComImport 
attribute will make interfaces accessible to COM clients even if they are 
not declared public. The MSDN documentation doesn't mention this fact, 
except in the most roundabout of ways:

"ComImportAttribute is a pseudo-custom attribute that indicates that a type 
has been defined in a previously published type library. The common language 
runtime treats these types differently when activating, exporting, coercing, 
and so on."

Hopefully this will save you some time if you have the same problem.

Cheers

Peter 



Similar Threads:

1.Problem with casting same COMImport interface from 2 different assemblies

A class in assembly A implements a COM interface as follows :

[ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMyInterface
{
[PreserveSig]
int MyMethod();
}

class MyClass : IMyInterface
{
int MyMethod()
{
...
}
}

This interface is exposed to the unmanaged world via COM Interop.

Now, assembly B also defines IMyInterface and gets this interface from the 
unmanaged world as follows :

IntPtr unk = GetMyInterface(); // Gets IMyInterface from unmanaged world
object obj = Marshal.GetObjectFromIUnkown(unk); // its really the class 
MyClass from assembly A above
IMyInterface myIntf = obj as IMyInterface; // this cast does not work and 
returns null becuase of separate declarations of IMyInterface in the two 
assemblies !

How to get this scenario to work? One would think that .Net would recognize 
that both the interfaces are the same ( based on the ComImport and Guid 
attributes) and allow the cast, but it doesnt allow the cast.

It is not possible for me to use a shared assembly and put the declaration 
of IMyInterface in that assembly.

So how can I solve this problem?

Thanks
Bob


2.Casting problem : COMImport interfaces in different assemblies

A class in assembly A implements a COM interface as follows :

[ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMyInterface
{
[PreserveSig]
int MyMethod();
}

class MyClass : IMyInterface
{
int MyMethod()
{
...
}
}

This interface is exposed to the unmanaged world via COM Interop.

Now, assembly B also defines IMyInterface and gets this interface from the
unmanaged world as follows :

IntPtr unk = GetMyInterface(); // Gets IMyInterface from unmanaged world
object obj = Marshal.GetObjectFromIUnkown(unk); // its really the class
MyClass from assembly A above
IMyInterface myIntf = obj as IMyInterface; // this cast does not work and
returns null becuase of separate declarations of IMyInterface in the two
assemblies !

How to get this scenario to work? One would think that .Net would recognize
that both the interfaces are the same ( based on the ComImport and Guid
attributes) and allow the cast, but it doesnt allow the cast.

It is not possible for me to use a shared assembly and put the declaration
of IMyInterface in that assembly.

So how can I solve this problem?

Thanks
Bob



3.COM interface requires Integer in VB, even though INT_PTR is defined in COM interface

4.COM interface requires Integer in VB, even though INT_PTR is defined in COM interface

Hi,


I have a small problem. I have defined some interfaces in a COM dll,
and have defined some parameters in these interfaces as INT_PTR. I
thought that when I import the COM dll in Visual Basic.NET that I can
use IntPtr as the type for these parameters, but the intellisense
shows me that the parameters in the interfaces have been changed to
Integer instead of the VB type IntPtr.

So, now I need to cast to Int32. I would also like to compile this
code for x64, wherease I need to cast it to Int64. Is this normal
behavior and do I need to distinguish between x86 and x64 mode when
passing arguments to a COM interface that has INT_PTR as one of its
parameters, or is there another trick to have the interfaces become
compatible with the Visual Basic IntPtr type, regardless of x86 or
x64?

Also, what happens with "Option Strict Off"? I prefer not to use this
though.

Thanks in  advance.

5.Passing a COM interface through a COM interface

6. Explicity interface visibility and Namespace Question

7. interface, implementation, and visibility

8. Expose a .NET interface to COM as an interface



Return to dotnet framework

 

Who is online

Users browsing this forum: No registered users and 26 guest