none
Failed to create COM Interop object RRS feed

  • Question

  • This issue could not be duplicated by 100%, but it exists in a customer's environment. This case will happen one time during a day.

    Please see below details:

    OS: Windows 2012,  64-bit

    1) C# Interop.cs

    [ComImport, Guid ("xxxxxxxx- xxxx-xxxx-xxxx-xxxxxxxxxxxx"), TypeLibType(0x1040)] 
    public interface IMyAgent
    {
    [MethodImpl(MethodImplOptions.InternalCall,MethodCodeType=MethodCodeType.Runtime),DisplId(0x12)]
    void RunTask(MarshalAs(UnmanagedType.Bstr)] string bstrProjName, ushort wSchID);
    }

    [ComImport, Guid ("xxxxxxxx- xxxx-xxxx-xxxx-xxxxxxxxxxxx"), CoClass(Typeof(ServiceClass))] 
    public interface Service: IMyAgent
    {//Blank
    }

    [ComImport, Guid ("xxxxxxxx- xxxx-xxxx-xxxx-xxxxxxxxxxxx"), TypeLibType(2), ClassInterface((short)0)] 
    public class ServiceClass: IMyAgent, Service
    {
    [MethodImpl(MethodImplOptions.InternalCall,MethodCodeType=MethodCodeType.Runtime),DisplId(0x12)]
    void RunTask(MarshalAs(UnmanagedType.Bstr)] string bstrProjName, ushort wSchID);
    }

    2) C# code

    private ServiceClass mService;

    this.mService = new ServiceClass(); //it will hang here.

    3) C++ Code,

    Service.cpp:

    extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int)
    {
    BOOL bEXE = false;  
    ...
    }

    Seems that it has not stepped into "WINAPI _tWinMain". This object is a windows service, its status is "Starting", and its process can not be found from task manager.

    This issue does not happen every time. 

    *.idl file:
    interface IMyAgent: IDispatch
    {
    //the methods definition
    }

    coclass Service
    {
    [default] interface IMyAgent
    }

    My questions:
    1) How to resolve this issue for the customer?

    2) I thought a workaround, but it does not work.
        Create a new thread with timeout setting to create the COM Interop object. If timeout, abort this new thread. However, it seems that it hangs at thread.Abort()

    Thanks Very much!

    Scott



    • Edited by ScottQ Monday, June 26, 2017 2:21 AM
    Monday, June 26, 2017 2:19 AM

Answers

All replies

  • Hi ScottQ,

    Thank you for posting here.

    >>Seems that it has not stepped into "WINAPI _tWinMain". This object is a windows service, its status is "Starting", and its process can not be found from task manager.

    Maybe it caused by the Windows service is opening and you open again.

    You could use  Attach to Process  to trace that  windows service open or not.

    For more details, please refer to the MSDN article.

    https://msdn.microsoft.com/en-us/library/0bxe8ytt.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 27, 2017 10:02 AM
    Moderator
  • Hello Wendy,

    Thanks for your reply.

    Before start this windows service (new ServiceClass()), the current status of this service is "Stopped".

    Our production is to start this windows service by scheduling (such as every 1 hour). Before start it, check the service status.

    Thanks

    Scott

    Wednesday, June 28, 2017 12:19 AM
  • Hi ScottQ,

    For your question, I need more information.

    First, what is the error message?

    1. The title of your question "Failed to create COM Interop object" is the error message? If it is, where you get the message?
    2. If the title is not the error message, could you provide error message? In C# code when it hangs, what is the exception? What do you mean, it seems not stepped in to "WINAPI _tWinMain"?

    Second, where you get the starting status?

    1. In my view, I could get the status "Running" of service in task manager when the service runs.
    2. What is the "starting" status mean in your side? Where you get the status? In C++?

    We are waiting for your update.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 28, 2017 10:41 AM
    Moderator
  • Hi Wendy,

    1. Our application allows customer to scheduler certain works.
    App1 (C#) will starts App2 (C++) which is a windows service for every one hour. It is App2 (the windows service) to complete the customer's backup work.

    2. The current issue is that App1 fails to start App2. (could not be duplicated by 100%)
    Based on investigation on the trace logs from customer, I found it hanged at below step in App1 side.

    this.mService = new ServiceClass(); //it will hang here.

    There are no any error messages or exceptions.

    Before call new ServiceClass() in App 1 side, using System.ServiceProcecss.ServiceController.Status to check App 2's status. When hang occurs, the current status of App2 is "Starting" (check its status from Windows Services MMC), but its process cannot be listed in Task Manager.

    3. If it is successful to new ServiceClass(), there will be a trace log from WINAPI _tWinMain (App2). So I think it failed to step into WINAP_tWinMain after call new ServerClass() in App1 side.

    I really appreciate your help and support.

    Scott



    • Edited by ScottQ Thursday, June 29, 2017 12:43 AM
    Thursday, June 29, 2017 12:36 AM
  • I searched below link: Release C # object that is COM interop

    The issue in above link occurs every time, and can catch the exception. But it is similar to my issue.

    In my App 1,  C# Class, Dispose() method:
    - Stop App 2 (windows service) - use System.ServiceProcess.ServiceController.Stop()

    - Release the COM object - System.Runtime.InteropServices.Marshal.ReleaseComObject(this.mService);
      And It has released the COM object successfully!

    - Set the COM object to null - this.mService = null;

    - GC.SuppressFinalize(this);
      In above link, the suggestion is GC.Collect();GC.WaitForPendingFinalizers();


    • Edited by ScottQ Thursday, June 29, 2017 5:00 AM
    Thursday, June 29, 2017 2:31 AM
  • Hi ScottQ,

    For the System.ServiceProcecss.ServiceController.Status method, there are seven status, ContinuePending, Paused, PausePending, Running, StartPending, Stopped, StopPending. There is no status of "Starting".

    For more details about ServiceControllerStatus, please refer to the MSDN article. 

    https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontrollerstatus(v=vs.110).aspx

    Could you check the status when it is successful to new SericeClass()? Does it shows running? Could you get it in Task Manager?

    If it is not the same status "starting" when you are successful to new ServiceClass(), you could make some judgement. When you get the "starting" status, you could stop the service and then restart.

    For stop the service, please refer to the MSDN article.

    https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.stop(v=vs.110).aspx

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 3, 2017 6:16 AM
    Moderator
  • Thanks for your reminder, Wendy!

    I just have added some trace logs to check the service's status and if its process exists when hang happens.
    Now wait for feedback from the customer.
    Previous feedback, the customer said the service status is "starting" from Windows service (MMC), and its process cannot be found from task manager.

    Now I add new traces to confirm this again using System.ServiceProcecss.ServiceController.Status. When to new SerivceClass successful, its status has "Running", "Stopped", "StopPending" from my trace logs. 

    Thank you again.

    Tuesday, July 4, 2017 2:51 AM
  • Hi ScottQ,

    We are waiting for your update. If you have something new, please feel free to let me know.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 4, 2017 7:55 AM
    Moderator
  • Hi Wendy,

    When hang happened, the status of service was "StartPending" and its process could not be found at this moment (using the method: Process.GetProcessByName).

    Can we stop/kill this service when its status hangs at "StartPending" via C# application?

    Do the below codes work to stop "StartPending"?

    if  ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
         (sc.Status.Equals(ServiceControllerStatus.StopPending)))
    {
       // Start the service if the current status is stopped.
       Console.WriteLine("Starting the Telnet service...");
       sc.Start();
    }  
    else
    {
       // Stop the service if its status is not set to "Stopped".
       Console.WriteLine("Stopping the Telnet service...");
       sc.Stop();
    }  

    Thanks

    Scott

    • Edited by ScottQ Tuesday, July 11, 2017 12:59 AM
    Monday, July 10, 2017 11:52 PM
  • Hi ScottQ,

    Yes, you could use the ServiceController.Stop Method () to stop this service and use 

    ServiceController.Start Method to start the service.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 11, 2017 5:59 AM
    Moderator
  • I found there is a ServiceBase.CanStop Property. The customer issue is that the service got stuck in "StartPending". I am worried that the service can not be stopped at this case. (The value of CanStop is false) 

    If possible, I will try to use ServiceController.Stop method.

    Thanks

    Tuesday, July 11, 2017 6:39 AM
  • Hi ScottQ,

    For this property, you get the property false, but you could set it to true to pass the stop command to service as well.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 12, 2017 6:52 AM
    Moderator
  • I found this property CanStop is read-only.

    [ServiceProcessDescriptionAttribute("SPCanStop")]
    public bool CanStop { get; }

    Are there any other ideas? Any suggestions, comments are all welcome.

    Thanks Again!


    • Edited by ScottQ Wednesday, July 12, 2017 11:30 PM
    Wednesday, July 12, 2017 11:30 PM
  • Hi ScottQ,

    To further help you about this issue, I am trying to invoke someone experienced to help look into this thread, this may take some time and as soon as we get any result, we will post back.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 14, 2017 7:53 AM
    Moderator
  • Hi Wendy,

    I really appreciate your help. Now, I try to stop the service using ServiceController.Stop () method for several times, and wait for feedback from the customer.

    When the service got stuck in "StartPending", the customer has to restart his computer. Are there any workarounds which can recover the service (stop or kill it, but its process can not be found in Task Manager at this moment), and it does not need to restart Computer. This customer is very important for our product.  Thanks again for your help.

    Scott 

    • Edited by ScottQ Monday, July 17, 2017 1:39 AM
    Monday, July 17, 2017 1:36 AM
  • The feedback from customer shows it failed to stop the service using ServiceController.Stop().

    The error is "Cannot stop 'ServiceName' service on computer '.' ". The current status of it is "StartPending", its process does not exist, The value of ServiceController.CanStop is False.

    Monday, July 17, 2017 11:03 PM

  • With the help from Microsoft, this issue has been resolved, see below details:
    Reason: DNS client API call fails and could lead to service restart freeze in Windows Server 2012 R2 or Windows Server 2012

    Resolution: install the following hotfix on the affected servers to fix this issue. 
    https://support.microsoft.com/en-us/help/3134242/dns-client-api-call-fails-and-could-lead-to-service-restart-freeze-in

    Thanks
    Scott


    • Edited by ScottQ Sunday, September 3, 2017 11:49 PM
    • Marked as answer by ScottQ Sunday, September 3, 2017 11:49 PM
    Sunday, September 3, 2017 11:48 PM