Author Topic: C# Loaded Via WinBatch - Renci.SshNet.dll won't load.  (Read 106 times)

JTaylor

  • Pundit
  • *****
  • Posts: 1023
    • Data & Stuff Inc.
C# Loaded Via WinBatch - Renci.SshNet.dll won't load.
« on: September 09, 2019, 07:43:56 pm »
The following code works fine in VS but when I try loading via WinBatch it tells me that it cannot load Renci.SshNet file or assembly because it cannot find the file.   I have tried using the full path but that didn't help in the reference.  Adding "using" statements for it doesn't help either.    Suggestions?

Thanks.

Jim


Code: Winbatch

cSharp = $"

namespace c_browser
{
    using System;
    using System.Data;
    using MySql.Data.MySqlClient;

 

    public class Program
    {

        public void DB_Connect()
        {

            string connstring = "server=localhost; port=3306; database=xxxxxxxxxx; UID=xxxxx; password=xxxxxxxx";
            MySqlConnection myConn = new MySqlConnection(connstring);

            try
            {

                myConn.Open();

                string web_login = "Y";
                string sql = "SELECT isbn, title FROM resources where buy_status = 'BROWSE' and isbn like '97%%' and Length(isbn) = 13;";
                MySqlCommand cmd = new MySqlCommand(sql, myConn);
                MySqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    string isbn = rdr[0].ToString();

                   // MessageBox.Show(isbn);
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.ToString());
            }

            myConn.Close();

        }
    }
}


$"


;message("HEY",cSharp)


GoSub LOAD_ROUTINE

  Load_WebBrowser()

  oSample = ObjectClrNew( 'c_browser.Program' )
  oSample.DB_Connect()
 

Exit

:LOAD_ROUTINE


#DefineSubRoutine Load_WebBrowser()
;***************************************************************************
;**  Run C# in memory using WinBatch - Use Namespace.Class defined in CSharpeSource
;**
;** Purpose:  Run C# in memory using WinBatch - Using Namespace.Class defined in CSharpeSource
;** Inputs:
;** Outputs: Results in message
;**
;** Developer: Deana Falk 2014.04.14
;***************************************************************************

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; mscorlib assembly is automatically loaded by WinBatch when the CLR is loaded.
; ObjectClrOption ('use','mscorlib, version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

  ObjectClrOption ( 'useany', 'System')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Compiles the c# code in Memory
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')
  objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
  objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ;TRUE
 
  objParams.ReferencedAssemblies.Add("System.dll")
  objParams.ReferencedAssemblies.Add("System.Data.dll")
  objParams.ReferencedAssemblies.Add("Mysql.Data.dll")
  objParams.ReferencedAssemblies.Add("Renci.SshNet.dll")
  objParams.ReferencedAssemblies.Add("System.Security.dll")
  ;objParams.ReferencedAssemblies.Add("")


  objResult = objCSharp.CompileAssemblyFromSource(objParams,cSharp)

  ;Compiler Output
  If objResult.Output.Count > 0 Then
    strOutput = ''
    For x = 0 to objResult.Output.Count-1
       If strOutput == "" Then
         strOutput = objResult.Output.Item(x)
       Else
         strOutput = strOutput:@LF:objResult.Output.Item(x)
       EndIf
    Next
    Pause('Compiler Output',strOutput)
  Endif
 
  ; Compiler Errors
  If objResult.Errors.Count > 0 Then
    strErrors = ''
    ForEach ce In objResult.Errors
      ;Pause("Error", ce.ToString())
      If strErrors == "" Then
        strErrors = ce.ToString()
      Else
        strErrors = strErrors:@LF:ce.ToString()
      EndIf
    Next
    Pause('Compiler Errors',strErrors)
    Exit
  EndIf
 


#EndSubRoutine



Return


 

stanl

  • Pundit
  • *****
  • Posts: 946

td

  • Tech Support
  • *****
  • Posts: 3054
    • WinBatch
Re: C# Loaded Via WinBatch - Renci.SshNet.dll won't load.
« Reply #2 on: September 10, 2019, 07:56:55 am »
The obvious question is where is the "Renci.SshNet.dll" file located on your system?  It either needs to be in the GAC or you need to use the "appbase" option to set the location of that DLL.  The MySQL installer places a copy of the "Mysql.Data.dll" in the GAC for you under the GAC_MSIL directory.

Another possibility is that you are not loading one of the "Renci.SshNet.dll" assembly's dependent assemblies.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

  • Pundit
  • *****
  • Posts: 1023
    • Data & Stuff Inc.
Re: C# Loaded Via WinBatch - Renci.SshNet.dll won't load.
« Reply #3 on: September 10, 2019, 09:52:12 am »
It is in the GAC_MSIL.  I also copied it to the script directory to see if that helps.   I used the full path in the R.A.Add()  method.  I tried what you suggested and get a slight different error.   

UPDATE:   I am not sure what I have done differently other than I tried what you suggested with appbase and copied the Renci dll from the VS compile directory and replaced the one that I copied from the GAC_MSIL location.   Must be some important difference in the Renci file even though they seem to be the same version under properties.  It seems to be working now.  Thanks.

Jim

td

  • Tech Support
  • *****
  • Posts: 3054
    • WinBatch
Re: C# Loaded Via WinBatch - Renci.SshNet.dll won't load.
« Reply #4 on: September 10, 2019, 10:44:47 am »
I built the "Renci.SshNet.dll" assembly,  dumped it into the scripts directory, added DirScript():"Renci.SshNet.dll" to the compile and set "appbase" to "DirScript()" with the ObjectClrOption function.  It worked first try. 

Next, I installed the "Renci.SshNet.dll" into the GAC using gacutil.exe, removed the DLL from the script's directory and then change the Add line to point to "Renci.SshNet.dll" file in the original release build directory and commented out the "ObjectClrOption('appbase', DirScript())" line from the script.  Again the script worked first try.

Methinks something went amock when you installed the DLL into the GAC.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

  • Pundit
  • *****
  • Posts: 1023
    • Data & Stuff Inc.
Re: C# Loaded Via WinBatch - Renci.SshNet.dll won't load.
« Reply #5 on: September 10, 2019, 12:13:22 pm »
May be.  I never installed that intentionally.   It was just there.   I went back and copied the version from GAC_MSIL to the script directory and error returns.   If I copy the one from VS studio back in, it works fine.   In any event, it is working so thanks again.

Jim

td

  • Tech Support
  • *****
  • Posts: 3054
    • WinBatch
Re: C# Loaded Via WinBatch - Renci.SshNet.dll won't load.
« Reply #6 on: September 11, 2019, 06:58:39 am »
I used the latest GitHub source and compiled it using VS 2019.  The VS projects did not install the compiled assembly into the GAC so your version of the assembly must have gotten into the GAC by some mechanism other than simply building it with VS or you used a different source.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade