【发布时间】:2014-01-16 07:36:57
【问题描述】:
我还没有在 StackOverflow 上看到答案。这是在具有下面定义的接口(和项目设置)的 C# 库项目中,因此它作为 COM 组件可见。
目标是通过 COM 接口使互操作行中的函数/方法可见。互操作行提供对旧 C 库/dll 中函数的可见性。
问题通过下面 NativeCalls 类中的 Convert 的互操作行显示。它包括“静态外部”。如果这些(即“静态外部”)包含在类定义上方的接口部分中(如图所示),我会收到错误:
- 修饰符“extern”对此项无效
- 修饰符“静态”对此项无效
如果我删除“extern static”,类中将找不到接口部分中的“Convert”。
如何装饰接口部分中的“Convert”行,这样我就不需要在 NativeCalls 类中编写额外的方法来包装每个互操作行(即那些以“public static extern”开头的方法,例如“转换”。如果我需要这样做,我有数百个要包装 - 也许一个不错的宏程序是为了:)。什么是更好/更短的方法?感谢您的帮助。
[ComVisible(true)]
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83D")]
public interface IComNativeCallsInterface
{
[DispId(1)]
static extern int Convert(ref formatfile fptr); // leaving "static extern" on front causes errors. Removing "static extern" will cause the call "Convert" to not be found in the class.
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA7D"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComNativeCallsEventsInterface
{
}
[ComVisible(true)]
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F93D"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComNativeCallsEventsInterface))]
public class NativeCalls : IComNativeCallsInterface
{
/// Return Type: int
///fptr: formatfile*
[System.Runtime.InteropServices.DllImportAttribute("coldlib.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Convert")]
public static extern int Convert(ref formatfile fptr);
... }
【问题讨论】:
标签: c# javascript .net com activex