Callback from Delphi DLL in C # - only works in winform standby mode or using app.doevents?

I give up, I have to write and ask; I use an unmanaged DLL written in Delphi, which asynchronously calls a callback (without parameters) that is passed to it whenever an event occurs in the equipment that it is designed for monitoring.

What I do in C #, I keep a static link to the created delegate, which is then passed as a parameter to the start method on the Delphi side. This tells the DLL to notify me using a callback whenever there is new data to retrieve using the GetData method.

Everything works fine, fine, until I try to do the same in a console application or in a Windows service. Or, if I create and call DLL-related methods in a separate thread, not allowing this thread to rotate the Application.DoEvents () application. Usually people with similar problems seem to have problems with the GC or conventions, but with (trying) to provide protection against problems with the GC, leaving the delegate ref I was left without any further hints on how to solve this.

I assume that I am missing something important in how the CLR calls my callbacks after the DLL calls thunk to connect to my callback handler.

The declarations of my DLL import are as follows:

    [DllImport("TheDelphiApi.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    private static extern UInt32 Start(MulticastDelegate callback);

    [DllImport("TheDelphiApi.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    private static extern void GetData(byte[] recdata);

    public delegate void EngineCallbackHandler();                     

    private static EngineCallbackHandler engineCallback;

And the call configuring the callback:

        UInt32 result = Start(engineCallback);

? DoEvents? , , - . /J

+1
1

, , , , , Start? TTimer ?

Start , 2 , DLL. - , .

EDIT:

:

DLL COM STA ( ), COM .

+1

Source: https://habr.com/ru/post/1682559/


All Articles