Page 1 of 2
Expose X# run-time functions to C#
Posted: Wed Oct 09, 2019 6:54 pm
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
Expose X# run-time functions to C#
Posted: Wed Oct 09, 2019 7:17 pm
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
Expose X# run-time functions to C#
Posted: Wed Oct 09, 2019 7:41 pm
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();
}
}
Expose X# run-time functions to C#
Posted: Wed Oct 09, 2019 7:57 pm
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.
Expose X# run-time functions to C#
Posted: Wed Oct 09, 2019 9:47 pm
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
Expose X# run-time functions to C#
Posted: Wed Oct 09, 2019 10:21 pm
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
Expose X# run-time functions to C#
Posted: Thu Oct 10, 2019 2:32 am
by Chris
Thanks Jamal, problem confirmed and logged!
Expose X# run-time functions to C#
Posted: Thu Oct 10, 2019 3:56 am
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
Expose X# run-time functions to C#
Posted: Thu Oct 10, 2019 6:18 am
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
Expose X# run-time functions to C#
Posted: Thu Oct 10, 2019 11:16 am
by Jamal
Hi Wolfgang,
That would be awesome!
Jamal