Oxygen Basic
Programming => Example Code => General => Topic started by: chrisc on April 13, 2018, 03:48:24 PM
-
I have atteched 3 programs where a C# program calls 2 O2 dlls
which can pass parameters to each other and back.
C# program GitO2data.exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// need this to run DllImport
using System.Runtime.InteropServices;
namespace GitO2Data
{
public partial class Form1 : Form
{
// Note that this program is compiled to 64 bits while its O2 dlls are also compiled to 64 bits
// setup the DLLs
[DllImport("DllMain.DLL", EntryPoint = "RetString")]
[return: MarshalAs(UnmanagedType.AnsiBStr)]
public static extern string RetString([MarshalAs(UnmanagedType.AnsiBStr)]
ref string Seltxt);
// reference https://stackoverflow.com/questions/16332701/how-to-call-c-dll-in-c-sharp
[DllImport("Greet.dll", EntryPoint = "greet")]
public static extern int greet(string rfst);
public Form1()
{
InitializeComponent();
}
// Button to get Message from an O2 DLL
private void button1_Click(object sender, EventArgs e)
{
// display the greeting
string txtmynam = "Good Man";
int dummynum = greet(txtmynam);
MessageBox.Show(" number from O2 : " + dummynum.ToString() );
}
// Button to get a string from an O2 DLL
private void button2_Click(object sender, EventArgs e)
{
string txtReturn = "";
// Obtain the entered text
string EnterText = textBox1.Text.Trim();
// get the return string
txtReturn = RetString(ref EnterText);
// Display the return string on the screen
MessageBox.Show(" String from O2 dll : " + txtReturn);
}
}
}
and the Dllmain.o2bas (thanxx to Charles )
% dll
% filename "DllMain.dll"
uses rtl64
def TRIM ltrim(rtrim(%1))
function RetString(stwant as string) as string, export
If LCASE(TRIM(stwant)) = "one" then
return "First string (one) was entered"
else
return " Other string was entered"
end if
end function
the Greet.o2bas (thanxx to Charles )
% dll
% filename "Greet.dll"
uses rtl64
function greet(stgreet as char) as long, export
print "hello "+stgreet
return 1
end function
-
Note that all progs are compiled to 64bits
-
Thanks Chris,
This C# stuff looks really intimidating.
I presume Visual Studio churns out most of the code.
-
Not really Charles, the Visual designer IDE only helps out with the visual form creation and where to locate controls
like the buttons and txt boxes. i still need to type in the main codes which is helped by IDE's intelligent sense.
i would say it is 30% work by the IDE and 70% by the programmer. but the plus side of the IDE is the debugging that is
quite efficient.
C# is not very secure as it uses MSIL or clear text pcode and is easier to hack. that's why we need O2 which
compiles to native codes. using O2 dll is important as the main C# calling program is easily hackable but the O2 dlls are
tough to hack. thus we place all the important functions into O2 dlls
your o2 compiler is very good as shown above we can now interface with c#, thanxx a lot Charles