Some help with C# interop

I received this message through the contact form at ClarionAddins the other day. Unfortunatly the lovely helpful person didn’t leave any contact detail. In case it is helpful to anyone I thought I might share it here.

Please do get in touch if this was you!

In a recent project I used the Unmanaged Exports nuget package to make a c# dll compatible with Clarion. The project I was working on was a multi-dll project with 8 or 9 Clarion dlls and I was adding the C# dll to this environment. I don’t know the specifics of why this happened but it was throwing error when trying to call a function on the C# dll from a Clarion dll, 0xc000005 Access Violation Error. To fix this you had to use the Windows API LoadLibrary function in the beginning of the program and/or call a c# function in the executable of the project prior to the dlls calling any of the c# functions.

Hope this helps incase anyone runs into the same issue I did.

When I do interop with using the unmanaged exports technique I use the LoadLib class on the clarion side. I am not sure where to get this anymore, perhaps @julesVerne can help with that?

In case you are curious here is an answer I put on StackOverflow about Clarion callbacks and Unmanaged Export interop:

Brahn, the Load Lib class is from Larry Sand.
I’m not sure if he has a download link for it, but I’m sure we can check with him.

Thanks @Rick_UpperPark. I seem to remember getting it from ClarionMag but I cannot locate it anymore. Hopefully Larry (a.k.a. @julesVerne) will join us here to help out :smile:

The code is available from Clarion Magazine:
http://archive.clarionmag.com/cmag/v3/v3n5runtimedlls1.html

1 Like

Hi i know this is an old post but im stuck with this same problem.

¿How do i use libmaker to make friendly clarion lib from an Unmanaged Export from a C# DLL?

¿Is there any link still available to help me with this issue?

Regards,

Martin

@Martin_Vecchione you will need a customized version of LibMaker. The one that ships with the Clarion examples has some bugs in it. @MarkGoldberg has one that should do the job hosted on GitHub:

https://github.com/MarkGoldberg/ClarionCommunity/tree/master/CW/MGLibMaker

Hi @brahn no luck it seems that @MarkGoldberg version of LibMaker doesn`t export the libs file i need. I try to use the sample code you post in StackOverlfow without luck.

Actually im using the code posted in ClarionMag http://archive.clarionmag.com/cmag/v8/v8n04interop6.html but i dont know why i`m getting 0xc000005 Access Violation Error when i use it in a Multi-dll project…try to LoadLibrary in the main exe begore calling c# function but i still get the same error.

Any clues?

Thanks,

Isn’t that article about COM Interop? I don’t have a subscription at the moment but from memory I think it is. In that case LibMaker won’t help you. It only works if you are using UnmanagedExports from .NET and correctly decorating your methods with the DllExport property.

Yeap its about COM interop, actually COM works pretty well but we have the problem to register before using it (its a pain when user dont administrative account)…so im trying to move to native c++ wrapper.

When i try to use the code you posted in StackOverflow a few yearks ago i came with the problem that i dont have a customized libmaker to make friendly Clarion lib files so i create a managed visual c++ as a wrapper and then call it from a clarion dll and i get 0xc000005 Access Violation Error.

Thats where im stuck right now and dont know how to move foward

Martin

The think is, LibMaker won’t help you with the COM interop. If you are trying to use LibMaker for your c++ wrapper then the shipping LibMaker should work fine or certainly the one from Mark will work too.

The later versions of Clarion, C9.1 and up I think, support RegFree COM but I have never used it myself. You might want to investigate that or maybe @davbrat can help out?

I recently had to add functionality from a C# dll to clarion project. Thanks to the great help from @MarkGoldberg on Skype I was able to get this done.

First of all you will need to download and compile the modified libmaker from https://github.com/MarkGoldberg/ClarionCommunity/tree/master/CW/MGLibMaker
Open the .cwproj or for C6 and older, open the .prj and compile

After testing with normal datatypes like long and float I was able to confirm that my LIB was correct. Now for the string part it is important to remember that Clarion will use CString meaning you will have to marshalAs your string parameters

   [DllExport("doEnrollFPUser", CallingConvention = CallingConvention.StdCall)]
   public static void doEnrollFPUser([MarshalAs(UnmanagedType.LPStr)]string p_parameters)
        { ......

After this you build your LIB and add to your clarion app. Add the procedure you are going to call with the prototype correctly setup as

(*CString passedString),NAME('doEnrollFPUser'),PASCAL,RAW,DLL(TRUE)

To use the new procedure in your clarion app you will have to set a new CString variable and pass that new variable to your procedure

Hope this makes sense and could be of help to any one out there

3 Likes