为什么我的C#DLL中的方法丢失了?

时间:2017-01-03 09:56:29

标签: c# mfc tlb

我正在尝试学习如何编写C#DLL并从MFC应用程序中调用它。

这里有一些DLL代码:

using System;
using System.IO;
using System.Xml.Serialization;

namespace MSATools
{
    public class MSAToolsLibrary
    {
        public MSAToolsLibrary()
        {
            _PublisherData = new PublisherData();
        }

        [XmlIgnore]
        private string _strPathXML;

        public void SetPathXML(String strPathXML)
        {
            _strPathXML = strPathXML;
        }

        [XmlIgnore]
        private PublisherData _PublisherData;

        public void SavePublisherData()
        {
            XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
            using (StreamWriter writer = new StreamWriter(_strPathXML))
            {
                x.Serialize(writer, _PublisherData);
            }
        }

        public void ReadPublisherData()
        {
            _PublisherData.Publishers.Clear(); // Reset

            XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
            using (StreamReader reader = new StreamReader(_strPathXML))
            {
                _PublisherData = (PublisherData)x.Deserialize(reader);
            }
        }

        public void AddPublisherData()
        {
            _PublisherData.AddStudent("Andrew", "Andrew notes");
            _PublisherData.AddStudent("Rachel", "Rachel notes");
            _PublisherData.AddStudent("Joshua", "Joshua notes");
            _PublisherData.AddStudent("Finlay", "Finlay notes");
        }
    }
}

我勾选了将其注册为互操作的选项,我有一个TLB文件。

然后我进入了一个新的MFC项目,并按照教程从TLB文件创建一个类。但它创造的只是:

// Machine generated IDispatch wrapper class(es) created with Add Class from Typelib Wizard

#import "D:\\My Programs\\2017\\MSATools\\MSATools\\bin\\Release\\MSATools.tlb" no_namespace
// CMSAToolsLibrary wrapper class

class CMSAToolsLibrary : public COleDispatchDriver
{
public:
    CMSAToolsLibrary() {} // Calls COleDispatchDriver default constructor
    CMSAToolsLibrary(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
    CMSAToolsLibrary(const CMSAToolsLibrary& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}

    // Attributes
public:

    // Operations
public:


    // _MSAToolsLibrary methods
public:

    // _MSAToolsLibrary properties
public:

};

我显然在这里遗漏了一些东西。我期待看到我的公共方法:

SetPathXML
ReadPublisherData
SavePublisherData
AddPublisherData

我错过了什么?

这是类在IDE中的外观(MSATools DLL项目):

Classes

更新

我试图在我的C#库中添加一个接口,我一定做错了:

using System;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.InteropServices;

namespace MSATools
{
    [Guid("xxxxxx")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface iInterface
    {
        void SetPathXML(String strPathXML);
        void SavePublisherData();
        void ReadPublisherData();
        void AddPublisherData();
    }

    [Guid("xxxx")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class MSAToolsLibrary
    {
#region iInterface Members
        public MSAToolsLibrary()
        {
            _PublisherData = new PublisherData();
        }

        [XmlIgnore]
        private string _strPathXML;

        public void SetPathXML(String strPathXML)
        {
            _strPathXML = strPathXML;
        }

        [XmlIgnore]
        private PublisherData _PublisherData;

        public void SavePublisherData()
        {
            XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
            using (StreamWriter writer = new StreamWriter(_strPathXML))
            {
                x.Serialize(writer, _PublisherData);
            }
        }

        public void ReadPublisherData()
        {
            _PublisherData.Publishers.Clear(); // Reset

            XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
            using (StreamReader reader = new StreamReader(_strPathXML))
            {
                _PublisherData = (PublisherData)x.Deserialize(reader);
            }
        }

        public void AddPublisherData()
        {
            _PublisherData.AddStudent("Andrew", "Andrew notes");
            _PublisherData.AddStudent("Rachel", "Rachel notes");
            _PublisherData.AddStudent("Joshua", "Joshua notes");
            _PublisherData.AddStudent("Finlay", "Finlay notes");
        }
#endregion
    }
}

编译好。但是当我转到Visual C ++ MFC并从TypeLib添加一个类并选择此TLB文件没有在列表中显示为可用。

混淆。

更新2

我决定尝试创建自己的类作为包装器:

#pragma once

#import "D:\\My Programs\\2017\\MSATools\\MSATools\\bin\\Release\\MSATools.tlb" raw_interfaces_only named_guids

class CMSATools
{
public:
    CMSATools();
    ~CMSATools();
    void SetPathXML(CString strPath);
    void Test();
};

#include "stdafx.h"
#include "MSATools.h"


CMSATools::CMSATools()
{
}


CMSATools::~CMSATools()
{
}


void CMSATools::SetPathXML(CString strPath)
{
    MSATools::iInterfacePtr     pInterface = NULL;

    HRESULT         hr;
    hr = pInterface.CreateInstance(__uuidof(MSATools::MSAToolsLibrary));

    if (SUCCEEDED(hr))
    {
        CComBSTR    bstrText = strPath.AllocSysString();
        pInterface->SetPathXML(bstrText);
    }
}


void CMSATools::Test()
{
    MSATools::iInterfacePtr     pInterface = NULL;

    HRESULT         hr;
    hr = pInterface.CreateInstance(__uuidof(MSATools::MSAToolsLibrary));

    if (SUCCEEDED(hr))
    {
        CComBSTR    bstrText(_T("d:\\TestStudents.XML"));

        pInterface->SetPathXML(bstrText);
        pInterface->AddPublisherData();
        pInterface->SavePublisherData();
    }
}

它编译,我可以用VisualAssist看到方法:

Syntax

但由于某些原因,hr未通过SUCCEEDED来电。

更新3

如果我将DLL重建为x64并将MFC EXE重建为x64,那么我会得到一个例外:

Exception

更新4

我必须添加到我的MFC应用程序InitInstance

::Coinitialize(true);

ExitInstance

::CoUnitialize();

然后异常就消失了。

1 个答案:

答案 0 :(得分:0)

尝试这个修改,用VS生成一个新的guid,因为我不记得它是否是自动写入另一个文件。

using System.Runtime.InteropServices;
...
[ComVisibleAttribute(true)]
[Guid("xxx-xxx-xxx-xxx-xxx")]
[ProgId("MSAToolsLibraryClass")]    
public class MSAToolsLibrary
{
...

修改

确保Assembly.cs有这样一行:

[议员:Guid(“xxx-xxx-xxx-xxx-xxx”)]