Page 1 of 3

Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 09, 2019 12:55 pm
by AnjaK
taken from http://forums.fivetechsupport.com/posti ... 3&p=222855

Hello!

It would be nice to use a .NET Dll with Fivewin and I tried the sample, but nothing happend, literally nothing. So I created my own .NET Dll and changed the sample dotnet.prg to this:

Code: Select all

#include "FiveWin.ch"

function Main()

   local oNet

   oNet := TDotNet():New()
    ? IIF(oNet != nil, "Net yes!", "Net no :(")
    XBrowse(__objGetValueList(oNet))
    
    oNet:Execute("SimpleDllFramework.dll", "SimpleDllFramework.Class1", "GetNumber", 2)
    ? oNet:GetNetError()
    ? oNet:GetReturnValue()
   
   oNet:End()
   
return nil
 

Code: Select all

┌────────────────────────────────────────────────────────────────────────────┐
?FiveWin for xHarbour 19.03 - Mar. 2019          xHarbour development power │▄
?(c) FiveTech 1993-2019 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 │█
└────────────────────────────────────────────────────────────────────────────┘?
  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀?
Compiling...
xHarbour 1.2.3 Intl. (SimpLex) (Build 20190603)
Copyright 1999-2018, http://www.xharbour.org http://www.harbour-project.org/
Compiling 'dotnet.prg' and generating preprocessed output to 'dotnet.ppo'...
Generating C source output to 'dotnet.c'...
Done.
Lines 27, Functions/Procedures 1, pCodes 69
Embarcadero C++ 7.30 for Win32 Copyright (c) 1993-2017 Embarcadero Technologies,
 Inc.
dotnet.c:
Turbo Incremental Link 6.80 Copyright (c) 1997-2017 Embarcadero Technologies, In
c.
* Application successfully built *
 
The sample doesn't show me either the messages or the XBrowse.
What went wrong?
I appreciate every hint I could get here ;)
Thanks in advance.

The simplest Dll I could imagine ;)

Code: Select all

namespace SimpleDllFramework
{
    public class Class1
    {
      public static int GetNumber(int input = 1)
      {
         return input * 10;
      }
   }
}
 
Compiled with .NET Framework 4 (first with 4.6.1), Core 2.2 and Standard 2 but nothing worked.

Sincerely

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 09, 2019 2:13 pm
by cnavarro
Anjak,
It works
Sample of use ( excuse my very poor english )

I have a DLL created in C # language named REPORT.DLL

My PRG ( DOTNET1.PRG )

Code: Select all

//----------------------------------------------------------------------------//
// Author: Cristobal Navarro
//----------------------------------------------------------------------------//
#include "FiveWin.ch"

Static cDll       := ".\HelloWorld.dll"
Static cWorkSpace := "ReportSamples.HelloWorld"
Static cMethod    := "Hola"   // Hola()
Static cOutFile   := ".\HelloWorld.pdf"

//----------------------------------------------------------------------------//

function Main()

   local oNet

   oNet   := TDotNet():New()
   oNet:Execute( cDll, cWorkSpace, cMethod, cOutFile )
   //? oNet:GetResult(), oNet:GetReturnValue(), oNet:GetValueReturn()

   ? oNet:GetNetError()
   oNet:End()

return nil
 
I have created a DLL with the functions that will serve as a "wrapper" to access the functions of the DLL Report.dll
My file .cs is HelloWorld.cs

Code: Select all

using Root.Reports;
using System;
using System.IO;

namespace ReportSamples
{
  /// <summary>Hello World (PDF Version)</summary>
  public class HelloWorld
  {
    //----------------------------------------------------------------------------------------------------x
    /// <summary>Starts the "Hello World" sample.</summary>
    //public static Object Hola( string fileout )
    public static int Hola( string fileout )
    {
      //string fileout = args[0];
      Report report = new Report(new PdfFormatter());
      FontDef fd = new FontDef(report, "Arial");
      FontProp fp = new FontPropMM(fd, 25);
      Page page = new Page(report);
//      page.AddCenteredMM(80, new RepString(fp, "Hello World!"));
      page.AddCB_MM(80, new RepString(fp, "Hello World!"));
      RT.ViewPDF(report, fileout ); //"HelloWorld.pdf");
      return 0; //report;
    }
  }
 
And file .bat for build this DLL ( buildcshello.bat )
csc /out:helloworld.dll /target:library /r:reports.dll helloworld.cs
and when I build and execute my prg ( DOTNET1.EXE )

Image

Later I will add other examples

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 09, 2019 2:17 pm
by cnavarro

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 09, 2019 8:13 pm
by cnavarro
Second example:
Create your own DLL to access its functions

My prg: DOTNET2.PRG

Code: Select all


#include "FiveWin.ch"

function Main()

   local oNet

   oNet   := TDotNet():New()
   ? oNet:NetVersion()[ 2 ]
   // ? oNet:GetNetError(), oNet:GetResult()
   oNet:Execute( "test.dll", "dllNamespace.dllClass", "ShowMsg", "C# from FWH" )
   ? if( Empty( oNet:GetNetError() ), "OK", oNet:GetNetError() )
   oNet:Execute( "test.dll", "dllNamespace.dllClass", "ShowMsg", "C#" )
   ? if( Empty( oNet:GetNetError() ), "OK", oNet:GetNetError() )
   oNet:Execute( "test.dll", "dllNamespace.dllClass", "GetMsg" )
   ? if( Empty( oNet:GetNetError() ), "OK", oNet:GetNetError() )
   oNet:End()
   
return nil

//----------------------------------------------------------------------------//
 
My file .cs: TEST.CS

Code: Select all

using System.Windows.Forms;
using System;
using System.Runtime.InteropServices;

namespace dllNamespace
{
    public class dllClass
    {
        public static int ShowMsg( string msg )
        {
            MessageBox.Show( msg );
            return 0;
        }

        public static int GetMsg( string cVal )
        {
            MessageBox.Show( "KKK" );
            return 0;
        }

    }
}
 
File .BAT for build file .CS: BUILDCS.BAT
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
csc /out:test.dll /target:library test.cs
Note that the class still has limitations, especially when returning non-integer values.
Above all it is designed to use DLLs in C #, third-party.

Re: Using C# (and .NET) from FWH not working(?)

Posted: Wed Jul 10, 2019 10:40 am
by AnjaK
Thank you for your response, Cristobal.

But the problem remains.
If I compile just this code:

Code: Select all

#include "FiveWin.ch"

function Main()

   local oNet

    ? "Start!"
    
   oNet := TDotNet():New()
    ? IIF(oNet != nil, "Net yes!", "Net no :(")

   oNet:End()
   
return nil
 
I see the MsgBox with "Start!" but the second one (? IIF(oNet != nil, "Net yes!", "Net no :(")) doesn't show up. I can not see if the object oNet is nil or not.

How do you compile your PRG?
I use xHarbour with BCC7.

Thank you once again
Anja

Re: Using C# (and .NET) from FWH not working(?)

Posted: Wed Jul 10, 2019 4:13 pm
by cnavarro
Dear Anja
I have not gotten it to work with xHarbour
Why do not you use Harbour?

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 16, 2019 10:40 am
by AnjaK
Dear Cristobal,

thank you for your answer. Harbour instead of xHarbour is the solution to the problem. Now I know why it isn't working.
But we have a big legacy application and I don't know if it is possible to transform this "big ball of mud" to Harbour.

We will find a way to work with the .NET lib we have, convert to an .exe file or something like that.

Sincerely

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 16, 2019 11:08 am
by cnavarro
AnjaK wrote:Dear Cristobal,

thank you for your answer. Harbour instead of xHarbour is the solution to the problem. Now I know why it isn't working.
But we have a big legacy application and I don't know if it is possible to transform this "big ball of mud" to Harbour.

We will find a way to work with the .NET lib we have, convert to an .exe file or something like that.

Sincerely
Dear Anja
Another solution is to make a small exe in harbour and be called from your application
If I can help you with something else, do not hesitate to contact me

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 30, 2019 9:02 pm
by ricbarraes
Good Afternoon Everybody,
I'm trying to call some CSharp functions from my FWH code through an .DLL, but it's not working.

I tried a really simple code as suggested above, but I'm receiving some error messages, and I can't figure out what is happening.

C# function in capture.dll

Code: Select all

using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace capture
{
    public class capClass
    {
        public static int teste(int i)
        {
            return i * 2;
        }
    }
}

Harbour code

Code: Select all

oNet := TDotNet():new()
oNet:Execute("capture.dll","capture.capClass","teste",1)
?oNet:GetNetError()
?oNet:GetReturnValue()

oNet:End()

 

the first time I execute the code, I'm receiving this message:

Error Description: Error BASE /1081 Argument Error: +

DOTNET_.PRG => TDOTNET:HOSTEXECUTE(257)
DOTNET_.PRG => TDOTNET:EXECUTE(160)


Then, when I close the window and reopen it to my function again, I receive this message

Error HostStart


Does anyone know what is wrong with my code?

P.S.: I'm generating the .DLL via Class Library Project on Visual Studio 2017.

Re: Using C# (and .NET) from FWH not working(?)

Posted: Tue Jul 30, 2019 9:56 pm
by cnavarro
Please try return at moment always string value

Code: Select all

namespace SimpleDllFramework
{
    public class Class1
    {
      public static int GetNumber(int input = 1)
      {
         int result = input * 10 ;
         string myString = result.ToString();
         return myString;
      }
   }
}
 

Re: Using C# (and .NET) from FWH not working(?)

Posted: Wed Jul 31, 2019 1:45 am
by ricbarraes
Ok, the first message is gone after I did that. thank you!
But now when I open my window for the first time to execute the function, it show me the error message:

Error HostExecute

And then, when I try to reopen the window as many times as I want, the error message change to:

Error HostStart

cnavarro wrote:Please try return at moment always string value

Code: Select all

namespace SimpleDllFramework
{
    public class Class1
    {
      public static int GetNumber(int input = 1)
      {
         int result = input * 10 ;
         string myString = result.ToString();
         return myString;
      }
   }
}
 

Re: Using C# (and .NET) from FWH not working(?)

Posted: Thu Aug 01, 2019 12:04 pm
by ricbarraes
+1

Re: Using C# (and .NET) from FWH not working(?)

Posted: Thu Aug 22, 2019 11:41 am
by Eroni
Hi to all.
Is there predict to write the code from tdotnet to xharbour?
Regards.

Re: Using C# (and .NET) from FWH not working(?)

Posted: Thu Aug 22, 2019 12:34 pm
by cnavarro
Not possible, sorry

Re: Using C# (and .NET) from FWH not working(?)

Posted: Thu Aug 22, 2019 1:53 pm
by Eroni
Ok, thanks for reply.