好的,这是我的MFC代码:
void CTestDlg::OnBnClickedButtonTest()
{
MSAToolsLibrary::IMSAToolsLibraryInterfacePtr p;
HRESULT hr;
hr = p.CreateInstance(__uuidof(MSAToolsLibrary::MSAToolsLibraryClass));
if (SUCCEEDED(hr))
{
__int64 i;
p->SetPathXML(m_strPublisherDatabaseXML.AllocSysString(), &i);
p->ReadPublisherData(&i);
CString strName = _T("Andrew Truckle");
CComBSTR bstrName = strName.AllocSysString();
VARIANT_BOOL bResult;
MSAToolsLibrary::_PublisherPtr pPublisher = NULL;
hr = p->GetPublisher(bstrName, &pPublisher, &bResult);
if (SUCCEEDED(hr))
{
if (bResult == VARIANT_TRUE)
{
AfxMessageBox(_T("Found"));
}
else
AfxMessageBox(_T("Not found"));
}
else
{
AfxMessageBox(_T("Failed"));
}
}
}
这是TLH中定义的方法:
virtual HRESULT __stdcall GetPublisher (
/*[in]*/ BSTR strPublisher,
/*[out]*/ struct _Publisher * * thePublisher,
/*[out]*/ VARIANT_BOOL * bResult ) = 0;
对于初学者,我不确定是否必须使用_Publisher
或_PublisherPtr
。
这是C#DLL中的方法:
public void GetPublisher(string strPublisher, out Publisher thePublisher, out bool bResult)
{
bResult = false;
thePublisher = null;
if(_PublisherData.PublisherDictionary.ContainsKey(strPublisher))
{
bResult = true;
thePublisher = _PublisherData.PublisherDictionary[strPublisher];
}
}
我不明白的是,在MFC中,我看不到发布商的属性,比如Name等:
我可以在对象浏览器中看到它:
这是Publisher
类:
using MSAToolsLibrary.PublisherEntry.AssignmentInfo;
using System.Xml.Serialization;
namespace MSAToolsLibrary.PublisherEntry
{
public enum Appointed
{
NotAppointed,
MinisterialServant,
Elder
}
public enum Serving
{
UnbaptisedPublisher,
Publisher,
RegularPioneer
}
public enum Gender
{
Male,
Female
}
public class Publisher
{
public string Name
{
get => _Name; set => _Name = value;
}
private string _Name;
public string Notes
{
get => _Notes; set => _Notes = value;
}
private string _Notes;
[XmlAttribute]
public Gender Gender
{
get => _Gender; set => _Gender = value;
}
private Gender _Gender;
[XmlAttribute("Appointed")]
public Appointed AppointedAs
{
get => _AppointedAs; set => _AppointedAs = value;
}
private Appointed _AppointedAs;
[XmlAttribute("Serving")]
public Serving ServingAs
{
get => _ServingAs; set => _ServingAs = value;
}
private Serving _ServingAs;
public Availability Availability
{
get => _Availability; set => _Availability = value;
}
private Availability _Availability = new Availability();
public Assignments Assignments
{
get => _Assignments; set { _Assignments = value; }
}
private Assignments _Assignments = new Assignments();
}
}
图书馆的界面:
[Guid("xxx")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface IMSAToolsLibraryInterface
{
void SetPathXML(String strPathXML, out Int64 iResult);
void ReadPublisherData(out Int64 iResult);
void GetPublisher(string strPublisher, out Publisher thePublisher, out bool bResult);
}
我需要做什么?
我仍然缺少一些东西。我关掉了Make assembly COM visible
。我已经添加了GUID,我需要再次编译它。
然后我改变了我的Publisher类:
using MSAToolsLibrary.PublisherEntry.AssignmentInfo;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
namespace MSAToolsLibrary.PublisherEntry
{
[Guid("xxx")]
[ComVisible(true)]
public enum Appointed
{
NotAppointed,
MinisterialServant,
Elder
}
[Guid("xxx")]
[ComVisible(true)]
public enum Serving
{
UnbaptisedPublisher,
Publisher,
RegularPioneer
}
[Guid("xxx")]
[ComVisible(true)]
public enum Gender
{
Male,
Female
}
[Guid("xxx")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface IPublisher
{
}
[Guid("xxx")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Publisher : IPublisher
{
public string Name
{
get => _Name; set => _Name = value;
}
private string _Name;
public string Notes
{
get => _Notes; set => _Notes = value;
}
private string _Notes;
[XmlAttribute]
public Gender Gender
{
get => _Gender; set => _Gender = value;
}
private Gender _Gender;
[XmlAttribute("Appointed")]
public Appointed AppointedAs
{
get => _AppointedAs; set => _AppointedAs = value;
}
private Appointed _AppointedAs;
[XmlAttribute("Serving")]
public Serving ServingAs
{
get => _ServingAs; set => _ServingAs = value;
}
private Serving _ServingAs;
public Availability Availability
{
get => _Availability; set => _Availability = value;
}
private Availability _Availability = new Availability();
public Assignments Assignments
{
get => _Assignments; set { _Assignments = value; }
}
private Assignments _Assignments = new Assignments();
}
}
它编译。然后在MFC中我改变了它:
void CTestDlg::OnBnClickedButtonTest()
{
MSAToolsLibrary::IMSAToolsLibraryInterfacePtr p;
HRESULT hr;
hr = p.CreateInstance(__uuidof(MSAToolsLibrary::MSAToolsLibraryClass));
if (SUCCEEDED(hr))
{
__int64 i;
p->SetPathXML(m_strPublisherDatabaseXML.AllocSysString(), &i);
p->ReadPublisherData(&i);
CString strName = _T("Andrew Truckle");
CComBSTR bstrName = strName.AllocSysString();
VARIANT_BOOL bResult;
MSAToolsLibrary::IPublisherPtr pPublisher;
// So I don't need to use pPublisher.CreateInstance
hr = p->GetPublisher(bstrName, &pPublisher, &bResult);
if (SUCCEEDED(hr))
{
if (bResult == VARIANT_TRUE)
{
AfxMessageBox(_T("Found"));
}
else
AfxMessageBox(_T("Not found"));
}
else
{
AfxMessageBox(_T("Failed"));
}
}
}
但我仍然看不到Intelisense中的任何成员。我错过了什么?
好的,似乎interface
不能包含这些字段?所以我尝试在类的名称中添加一个公共方法(作为示例):
[Guid("xxx")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface IPublisher
{
string GetName();
}
[Guid("xxx")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Publisher : IPublisher
{
public string GetName()
{
return Name;
}
public string Name
{
get => _Name; set => _Name = value;
}
private string _Name;
}
那很有用。我可以从GetName
致电MFC
。
答案 0 :(得分:1)
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface IMSAToolsLibraryInterface
你知道如何正确地做到这一点。但是你没有为你的Publisher类做正确的事。您可能已经发现了这个并使用另一个锤来解决问题,您使用了Project>属性>申请>装配信息> "使组件COM-Visible"。
这是一个使所有公共类型可见的大锤解决方案。现在[ClassInterface]属性开始变得重要,它确定了如何将类自动转换为接口。必需,因为COM只使用接口。但Publisher类没有明确地具有该属性。所以你发现你不喜欢默认的ClassInterfaceType.AutoDispatch。这使得自动生成的界面看起来像这样:
interface _Publisher : IDispatch {
};
只是IDispatch接口成员是"继承",没有类成员。正如您在IntelliSense框中看到的那样,Invoke等是IDispatch接口成员。对脚本语言很有用,如果您喜欢IntelliSense,则无用。
这样做是正确的。考虑关闭复选框,它没有帮助你。您可以使用快捷方式并将[ClassInterface(ClassInterfaceType.AutoDual)]应用于Publisher类。不太干净,因为它还公开了从System.Object继承的成员,并且类型库将依赖于mscorlib.tlb。或者最正义的方式,即您所知道的方式,声明一个IPublisher接口。而ClassInterface.None现在是您的Publisher类的正确选择。
顺便说一句,请考虑在接口上使用ComInterfaceType.InterfaceIsDual,这样您的COM服务器也可以从脚本语言中使用。