A
A
Alexey Larionov2014-09-18 15:59:59
C++ / C#
Alexey Larionov, 2014-09-18 15:59:59

Using COM Objects in Windows Service C#?

Using OleWoo, I get the uuid of the COM object and the DispID of its properties and methods from a tlb file. Created a wrapper (dll-library).

public class Broker : IDisposable
  {
        #region Переменные класса
        /// <summary>
        /// Тип приложения 
        /// </summary>
        private Type _sapBrokerType;
        /// <summary>
        /// Объект приложения
        /// </summary>
        private object _sapBrokerObj;
        /// <summary>
        /// Освобождены ли используеммые ресурсы
        /// </summary>
        private bool _disposed = false;


public Broker()
        {
            _sapBrokerType = Type.GetTypeFromCLSID(new Guid("{b9df38f7-5340-4512-bae6-4ef7465a3c17}"));
            _sapBrokerObj = _sapBrokerType.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
                BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
        }

I connect this DLL as a reference to a project like Windows Form App, everything works. The COM object is created, all methods work.
var broker = new Broker();
I push the same code into a Windows Service type project, it crashes on creating a COM object:
COMException was caught

Failed to get the COM class factory for the component with CLSID {B9DF38F7-5340-4512-BAE6-4EF7465A3C17} due to the following error: 80080005 running the server application (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2014-09-18
@lari4

Most likely, the user under which the service is running does not have rights to create an object.
If it is DCOM, then the rights can be set in the "Component Services" applet, "Administration" section of the Toolbar.
It may happen that the com object being created requires a GUI. For example, it uses a message queue, displays dialogs. In this case, a service that is not running as a system user with the "Allow desktop interaction" checkbox checked will most likely not work as a normal application does.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question