Marshal.GetActiveObject() fails for windows services

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all,
 
    Sorry if this is in the wrong Forum section but I couldnt spot anything more suitable.
 
    I am currently developing a windows service (not web service) that needs to interface with a legacy system via a COM object that is located in the ROT (Running Object Table) in memory. If I run my windows service as a Console Application, Marshal.GetActiveObject() returns the COM object that I want, however if I run the same code as a Windows Service I get an Operation Not Available COMException (HRESULT: 0x800401E3) which means it cant find the object in memory.
 
   I thought it might be a security issue so Ive tried running my service in the Local System account (with access to the Desktop) and as an administrative user. Both times it failed to find the COM object.
 
  Ive also tried setting the SecurityPermissionSet so that it can access Unmanaged code as suggested in the MSDN documentation. But still no avail.
  <font size=2>
</font><font color="#0000ff" size=2>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
<font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#2b91af" size=2>CDSNotifier</font>
<font size=2>{
</font><font color="#0000ff" size=2>  private</font><font size=2> </font><font color="#0000ff" size=2>string</font><font size=2> ID = </font><font color="#a31515" size=2>"CDS"</font><font size=2>;
</font><font color="#0000ff" size=2>  private</font><font size=2> </font><font color="#0000ff" size=2>string</font><font size=2> CDS_HANDLE = </font><font color="#a31515" size=2>"BroadcastManager.Content"</font><font size=2>;
</font><font color="#0000ff" size=2>  public</font><font size=2> </font><font color="#0000ff" size=2>static</font><font size=2> CommonDataShare.</font><font color="#2b91af" size=2>clsMainClass</font><font size=2> CDS = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#0000ff" size=2>  public</font><font size=2> CDSNotifier()
  {
</font><font color="#0000ff" size=2>    try </font><font size=2>
    {
      System.Security.</font><font color="#2b91af" size=2>PermissionSet</font><font size=2> ps = </font><font color="#0000ff" size=2>new</font><font size=2> System.Security.</font><font color="#2b91af" size=2>PermissionSet</font><font size=2>(System.Security.Permissions.</font><font color="#2b91af" size=2>PermissionState</font><font size=2>.None);
      ps.AddPermission(</font><font color="#0000ff" size=2>new</font><font size=2> System.Security.Permissions.</font><font color="#2b91af" size=2>SecurityPermission</font><font size=2>(System.Security.Permissions.</font><font color="#2b91af" size=2>SecurityPermissionFlag</font><font size=2>.UnmanagedCode));
      ps.Assert();
</font><font color="#2b91af" size=2>      Object</font><font size=2> objCDS = </font><font color="#2b91af" size=2>Marshal</font><font size=2>.GetActiveObject(</font><font color="#a31515" size=2>"CommonDataShare.clsMain"</font><font size=2>);
      CDS = (CommonDataShare.</font><font color="#2b91af" size=2>clsMainClass</font><font size=2>)</font><font color="#2b91af" size=2>Marshal</font><font size=2>.CreateWrapperOfType(objCDS, </font><font color="#0000ff" size=2>typeof</font><font size=2>(CommonDataShare.</font><font color="#2b91af" size=2>clsMainClass</font><font size=2>));
    }
</font><font color="#0000ff" size=2>    catch</font><font size=2> (</font><font color="#2b91af" size=2>Exception</font><font size=2> e)
    {
</font><font color="#2b91af" size=2>      CriticalEvents</font><font size=2>.OnWarning(</font><font color="#0000ff" size=2>this</font><font size=2>, </font><font color="#a31515" size=2>"Failed to get CommonDataShare:"</font><font size=2> + e.Message, e);
      CDS = </font><font color="#0000ff" size=2>null</font><font size=2>;
    }

</font><font color="#0000ff" size=2>    if</font><font size=2> (CDS == </font><font color="#0000ff" size=2>null</font><font size=2>)
    {
      CDS = </font><font color="#0000ff" size=2>new</font><font size=2> CommonDataShare.</font><font color="#2b91af" size=2>clsMainClass</font><font size=2>();
    }
    CDS.__clsMain_Event_Message += </font><font color="#0000ff" size=2>new</font><font size=2> CommonDataShare.</font><font color="#2b91af" size=2>__clsMain_MessageEventHandler</font><font size=2>(onCDSMessage);
  }
} </font>
  </font>
Any help on how to resolve this would be much appreciated.
 
    Regards,
 
        Ben <font size=2>
  </font>

View the full article
 
Back
Top