Regasm and Com Interop false negatives

2019-06-03 05:43发布

问题:

I'm attempting to expose our library via COM but seem to be fighting the tools more than the actual problem. Regardless of how I write my COM exposed class I get the warning: "XXX.dll does not contain any types that can be registered for COM interop" (marked project as Register for COM interop in the project properties). Even the simple class below (as the only class in an assembly that is signed and marked with ComVisible(false)) still warning persists:

  [Guid("77699130-7D58-4d29-BE18-385871B000D1")]
  [ComVisible(true)]
  public interface IExample
  {
    string GetText();
    void SetText(string text);
  }

  [Guid("F91E5EE1-D220-43b5-90D1-A48E81C478B7")]
  [ComVisible(true)]
  public class Example : IExample
  {
    private string m_text = "default text";

    public string GetText()
    {
      return m_text;
    }

    public void SetText(string text)
    {
      m_text = text;
    }
  }

I've tried to run regasm from the command line on the output which gladly states "Types registered successfully". However, running with the /verbose switch no extra information is displayed (I seem to recall that it lists the name or perhaps just the number of registered types). Also tried running regasm with the /regfile switch to generate a regfile which generates this:

[HKEY_CLASSES_ROOT\ComExample.Example]
@="ComExample.Example"

[HKEY_CLASSES_ROOT\ComExample.Example\CLSID]
@="{F91E5EE1-D220-43B5-90D1-A48E81C478B7}"

[HKEY_CLASSES_ROOT\CLSID\{F91E5EE1-D220-43B5-90D1-A48E81C478B7}]
@="ComExample.Example"

[HKEY_CLASSES_ROOT\CLSID\{F91E5EE1-D220-43B5-90D1-A48E81C478B7}\InprocServer32]
@="mscoree.dll"
"ThreadingModel"="Both"
"Class"="ComExample.Example"
"Assembly"="COMInteropTesting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8568e57f6b170d6c"
"RuntimeVersion"="v2.0.50727"

[HKEY_CLASSES_ROOT\CLSID\{F91E5EE1-D220-43B5-90D1-A48E81C478B7}\InprocServer32\1.0.0.0]
"Class"="ComExample.Example"
"Assembly"="COMInteropTesting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8568e57f6b170d6c"
"RuntimeVersion"="v2.0.50727"

[HKEY_CLASSES_ROOT\CLSID\{F91E5EE1-D220-43B5-90D1-A48E81C478B7}\ProgId]
@="ComExample.Example"

[HKEY_CLASSES_ROOT\CLSID\{F91E5EE1-D220-43B5-90D1-A48E81C478B7}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}]

I'm no COM wizard of any measure but the registration looks valid to me. However, nothing seems to appear in the registry when running regasm on the assembly.

So my question is whether I'm missing something in my simple example to have the registration work, am I seeing false indications and is the registration actually correct and how can I better determine if the COM registration actually works

EDIT: Using the alterations suggested by Kev I could not avoid the warnings from VS2008 but I could actually get something registered in the registry using regasm directly (nothing in the registry from just the VS2008 integration).

I then tried to remove the simple class so I had an empty assembly. Running regasm on this did not report any sort of warnings, only "Types registered successfully". I see warnings like "RegAsm : warning RA0000 : No types were registered" when running RegAsm on some of my other assemblies which may be incorrectly exposed to COM. What is going on here, can I trust anything except the registry and what about the on/off warnings from regasm?

回答1:

I created a class library project based on your code as follows:

In the project properties page, select the Signing tab. Check the 'Sign the assembly' checkbox and select <New...> from the 'Choose a strong name key file:' drop down box.

I then added the following class (Example.cs):

[Guid("77699130-7D58-4d29-BE18-385871B000D1")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IExample
{
    [DispId(1)]
    string GetText();

    [DispId(2)]
    void SetText(string text);
}

[Guid("F91E5EE1-D220-43b5-90D1-A48E81C478B7")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Example : IExample
{
    private string m_text = "default text";

    [ComVisible(true)]
    public string GetText()
    {
      return m_text;
    }

    [ComVisible(true)]
    public void SetText(string text)
    {
      m_text = text;
    }
}

I then built the project then dropped to a command line where the DLL was output from the build.

To register:

regasm.exe COMInteropTesting.dll /register /codebase /tlb

If your component needs to be accessible to multiple applications:

gacutil.exe -i COMInteropTesting.dll

This all worked fine on Windows 2003 32bit, Windows 2008 32bit and Windows 7 64 bit.

One gotcha though is that if you're testing the component with say CScript on a 64 bit system, you need to run the correct CScript interpreter. If you registered the assembly using 32bit RegAsm.exe (at C:\Windows\Microsoft.NET\Framework\v2.0.50727) then you need to run the 32 bit CScript interpreter at c:\Windows\SysWOW64.