Clarion calling C# DLL

I have a C# source compiled as DLL. Now this dll occasionally does some downloading, long story short, it can respond to clarion call in a second or in 10 seconds. When it’s a second, my app works fine, when it should be 10, it just crashes (closes down).

This is the C# code I’m calling from clarion:

        [DllExport("QRVerify", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.BStr)]        
        public static string StringVerify([MarshalAs(UnmanagedType.BStr)] string QrCode, [MarshalAs(UnmanagedType.BStr)] string TrustListUri, [MarshalAs(UnmanagedType.BStr)] string ValueSetUri, [MarshalAs(UnmanagedType.BStr)] string RuleSetUri)
        {
             
            var VR = Verify(QrCode, TrustListUri, ValueSetUri, RuleSetUri);
            return Newtonsoft.Json.JsonConvert.SerializeObject(VR);
        }

So, it’s a bunch of async code behind in this c# project.

My prototype:

> module('eudcc-verifier.dll')
>     QRVerify(bstring,bstring,bstring,bstring), bstring, name('QRVerify'), pascal,raw,dll(true)
> end

My code:

qrcode='123'  
            qrcodebstr=clip(qrcode)
            trustlistbstr='some https'
            ValueSetBstr='some https'
            RuleSetbstr='some https'
            retstr=QRVerify(qrcodebstr,trustlistbstr,ValueSetBstr,RuleSetbstr)     

So, how do I solve this? If needed I can modify c#, I have full source, but I’m not a c# programmer. I’m not sure why CW can’t just wait (ok, it would appear hanged for those 10 secs, I know). Or what could be the problem?

Thank you.

Hi @Bostjan_Laba

I would be tempted to have a standard sync method that clarion calls, and from that method call the Task method.

It’s not something I have tried. More details about how to achieve this can be found here: async await - How to call asynchronous method from synchronous method in C#? - Stack Overflow although that post is a few years old.

Mark

Mark, I’ve changed the code to a normal static procedure which calls other async ones. Because if I called async from CW, it crashed all the time. So it’s the edited code now (in my previous code). This is now as described - works for short times, crashed on longer.

I’m not sure why the execution time of the DLL would matter to CW. I would expect it to just wait until DLL finishes and reports back…

It’s the c# code. There are some exception points that do not return value when they should. Not a CW issue I think.

Well, seems like I will have to somehow deal with async returns from the dll. If anyone has experience on that, I’d appreciate it.

I prefer to call Clarion callback function after async method is completed:

// execute js script and get a result
string result = await webView.ExecuteScriptAsync(javaScript);

// pass script result to Carion
dScriptResult?.Invoke(claInst, scriptName, result);