简体   繁体   中英

COM Interop Not Detecting .NET Functions Via Interface

I have a .NET assembly that I have executed regasm and gacutil . I also have a COM interop that I am trying to get to work with the .NET assembly . However, through my pDotNetCOMPtr I am not able to "detect" any of the methods on my .NET public interface. The MFC COM DLL keeps saying that there is not method called Encrypt in _SslTcpClientPtr when I try to compile with Visual Studio 2010. I am using the .NET 4.0 Framework. Thoughts?

COM / MFC

extern "C" __declspec(dllexport) BSTR __stdcall Encrypt(BSTR encryptString)
{   
    CoInitialize(NULL);

    ICVTnsClient::_SslTcpClientPtr pDotNetCOMPtr;

    HRESULT hRes = pDotNetCOMPtr.CreateInstance(ICVTnsClient::CLSID_SslTcpClient);

    if (hRes == S_OK)
    {
        BSTR str;

        hRes = pDotNetCOMPtr->Encrypt(encryptString, &str);     

        if (str == NULL) {
            return SysAllocString(L"EEncryptionError");
        }
        else return str;    
    }

    pDotNetCOMPtr = NULL;

    return SysAllocString(L"EDLLError");

    CoUninitialize ();
}

.NET

namespace ICVTnsClient
{
    [Guid("D6F80E95-8A27-4ae6-B6DE-0542A0FC7039")]
    [ComVisible(true)]
    public interface _SslTcpClient
    {
        string Encrypt(string requestContent);
        string Decrypt(string requestContent);        
    }

    [Guid("13FE33AD-4BF8-495f-AB4D-6C61BD463EA4")]
    [ClassInterface(ClassInterfaceType.None)]
    public class SslTcpClient : _SslTcpClient
    {

       ...
       public string Encrypt(string requestContent) { // do something }

       public string Decrypt(string requestContent) { // do something }

    }
  }
}

That's because you forgot the [InterfaceType] attribute so that the interface can be early-bound and the method names appear in the type library. Fix:

[Guid("D6F80E95-8A27-4ae6-B6DE-0542A0FC7039")]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface _SslTcpClient
{
    // etc..
}

ComInterfaceType.InterfaceIsDual allows it to be both early and late bound. Microsoft prefers the default, IsIDispatch, less ways to shoot your foot with late binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM