-->

How to access COM vtable and acess its entries fro

2019-09-14 01:51发布

问题:

I need to access COM vtable which have entries of those functions which are exposed to outside world under some specific interface in C#.

I've accessed and iterate over the types enclosed in the TLB with LoadTypeLib and playing with ITypeInfo.

Now only thing I need to access one by one those methods inside vtable of COM Interface and need to call them at RUNTIME one by one.

I need COM vtable address and its indexed entries (i.e function addresses).

回答1:

You can create a C# wrapper from the TLB using the TlmImp.exe program. See How to: Generate Primary Interop Assemblies Using Tlbimp.exe for more info.

e.g. tlbimp LibUtil.tlb /primary /keyfile:CompanyA.snk /out:LibUtil.dll

Edit: To reflect comment.

Since C# code is always running in the CLR any call to the unmanaged world will need to be marshalled correctly. You have the option of

(a) allowing the generated wrappers of just doing the interop, or

(b) you can optimise the wrappers your self.

(c) The other thing you can do is to write your oen wrapper in managed c++ and then call through the vtable directly. I suspect this will give you a very low call overhead.

(d) The final option is to just write a simple unmanaged c dll with a single function and use P/Invoke to call it directly from the managed code.