# Clarion 11 call C# dll will show Exception screen

**URL:** <https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157>\
**Category:** questions\
**Tags:** interop, dotnet\
**Created:** [April 18, 2023, 3:00am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157 "2023-04-18T03:00:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![skyong38](https://clarionhub.com/user_avatar/clarionhub.com/skyong38/32/4042_2.png) [@skyong38](https://clarionhub.com/u/skyong38)\
**Post date:** [April 18, 2023, 3:00am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/1 "2023-04-18T03:00:33Z")

</div>

I am using Clarion 11 to call C# dll, once invoke C# function, it will appear Exception Screen.

 ![image](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/7/78d80f2af66f44cc1bae7cd12a567f5566c32008.png)

Any idea of this issue? I use MGLibMkr.exe to generate lib.

Thank you.

Regards,  
Nelson

---

<div class="post-metadata">

**Author:** ![Mike\_Duglas](https://clarionhub.com/user_avatar/clarionhub.com/mike_duglas/32/1061_2.png) [@Mike\_Duglas](https://clarionhub.com/u/Mike_Duglas)\
**Post date:** [April 18, 2023, 7:14am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/2 "2023-04-18T07:14:40Z")

</div>

Clarioners are the only people on Earth who don’t know what a debugger is for.

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 18, 2023, 12:00pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/3 "2023-04-18T12:00:06Z")

</div>

What is C# function your program tries to call?  
How it is prototyped on the C# and Clarion sides?  
What are actual parameters passed to the call and their types?

---

<div class="post-metadata">

**Author:** ![Bostjan\_Laba](https://clarionhub.com/user_avatar/clarionhub.com/bostjan_laba/32/2498_2.png) [@Bostjan\_Laba](https://clarionhub.com/u/Bostjan_Laba)\
**Post date:** [April 19, 2023, 6:21pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/4 "2023-04-19T18:21:41Z")

</div>

More like only ones not really knowing how to use it. Myself included, tbh.

---

<div class="post-metadata">

**Author:** ![MarkGoldberg](https://clarionhub.com/user_avatar/clarionhub.com/markgoldberg/32/1511_2.png) [@MarkGoldberg](https://clarionhub.com/u/MarkGoldberg)\
**Post date:** [April 19, 2023, 8:08pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/5 "2023-04-19T20:08:42Z")

</div>

We’d like to be able to help you  
But we need so much more information.  
Some of the first thoughts that come to mind are:

1. What does the call look like on the clarion side?
2. [How to improve the Call Stack when your program GPFs to show Procedure Names - ide / Tips - ClarionHub](https://clarionhub.com/t/how-to-improve-the-call-stack-when-your-program-gpfs-to-show-procedure-names/188)
3. Are there parameters
4. Is the call wrapped in a class  
a) if so are there LINK/MODULE attrubutes?  
i) do you have your pragma’s set  
ii) is the include for the class at a global (or module) scope ?
5. is the .NET program configured for x86 ?
6. when exactly does the crash occur?  
a) as you make the call  
b) upon return from the call

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [April 19, 2023, 9:19pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/6 "2023-04-19T21:19:03Z")

</div>

Also, if you have the source for the C# DLL, you can open the running Clarion process in VS and Debug the C# DLL, if that’s where the crash occurs.

---

<div class="post-metadata">

**Author:** ![skyong38](https://clarionhub.com/user_avatar/clarionhub.com/skyong38/32/4042_2.png) [@skyong38](https://clarionhub.com/u/skyong38)\
**Post date:** [April 20, 2023, 2:36am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/7 "2023-04-20T02:36:30Z")

</div>

Thanks all for helping.

This is simple C# DLL which configured for x86. I not passing any parameter over, because Clarion don’t have DateTime.Now.Ticks function, so I let C# generate it and pass back to me.

I use MGLibMkr to generate Lib and added to my App Library Object. GenerateRandomString() is the C# function name.

I just code → LocRandomStr = GenerateRandomString(), and it able return me correct value, and finish all the code after it without GPF, GPF occurred when finished Embed point coding.

If I set Debug Info → Full. no GPF will appear.

Thank you.

---

<div class="post-metadata">

**Author:** ![vitesse](https://clarionhub.com/user_avatar/clarionhub.com/vitesse/32/837_2.png) [@vitesse](https://clarionhub.com/u/vitesse)\
**Post date:** [April 20, 2023, 2:58am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/8 "2023-04-20T02:58:07Z")

</div>

> [@skyong38](#):
>
> GenerateRandomString() is the C# function name.

Nelson - not directly answering your gpf problem but is there any reason you are using a C# dll to do this when you could just use Clarion code to generate a random string?

---

<div class="post-metadata">

**Author:** ![skyong38](https://clarionhub.com/user_avatar/clarionhub.com/skyong38/32/4042_2.png) [@skyong38](https://clarionhub.com/u/skyong38)\
**Post date:** [April 20, 2023, 3:34am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/9 "2023-04-20T03:34:28Z")

</div>

Clarion got C# “DateTime.Now.Ticks” function?

My vendor using a lot C# function, I use this as testing purpose.

TQ

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [April 20, 2023, 12:12pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/10 "2023-04-20T12:12:19Z")

</div>

> [@skyong38](#):
>
> I just code → LocRandomStr = GenerateRandomString(), and it able return me correct value, and finish all the code after it without GPF, GPF occurred when finished Embed point coding.

Show your `Code` how you declared `GenerateRandomString()` in Clarion?

And the C# `Code`?

You’re missing something (like PASCAL) or have something wrong (like C). We cannot help you without some `Code`.

> [@skyong38](#):
>
> DateTime.Now.Ticks function, so I let C# generate it and pass back to me

The way I read it that returns a 64 bit INT. Clarion is 32 bit so you’ll be losing 32 bits.

You may be able to code it as a REAL return in Clarion which is 64 bits. Then put an Int64 Over that Real. Better would be to code it on both sides as `(*Long Hi64, *LONG Lo64)` and no return.

In Clarion you write Code. Here we help you fix your Code. You MUST POST YOUR CODE for us to help.

---

<div class="post-metadata">

**Author:** ![PaulAttryde](https://clarionhub.com/user_avatar/clarionhub.com/paulattryde/32/3519_2.png) [@PaulAttryde](https://clarionhub.com/u/PaulAttryde)\
**Post date:** [April 20, 2023, 12:44pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/11 "2023-04-20T12:44:51Z")

</div>

Why do you need to use DateTime.Now.Ticks in the first place? Depending on what you’re trying to do there might be a Clarion solution and you don’t need c# at all.

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 20, 2023, 12:46pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/12 "2023-04-20T12:46:30Z")

</div>

> [@skyong38](#):
>
> This is simple C# DLL which configured for x86. I not passing any parameter over, because Clarion don’t have DateTime.Now.Ticks function, so I let C# generate it and pass back to me.

DateTime.Now in C# is nothing else as value returned from the call to GetSystemTimeAsFileTime Windows API function converted to “ticks”. One “tick” is a 1/10000000 of second. If you don’t need in nano-second resolution (which is calculated really, actual resolution is dependent from Windows version and hardware and counting in milliseconds), you can use API functions like GetSystemTime or GetSystemTimeAsFileTime. API functions QueryPerformanceCounter and QueryPerformanceFrequency can be used for calculations of time elapses with he most possible actual resolution.

> [@skyong38](#):
>
> I just code → LocRandomStr = GenerateRandomString(), and it able return me correct value, and finish all the code after it without GPF

What is Clarion prototype of GenerateRandomString? What is type of LocRandomStr?  
Strings in managed code are Unicode. It’s need to use Marshal.StringToBSTR to get the pointer to an Unicode strings usable in native code. Then, Unicode string pointed by the pointer returned from Marshal.StringToBSTR can be converted to an ANSI string.

> [@skyong38](#):
>
> GPF occurred when finished Embed point coding.

I guess, you had in mind that the described code was coded in some embed point and the exception occurred on the exit from this code. Am I right? For me, the most probable reasons for the exception are:

- memory overrun on the “random” string storing
- incorrect invalidation of some object on the C# side.

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 20, 2023, 12:59pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/13 "2023-04-20T12:59:08Z")

</div>

If somebody interested, here is the implementation of DateTime.Now from .Net reference sources:

```auto
        // Returns a DateTime representing the current date and time. The 
        // resolution of the returned value depends on the system timer. For
        // Windows NT 3.5 and later the timer resolution is approximately 10ms, 
        // for Windows NT 3.1 it is approximately 16ms, and for Windows 95 and 98 
        // it is approximately 55ms.
        // 
        public static DateTime Now {
            get {
                return UtcNow.ToLocalTime();
            } 
        }
 
        public static DateTime UtcNow { 
            get {
                // following code is tuned for speed. Don't change it without running benchmark. 
                long ticks = 0;
                ticks = GetSystemTimeAsFileTime();
                return new DateTime( ((UInt64)(ticks + FileTimeOffset)) | KindUtc);
            } 
        }

```

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [April 20, 2023, 1:08pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/14 "2023-04-20T13:08:08Z")

</div>

> [@PaulAttryde](#):
>
> Why do you need to use DateTime.Now.Ticks in the first place?

Think it’s just to try using C#… He said:

> My vendor using a lot C# function, I use this as testing purpose.

---

<div class="post-metadata">

**Author:** ![FedericoNavarro](https://clarionhub.com/letter_avatar_proxy/v4/letter/f/2acd7d/32.png) [@FedericoNavarro](https://clarionhub.com/u/FedericoNavarro)\
**Post date:** [April 20, 2023, 1:23pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/15 "2023-04-20T13:23:29Z")

</div>

> [@anon77170705](#):
>
> For me, the most probable reasons for the exception are:
> 
> - memory overrun on the “random” string storing
> - incorrect invalidation of some object on the C# side.

I think it may be caused by uncoordinated registers’ Push and Pop caused by a bad prototype definition of the function called, leaving parameters leaked on the stack or the opposite double cleaning them. As by his description the problem arise at Clarion function return and only in release mode, not in debug mode, which generates code relying on base pointer.

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 20, 2023, 1:32pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/16 "2023-04-20T13:32:53Z")

</div>

> [@FedericoNavarro](#):
>
> I think it may be caused by uncoordinated registers’ Push and Pop caused by a bad prototype definition of the function called,

This is possible but if to take into account the call stack shown in the posted exception message (exception is inside code external for the program) and the information that exception was at exit from the test code, incorrect prototypes are unlikely the reason. Though, some chances exist.

---

<div class="post-metadata">

**Author:** ![anon51095673](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@anon51095673](https://clarionhub.com/u/anon51095673)\
**Post date:** [April 20, 2023, 10:37pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/17 "2023-04-20T22:37:14Z")

</div>

Always though the DOT NET DLL and win 32 were not compatible.

> **[UnmanagedExports 1.2.7](https://www.nuget.org/packages/UnmanagedExports)**
>
> A set of compile-time libraries (nothing to deploy) and a build task that enable you to export functions from managed code to native applications.
> That means, you can create plugins in a managed language like C# or F# for native applications...

---

<div class="post-metadata">

**Author:** ![Oggy](https://clarionhub.com/user_avatar/clarionhub.com/oggy/32/397_2.png) [@Oggy](https://clarionhub.com/u/Oggy)\
**Post date:** [April 21, 2023, 8:17pm UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/18 "2023-04-21T20:17:44Z")

</div>

If you are using C# code, its mandatory, in c# code to catch exception inside c# code or you get exactly your gpf… As always, if some error occured in c# code, and you do not try to catch it, this appear…  
So, examine a little try and catch…

---

<div class="post-metadata">

**Author:** ![skyong38](https://clarionhub.com/user_avatar/clarionhub.com/skyong38/32/4042_2.png) [@skyong38](https://clarionhub.com/u/skyong38)\
**Post date:** [April 25, 2023, 1:42am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/19 "2023-04-25T01:42:14Z")

</div>

My Clarion app need to integrate with Vendor payment device, he provide API code in C#.

This is one sample :-

using System;  
using System.Text;

namespace RevenueMonsterOpenAPI.Util  
{  
internal class RandomString  
{  
//private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden  
private static readonly Random Random = new Random((int)DateTime.Now.Ticks);  
public static string GenerateRandomString(int size)  
{  
var builder = new StringBuilder();  
for (var i = 0; i \< size; i++)  
{  
var ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 \* Random.NextDouble() + 65)));  
builder.Append(ch);  
}

```
        return builder.ToString();
    }
}

```

}

There are a lot function in C# API they provided need integrate with. So, I decide compiled it in C# dll and clarion call will be simple way.

Thank you for all reply.

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 25, 2023, 6:46am UTC](https://clarionhub.com/t/clarion-11-call-c-dll-will-show-exception-screen/6157/20 "2023-04-25T06:46:29Z")

</div>

Not-managed code can’t use managed strings directly. Use Marshal.StringToBSTR for conversion.
