Integrating C# & C++ application using secured COM

2019-09-07 13:29发布

I'm trying to get my C#4.0 (VS2010) app to interact with a C++ app using COM and a referenced type library.

All the examples I've seen using third-party libraries mostly include ActiveX controls or dll's where you can simply add a reference and create objects. I'm unable to create any objects directly (I get an error saying to use the interface instead) and I'm unable to find anything that will return the interface.

This is a secure COM library for accessing an encrypted database and requires a passphrase when creating the instance.

I've spent a lot of time on this to know generally what needs to be done but also enough to realize that it is obscure enough that I'm not going to figure it out in a reasonable time period. The closest examples I've found are very abstract and undocumented. It might be useful for someone who understands COM and reflection and interop very well but is insufficient to use as a starting point with my experience.

I'm stuck at the point where I have a single method to call but I feel months away from being able to call it.


The C++ app was built in release mode using VS2005 and the .tlb was successfully added as a reference to my C# 2010 solution. The C++ application is installed on my development machine.

In the object browser I can see public class SecureRepositoryClass, member of SecurityAgentLib, with method GetPolicyValue(string,string,string) that I want to call. This implements ISecureRepository which defined GetPolicyValue.

I know something is supposed to return a reference I can cast to ISecureRepository and then call the method I need.

What I can't figure out how to do is to get a reference to this. Our C++ project (written by someone else, I'm not a C++ developer unfortunately) uses the following code (with variables that are set in another context)

   HRESULT hr = CoGetClassObject(CLSID_SecureRuntimeAgent, CLSCTX_LOCAL_SERVER, NULL, IID_IClassFactory2, (void**) &pClf);

   if ((SUCCEEDED(hr)))
   {
         CComBSTR bstrKey = (LPCTSTR)ProduceKey();
         hr = pClf->CreateInstanceLic(NULL, NULL, IID_ISecureRuntimeAgent, bstrKey, (void**)&m_pIAPLI);
         if ((!SUCCEEDED(hr)))

I've searched for CreateInstanceLic, CoGetClassObject, and have been trying to find C# equivalents.

I've tried several examples but without really understanding what they mean it is hard to adapt to this particular situation.

I know what I have, and what I need, but not how to get there.

1条回答
女痞
2楼-- · 2019-09-07 14:08

It does not appear that the IClassFactory2 COM interface is implemented in the standard run time callable wrapper, so it looks like you are going to have to do it the brute force way and call the unmanged api in Ole32.dll from your managed code. There is an example of doing that in C# here http://www.dotnet247.com/247reference/msgs/28/144657.aspx . I am hesitant to copy that code directly here since no copyright terms are specified for that code.

查看更多
登录 后发表回答