Generating VpnApi COM object instances within a Windows Service

The problem arises from the actions of COM objects that are loaded in memory. Nonetheless, I believe that a viable fix could be to create a proxy class that manages access to the VpnApi COM object. This class could include a scheduler that funnels requests to a single thread or a singleton class to address the issue.


Utilizing the Cisco AnyConnect VpnApi, my service references a COM ‘Manifest?’ dll that I created. I attempted an alternate approach by simply adding a reference to the COM service and allowing VS to embed the interop assembly. Running under the System account, the service executes tasks in the background while awaiting a custom command, which triggers the ServiceBase.OnCustomCommand function.


using VpnApiLib;

The code is pretty simple:

 IVpnApi vpn = null;
     vpn = new VpnApi();
     return true;
 catch (Exception ex)
 finally {
     if (vpn!= null)
 return false;

Nonetheless, upon invoking it from the service, an
is received.

attempted to read or write protected memory
indicates memory corruption, it is usually a sign of other memory being affected as well.

My Stack Trace:

at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at ...ConnectivityAction.IsVpnInstalled() in D:...ConnectivityAction.cs:line 208

I encountered a distinct error when the operation failed for ”
retrieving the com class factory for component with clsid
{C15C0F4F-DDFB-4591-AD53-C9A71C9C15C0}” with the error code 800701e7.

I attempted to reference the COM object by incorporating the code in a
Console app
and it resolved smoothly without any exceptions.

Despite attempting all potential solutions, including the use of tried and functional pinvoke code for impersonation, I have been unable to resolve the issue. Each time the windows service calls it, one of various exceptions occurs.

Solution 1:

This forum post implies that the errors encountered could be due to problems with the links.

A discussion on the Cisco support forum addresses a problem with the C# VPNApiclass, which results in a Com Exception 800701e7 that persists until the system is rebooted.

Solution 2:

After struggling for three days, I have finally managed to solve the problem.

The issue arose from the fact that the COM object was referenced in the ‘bootstrapper’ class of the application (which I inherited), and was indirectly loaded through Unity using an assembly search pattern. This search pattern made it difficult to identify the problem.

The problem arises from how COM objects are handled in memory. It appears that when accessed through the surrogate created by tlbimp.exe, the COM object is loaded into a specific memory location. This location is then saved in the surrogate when it is created. As a result, simply referencing the COM object in the bootstrapper and loading it through
unity created
causes it to be instantiated in memory on the main thread.

The OnCustomCommand event was firing on a separate thread that lacked access to the memory where the COM object was loaded by the main thread, resulting in the ”
protected memory
” error.

Although the referencing class was inactive, I managed to get everything working by commenting it out. Nevertheless, I believe that a proxy class could be the ultimate answer to this issue. This class would handle all accesses to the VpnApi COM object, possibly with a scheduler that directs to a single thread or a singleton class.

At the moment, I haven’t dedicated much time to thinking about it. However, once I come to a conclusion, I’ll confidently wrap it up and head home. Any recommendations from you all will be highly appreciated and rewarded with my up-votes.

Frequently Asked Questions

Posted in Uncategorized