我正在制作一个部分用 C++ 和 C# 编写的程序。 C#主要用于GUI。 在我的 C++ 代码中,我创建了 PowerPoint COM 对象并在其中打开了一个演示文稿。
有时我需要在我的 C# 代码中引用 PowerPoint::_PresentationPtr
。调用方法等等。
PowerPoint::_PresentationPtr
是用
_COM_SMARTPTR_TYPEDEF(_Presentation, __uuidof(_Presentation));
它由 Visual Studio 在 msppt.tlh Microsoft.Office.Interop.PowerPoint.dll 绑定(bind)中定义:
struct __declspec(uuid("91493463-5a91-11cf-8700-00aa0060263b"))
/* interface */ PresEvents;
struct /* coclass */ Presentation;
struct __declspec(uuid("9149349d-5a91-11cf-8700-00aa0060263b"))
/* dual interface */ _Presentation;
/* PowerPoint::_PresentationPtr */
_COM_SMARTPTR_TYPEDEF(_Presentation, __uuidof(_Presentation));
因此它使 PowerPoint::_PresentationPtr
易于使用 C++ 中的 COM 对象,调用方法等。
这是 C# Microsoft.Office.Interop.PowerPoint.Presentation
namespace Microsoft.Office.Interop.PowerPoint
{
[CoClass(typeof(PresentationClass))]
[Guid("9149349D-5A91-11CF-8700-00AA0060263B")]
public interface Presentation : _Presentation, PresEvents_Event
{
}
}
我在 C# 中有一个使用 C++ 实现的接口(interface),因此我可以在应用程序的 C# 部分访问它。
ApplicationHost.cs
namespace SampleWpfUserControlLibrary
{
public interface IApplicationHostWindow
{
void OpenDocument();
PowerPoint.Presentation GetPresentation();
void Exit();
}
}
应用程序主机.h
#pragma once
#using <SampleWpfUserControlLibrary.dll>
using namespace SampleWpfUserControlLibrary;
ref class ApplicationHostWrapper : IApplicationHostWindow
{
public:
ApplicationHostWrapper(CMainFrame * pMainFrame)
{
_pMainFrame = pMainFrame;
}
virtual void __clrcall Exit() sealed
{
_pMainFrame->SendMessage(WM_CLOSE);
}
virtual void __clrcall OpenDocument() sealed
{
_pMainFrame->OpenDocument();
}
virtual PowerPoint::_PresentationPtr __clrcall GetPresentation() sealed
{
CMFCBindDoc * pDoc = _pMainFrame->GetDocView()->GetDocument();
return pDoc->GetPresentation();
}
};
但是它不编译。争论 GetPresentation()
没有实现。
由于类型不匹配。
更新:
我应该在 C++ 代码 PowerPoint::_Presentation
或 PowerPoint::_PresentationPtr
中返回什么,我应该在 C# 部分将它映射到什么?或者如何在 C# 中将其转换为 Microsoft.Office.Interop.PowerPoint?我只想在 C# 中引用它。
这些是我得到的错误:**
- Error 3 error C2259: 'ApplicationHostWrapper' : cannot instantiate abstract class
Source\Application\MainFrm.cpp 146 1 Application- Error 1 error C3766: 'ApplicationHostWrapper' must provide an implementation for the interface method
'Microsoft::Office::Interop::PowerPoint::Presentation
^SampleWpfUserControlLibrary::IApplicationHostWindow::getPresentation(void)' source\application\ApplicationHostWrapper.h 39 1 Application
在对象浏览器中的 getPresentation() 方法是这样的:
unknown-type^ getPresentation() Member of SampleWpfUserControlLibrary::IApplicationHostWindow
最佳答案
好的,我找到了解决方案。
在 ref class ApplicationHostWrapper : IApplicationHostWindow
中,通过在智能指针上调用 GetInterfacePtr(),我们可以获得指向接口(interface)的指针。
virtual void* __clrcall GetPresentationPtr() sealed
{
CMFCBindDoc * pDoc = _pMainFrame->GetDocView()->GetDocument();
return (void*)pDoc->GetPresentation().GetInterfacePtr();
}
在 C# 接口(interface)声明 ApplicationHost.cs:
namespace SampleWpfUserControlLibrary
{
public interface IApplicationHostWindow
{
void OpenDocument();
unsafe void* GetPresentationPtr();
void Exit();
}
}
这允许我们在托管代码中创建一个方法,例如在一个类中:
private IApplicationHostWindow _hostWindow;
unsafe private PowerPoint.Presentation getPresentation(){
IntPtr preIntPtr = new IntPtr(_hostWindow.GetPresentationPtr());
return (PowerPoint.Presentation)Marshal.GetUniqueObjectForIUnknown(preIntPtr);
}
唯一的缺点是它需要使用/unsafe 进行编译。我还不知道会产生什么后果。如果有人知道如何克服/unsafe 并以不同的方式做到这一点,欢迎您。
关于c# - 如何使用 C# 接口(interface)将 C++ 智能指针返回到 PowerPoint 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24747681/