0

I am using a third party COM component to communicate with hardware device in my .net 6 console application. Initially when I started implementing the functionality I was getting following error

Unable to cast COM object of type 'System.__ComObject' to interface type 'DrvFRLib.DrvFR'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A9BFA98D-04ED-4B1E-A874-4C6C3A91930E}' failed due to the following error: Unspecified error (0x80004005 (E_FAIL)).

and stack trace

at System.Runtime.CompilerServices.CastHelpers.ChkCastAny(Void* toTypeHnd, Object obj) at Gizmo.PointOfService.Devices.FiscalPrinters.FiscalPrinterDrivers.ShtrihFiscalPrinterDriver.d__5.MoveNext() in D:\My Documents\Visual Studio 2015\Projects\Gizmo\Gizmo.PointOfService\Gizmo.PointOfService\Devices\FiscalPrinters\FiscalPrinterDrivers\ShtrihFiscalPrinterDriver.cs:line 30 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Gizmo.PointOfService.Devices.FiscalPrinters.OFDFiscalPrinter.d__11.MoveNext() in D:\My Documents\Visual Studio 2015\Projects\Gizmo\Gizmo.PointOfService\Gizmo.PointOfService\Devices\FiscalPrinters\OFDFiscalPrinter.cs:line 101

After converting my application into a WPF app and basically hosting WPF app in my application like this

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{        
    protected override async void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        //run the app
        await Program.Main(e.Args);            
    }
}

the error was gone and I was able to insatiate the COM component. Now the initial intention was to have my application running as windows service but once I tried to run the application as service the same exception would appear. Now I cant be sure but it looks like this COM component has some dependencies on GUI (WndProc) functionality ? So the question is whether anybody have experienced similar problems and perhaps have any ideas on potential workarounds?

NullReference
  • 862
  • 1
  • 11
  • 27
  • 1
    Running as a real Windows service is completely different than a normal Windows app. It's also different from running as a simple Console application. Does it work with a simple Console application? Have you tried to add an `[STAThread]` attribute to the main method (see https://stackoverflow.com/questions/1361033/what-does-stathread-do)? Otherwise you should contact the vendor and see if what you do is supported or not. – Simon Mourier Apr 21 '22 at 06:16
  • You are right , it had to do with STA thread. Thanks! – NullReference Apr 21 '22 at 10:48

0 Answers0