Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#ITypeInfo.GetContainingTypeLib在传递VBA类的活动实例时失败_C#_Excel_Vba_Com_Typeinfo - Fatal编程技术网

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

C#ITypeInfo.GetContainingTypeLib在传递VBA类的活动实例时失败,c#,excel,vba,com,typeinfo,C#,Excel,Vba,Com,Typeinfo,因此,我尝试在VBA类实例上调用ITypeInfo,虽然它看起来很有希望,但我想看看是否可以获得对其包含项目的引用,类似于类型库。我认为ITypeInfo.GetContainingTypeLib可能有用,但它抛出一个异常,表明VBA不合作。有人对VBA如何做与标准COM规范不同的事情有什么想法吗 C#类库代码在这里。注册COM互操作,并在AssemblyInfo.cs中设置COMVisible(true),使其可从VBA访问。下面给出了VBA客户端代码 using System; using

因此,我尝试在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

        }
    }
}
在Class1可以是任何类的情况下,我有两个空函数,但我认为这并不相关


我已经问过了。

如果dll是com公开的,则必须向regsvr32注册


此外,您可能需要对.net程序集进行GAC处理,因为这些程序集是内部管理的,所以答案可能会非常多。你可以看看一般的方法。
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