How to use External C++ dll

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

How to use External C++ dll

Post by Juraj »

Hi All,
I want to implement an external C++ library into my application. I will write it in XSharp core dialect as a WPF Windows app. What a function to do from this library you would recommend?

Juraj
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

How to use External C++ dll

Post by wriedmann »

Hi Juray,

the C++ library comes in form of a DLL? And do you need only functions or also classes?
Do you have tried to ask the vendor if he has also C# bindings?
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

How to use External C++ dll

Post by wriedmann »

Hi Juraj,

the same rules apply for X# as for C#:
https://stackoverflow.com/questions/772 ... in-c-sharp
https://stackoverflow.com/questions/295 ... sharp?rq=1

Basically it is the same situation as in VO (but since VO is written in C++, there is some documented but really complicated way to make it work nevertheless - please see "Using C++ DLLs" in the "How To" section of the VO help).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

How to use External C++ dll

Post by Juraj »

Hi Wolfgang,

Thank you for your response. From 1.1.2020 on-line cash register must be used in Slovakia. The EFox (on line cash register) manufacturer also ships this library with the hardware. I do not have more information from the manufacturer.
In VO or Vulcan I use:

_DLL FUNCTION BeginSaleReceipt(nHandle AS INT, cTransactionID AS STRING) AS INT
PASCAL:EFoxComm.BeginSaleReceipt ANSI

I wanted to see if there was another, better way.

Juraj
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How to use External C++ dll

Post by robert »

Juraj,
Try this

STATIC CLASS EFox
[DllImport("EFoxComm.dll")];
STATIC METHOD BeginSaleReceipt(nHandle AS INT, cTransactionID AS STRING) AS INT
END CLASS

You can then call the function with EFox.BeginSaleReceipt(...)

See also https://github.com/X-Sharp/XSharpPublic ... /Win32.prg for an example

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

How to use External C++ dll

Post by Juraj »

Hi Robert,

Thank for for your response.

I use this code:

USING System.Runtime.InteropServices

STATIC CLASS EFox
//
[DllImport("eKasaEFoxComm.dll"];
STATIC METHOD Abort(nHandle AS INT) AS INT
//
[DllImport("eKasaEFoxComm.dll"];
STATIC METHOD BeginSaleReceipt(nHandle AS INT, cTransactionID AS STRING) AS INT
//
END CLASS

Result:
Error XS9002 Parser: unexpected input 'STATIC CLASS EFoxCRLF //[DllImport("eKasaEFoxComm.dll"]'

Juraj
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

How to use External C++ dll

Post by Juraj »

Hi Robert

in line

[DllImport("EFoxComm.dll"];

the right parenthesis was missing. My fault that I noticed it later

Juraj
User avatar
Jean3riv
Posts: 2
Joined: Sat Sep 26, 2015 11:57 am

How to use External C++ dll

Post by Jean3riv »

Hi Robert
I have not been able to call a function via a CLASS, even with your examples...
However I have been able to call many functions to an external win32 DLL via the following code:
//_________________

[DllImport("Randomad32.dll")];
_DLL FUNCTION MersenneRandomInitD ( IsEED AS INT ) AS VOID PASCAL:Randomad32.MersenneRandomInitD
// Must be call before MersenneIRandomD() function
// Must be called (ONLY 1 TIME) before you use the MersenneIRandomD() function, if this last function is
//used many times in a loop. Otherwise there is very bad perfomance in speed.

[DllImport("Randomad32.dll")];
_DLL FUNCTION ReadTSC() AS INT PASCAL:Randomad32.ReadTSC
// This function will generate the seed

[DllImport("Randomad32.dll")];
_DLL FUNCTION MersenneIRandomD( iMin AS INT , iMax AS INT ) AS INT PASCAL:Randomad32.MersenneIRandomD
//This function to generate a number between Min and Max, it can be called many times into your own class or function

//The DLL can be found here:
//https://www.rayonline.com/jrinfo/downlo ... omad32.dll
//______________________________
Hope this will help somebody
Best regards
Jean Raymond
https://rayonline.com/
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How to use External C++ dll

Post by robert »

Jean Raymond,
The combination of DllImport and _DLL FUNCTION is not needed.
_DLL FUNCTION will automacally generate the DllImport attribute

Below is an example of DllImport in the XSharp.VO DLL.
As you can see I am declaring strings with AS STRING and I am adding the CharSet attribute so the Marshalling code in the .Net runtime know that string needs to be converted from Unicode to Ansi

Code: Select all

STATIC CLASS XSharp.Win32
     CONST SW_SHOWNORMAL  := 1 AS LONG 
    [DllImport("kernel32.dll", CharSet := CharSet.Ansi )];
    STATIC METHOD WinExec(lpCmdLine AS STRING, uCmdShow AS DWORD) AS DWORD
    [DllImport("kernel32.dll" )];
    STATIC METHOD GetCurrentProcess() AS IntPtr 
    [DllImport("kernel32.dll" )];
    STATIC METHOD VirtualQueryEx(hProcess AS IntPtr, lpAddress AS IntPtr, ;
        lpBuffer AS _WINMEMORY_BASIC_INFORMATION, dwLength AS DWORD) AS DWORD PASCAL
END CLASS

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply