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

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

Postby AnjaK » Tue Jul 09, 2019 12:55 pm

taken from http://forums.fivetechsupport.com/posting.php?mode=edit&f=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  Expand view

#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  Expand view

┌────────────────────────────────────────────────────────────────────────────┐
?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  Expand view

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
AnjaK
 
Posts: 6
Joined: Wed Jun 05, 2019 2:24 pm

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

Postby cnavarro » Tue Jul 09, 2019 2:13 pm

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  Expand view

//----------------------------------------------------------------------------//
// 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  Expand view

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
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby cnavarro » Tue Jul 09, 2019 2:17 pm

Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby cnavarro » Tue Jul 09, 2019 8:13 pm

Second example:
Create your own DLL to access its functions

My prg: DOTNET2.PRG

Code: Select all  Expand view


#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  Expand view

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.
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby AnjaK » Wed Jul 10, 2019 10:40 am

Thank you for your response, Cristobal.

But the problem remains.
If I compile just this code:
Code: Select all  Expand view

#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
AnjaK
 
Posts: 6
Joined: Wed Jun 05, 2019 2:24 pm

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

Postby cnavarro » Wed Jul 10, 2019 4:13 pm

Dear Anja
I have not gotten it to work with xHarbour
Why do not you use Harbour?
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby AnjaK » Tue Jul 16, 2019 10:40 am

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
AnjaK
 
Posts: 6
Joined: Wed Jun 05, 2019 2:24 pm

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

Postby cnavarro » Tue Jul 16, 2019 11:08 am

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
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby ricbarraes » Tue Jul 30, 2019 9:02 pm

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  Expand view
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  Expand view

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.
Sds,
Ricardo Arraes
ricardo@vfatec.com.br
User avatar
ricbarraes
 
Posts: 55
Joined: Tue Jun 30, 2015 2:26 am
Location: Brazil

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

Postby cnavarro » Tue Jul 30, 2019 9:56 pm

Please try return at moment always string value

Code: Select all  Expand view

namespace SimpleDllFramework
{
    public class Class1
    {
      public static int GetNumber(int input = 1)
      {
         int result = input * 10 ;
         string myString = result.ToString();
         return myString;
      }
   }
}
 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby ricbarraes » Wed Jul 31, 2019 1:45 am

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  Expand view

namespace SimpleDllFramework
{
    public class Class1
    {
      public static int GetNumber(int input = 1)
      {
         int result = input * 10 ;
         string myString = result.ToString();
         return myString;
      }
   }
}
 
Sds,
Ricardo Arraes
ricardo@vfatec.com.br
User avatar
ricbarraes
 
Posts: 55
Joined: Tue Jun 30, 2015 2:26 am
Location: Brazil

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

Postby ricbarraes » Thu Aug 01, 2019 12:04 pm

+1
Sds,
Ricardo Arraes
ricardo@vfatec.com.br
User avatar
ricbarraes
 
Posts: 55
Joined: Tue Jun 30, 2015 2:26 am
Location: Brazil

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

Postby Eroni » Thu Aug 22, 2019 11:41 am

Hi to all.
Is there predict to write the code from tdotnet to xharbour?
Regards.
FWH 1709 BCC72 MySql MariaDB
Visual Studio 2019 / Xamarin / C#
User avatar
Eroni
 
Posts: 90
Joined: Fri Jul 21, 2006 7:15 pm
Location: Criciuma/SC Brazil

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

Postby cnavarro » Thu Aug 22, 2019 12:34 pm

Not possible, sorry
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby Eroni » Thu Aug 22, 2019 1:53 pm

Ok, thanks for reply.
FWH 1709 BCC72 MySql MariaDB
Visual Studio 2019 / Xamarin / C#
User avatar
Eroni
 
Posts: 90
Joined: Fri Jul 21, 2006 7:15 pm
Location: Criciuma/SC Brazil

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 81 guests