0

I have a native dll that I wrapped in a dotnet6 libary. I am successfully loading my wrapper, and calling against that library using a simple dotnet6 WinForms app. I can see it is working and responding well. The native dll has it's own internal state machine, managing communication with a third party piece of hardware (via a COM port, supplied during loading).

When I create an instance of my wrapper in WinForms, everything works as expected. I have an event on the wrapper to let me know when it has completed a lengthy (~5 seconds) initialisation with the hardware it is talking to.

When I try to replicate that with a console application, it never makes the connection. I am logging timer ticks in the wrapper every second while it is trying to connect, but it doesn't ever connect. I am wondering if this is something to do with the way a WinForms app manages it's own objects that allows the native lib to execute in a way the console doesn't?

I have tried moving the creation and call into a background thread in the console app, but this has the same behaviour. Does anyone have any ideas on this?

Mark Lazarides
  • 366
  • 2
  • 13
  • 1
    https://stackoverflow.com/a/21684059/17034 – Hans Passant Apr 11 '22 at 14:05
  • thanks @HansPassant, but I cannot use that name space - this is destined for a windows service. I will see if I can swap out Application for Dispatcher, and manage it that way. – Mark Lazarides Apr 11 '22 at 15:42
  • 1
    Using it in a service is not a problem. – Hans Passant Apr 11 '22 at 17:06
  • @HansPassant, I cannot believe I have to ask this.... but I cannot add the appropriate using statement. I'm using dotnet 6 in my wrapper class. It won't add System.Windows.Forms or the newer Microsoft.WindowsDesktop.App.Forms framework package. ANy ideas? – Mark Lazarides Apr 11 '22 at 20:17
  • 1
    [UseWindowsForms](https://stackoverflow.com/questions/57509951/use-windows-forms-in-a-net-core-class-library-net-core-control-library) – Hans Passant Apr 11 '22 at 20:24

1 Answers1

0

I would suspect two thing.

  • Thread
    If the Wrapper is called from UI thread then that may be a reason for the Wrapper to work.
  • Wrapper usage
    Maybe there is an initialization method called in WinForms, and not called in Commandline ?
MikolajR
  • 81
  • 4
  • It isn't hanging the UI thread - I suspect (but of course, don't know!) that the native lib is managing it's own thread once I call initialise on it. I have tried making the instance static in the console app to ensure it is on the heap (on the basis that something might be going on with the native lib falling out of scope), but that made no difference either. I am wondering if it is something to do with access to the COM port. – Mark Lazarides Apr 11 '22 at 14:09