Expose X# run-time functions to C#

This forum is meant for questions and discussions about the X# language and tools
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

Expose X# run-time functions to C#

Post by Jamal »

I am opening a dbf using the DBFCDX driver, but it fails to open CDX file since it contains functions such as DTos().

How to add the entire set of the X# run-time function in C# and also create my BYO functions so that CDX can link to them successfully.

I created a C# function:

Code: Select all

public string DToS(XSharp.__Date dDate)
        {
            return Dbf.DToS(dDate);
            
        }	
but I call Dbf.DbUseArea(...), I get $exception {"Specified cast is not valid."} XSharp.Error

Note: the DBF file opens using the DBFNTX driver.

Please advise.

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

Expose X# run-time functions to C#

Post by wriedmann »

Hi Jamal,
you need to build your own intermediate DLL that uses the X# runtime and exposes only strongly typed methods.
I'm doing something like that to use DBFs in my X# Core applications (that don't use the X# runtime itself).
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Expose X# run-time functions to C#

Post by Chris »

Hi Jamal,

Did you add references to the XSharp runtime dlls? If you did, then the macro compiler should find them automatically.

I just tried the following and it seems to work fine, I get the first logical record printed:

Code: Select all

public class Program
{
	static void Main()
	{
		string cPath;
		cPath = "c:...testcs.dbf";
		XSharp.RT.Functions.VoDbUseArea(true, "DBFCDX" , cPath , "alias" , true , false);
		XSharp.RT.Functions.DbGoTop();
		System.Console.WriteLine(XSharp.RT.Functions.FieldGet(1).ToString());
		XSharp.RT.Functions.VoDbCloseArea();		
	}
}
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Expose X# run-time functions to C#

Post by Chris »

Also, when you want to use in the dbf indexes (or in the macro compiler in gnerl) methods defined in your c# module, you need to put them in a class and add this assembly wide attribute:

[assembly: XSharp.Internal.ClassLibrary("MyRuntimeFuncsClass", "")]

Wolfgang, what you say is correct for directly calling X# runtime functions that have CLIPPER calling convention from c#, but when using the macrocompiler (so also functions in expressions) this is not a problem, because the macro compiler know how to call them.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

Expose X# run-time functions to C#

Post by Jamal »

Hi Chris,

I am still getting the "Specified cast is not valid." error when calling XSharp.RT.Functions.DbUseArea().

Could you please test the attached dbf file and cdx?

Note: the CDX has one order on DToS(HIREDDATE). In X# the DBF file opens fine.

Jamal
Attachments
testdbf.zip
(4.7 KiB) Downloaded 102 times
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

Expose X# run-time functions to C#

Post by Jamal »

It seems adding the following line:

XSharp.CoreDb.RddInfo(_SET_AUTOORDER, 1);

was causing the error. After I commented it out, the file opened fine!

I tested also in X#, and got the same error:

Code: Select all

PRIVATE METHOD OpenDBFButton_Click(sender AS OBJECT, e AS System.EventArgs) AS VOID STRICT
             RddInfo(_SET_AUTOOPEN, true)
           
            RddInfo(_SET_AUTOORDER, 1)   // causes Specified cast is not valid. This works in VO.
            
            if (DbUseArea(true, "DBFCDX", "C:TestingDirTESTDBF.dbf", "TESTDBF", true, false))
                MessageBox.Show(LastRec().ToString())
                DBCloseArea()
                MessageBox.Show("Opened")
                
            else
                MessageBox.Show("Failed") 
            endif
            RETURN
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Expose X# run-time functions to C#

Post by Chris »

Thanks Jamal, problem confirmed and logged!
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Expose X# run-time functions to C#

Post by wriedmann »

Hi Chris,
Wolfgang, what you say is correct for directly calling X# runtime functions that have CLIPPER calling convention from c#, but when using the macrocompiler (so also functions in expressions) this is not a problem, because the macro compiler know how to call them.
I have a few classes in my XbaseInterface DLL:
- an AppDBServer class that inherits from the DBServer class
- a CoreDBFServer class that acts as proxy for the AppDBServer class and exposes methods like FieldGetString(), FieldGetLogic(), FieldGetDecimal() and a few more so I don't need to cast the return values in my Core dialec applications
- a XbDate class that works like a Nullable<DateTime>, but maintains only the date part. It is like the Xbase date, but usable without problems in Core dialect applications.

If there is some interest, I can share that library toghether with a sample.

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

Expose X# run-time functions to C#

Post by Juraj »

Hi Wolfgang,


I create the new app only as WPF in Core dialect. Sometimes I need to load an older DBF file. Your library would help.

Juraj
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

Expose X# run-time functions to C#

Post by Jamal »

Hi Wolfgang,

That would be awesome!

Jamal
Post Reply