gpt4 book ai didi

C# ITypeInfo.GetContainingTypeLib 在传递 VBA 类的实时实例时失败

转载 作者:可可西里 更新时间:2023-11-01 08:30:01 25 4
gpt4 key购买 nike

所以我尝试在 VBA 类实例上调用 ITypeInfo,虽然它看起来很有希望,但我想看看我是否可以获得对其包含项目的引用,类似于类型库。我认为 ITypeInfo.GetContainingTypeLib 可能有用,但它会抛出一个异常,表明 VBA 不会合作。有人知道 VBA 如何以不同于标准 COM 规范的方式做事吗?

C# 类库代码在这里。注册 COM 互操作并在 AssemblyInfo.cs 中设置 COMVisible(true) 以使其可从 VBA 访问。下面给出了 VBA 客户端代码。

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;


namespace TypeLibraryInspector
{
[ComImport()]
[Guid("00020400-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDispatch
{
[PreserveSig]
int GetTypeInfoCount(out int Count);

[PreserveSig]
int GetTypeInfo
(
[MarshalAs(UnmanagedType.U4)] int iTInfo,
[MarshalAs(UnmanagedType.U4)] int lcid,
out System.Runtime.InteropServices.ComTypes.ITypeInfo typeInfo
);


//void GetTypeInfo(int typeInfoIndex, int lcid, [MarshalAs(UnmanagedType.CustomMarshaler,
// MarshalTypeRef = typeof(System.Runtime.InteropServices.CustomMarshalers.TypeToTypeInfoMarshaler))] out Type typeInfo);

//void GetTypeInfo(int typeInfoIndex, int lcid, out IntPtr piTypeInfo);


[PreserveSig]
int GetIDsOfNames
(
ref Guid riid,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
string[] rgsNames,
int cNames,
int lcid,
[MarshalAs(UnmanagedType.LPArray)] int[] rgDispId
);

[PreserveSig]
int Invoke
(
int dispIdMember,
ref Guid riid,
uint lcid,
ushort wFlags,
ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams,
out object pVarResult,
ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
IntPtr[] pArgErr
);
}


public interface IInspector
{
void InspectThisObject(object vbaClassInstance);
}

[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IInspector))]

public class Inspector : IInspector
{
private const int S_OK = 0; //From WinError.h
private const int LOCALE_SYSTEM_DEFAULT = 2 << 10; //From WinNT.h == 2048 == 0x800



void IInspector.InspectThisObject(object vbaClassInstance)
{
//https://limbioliong.wordpress.com/2011/10/18/obtain-type-information-of-idispatch-based-com-objects-from-managed-code/
IDispatch pDispatch = (IDispatch)vbaClassInstance;

ITypeInfo piTypeInfo;
pDispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT, out piTypeInfo);

string s1; string s2; string s3;
int i1;
piTypeInfo.GetDocumentation(-1, out s1, out s2, out i1, out s3);
//s1 = "Class1" good
//s2 = null shame

ITypeLib piTypeLib;
int pIndex;

piTypeInfo.GetContainingTypeLib(out piTypeLib, out pIndex); // <-- throws Exception 0x800A88C1

}
}
}

所以这里有一些客户端 VBA

Sub Test()

Dim oInspector As TypeLibraryInspector.Inspector
Set oInspector = New TypeLibraryInspector.Inspector

Dim oClass1 As Class1
Set oClass1 = New Class1

oInspector.InspectThisObject oClass1

End Sub

其中 Class1 可以是任何类,我有两个空函数,但我认为这不相关。

我问过equivalent C++ question .

最佳答案

如果 dll 是 com 暴露的,它必须用 regsvr32 注册

您可能还需要 GAC .net 程序集

关于C# ITypeInfo.GetContainingTypeLib 在传递 VBA 类的实时实例时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51912746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com