Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell code failed to run
#1
The following powershell code can be run successfully in powershell_ISE, but it prompts to load dll error in QM, how to fix it, thanks in advance Heart

Code reference: https://www.example-code.com/powershell/create_json.asp

Because the ChilkatDotNet component does not provide QM programming code, I very much hope to solve the above problem.

I have not tried the C# code example. As far as I know, C# code needs to deal with the quotes and other issues.

Macro PS run
Code:
Copy      Help
str code=
;[Reflection.Assembly]::LoadFile("C:\Chilkat\ChilkatDotNet47.dll")
;
;$json = New-Object Chilkat.JsonObject
;
;#  The only reason for failure in the following lines of code would be an out-of-memory condition..
;
;#  An index value of -1 is used to append at the end.
;$index = -1
;
;$success = $json.AddStringAt(-1, "Title", "Pan's Labyrinth")
;$success = $json.AddStringAt(-1, "Director", "Guillermo del Toro")
;$success = $json.AddStringAt(-1, "Original_Title", "El laberinto del fauno")
;$success = $json.AddIntAt(-1, "Year_Released", 2006)
;
;$json.EmitCompact = $false
;$($json.Emit())

PsCmd code "" _s
out _s
#2
The problem has been solved, I need to use the 32-bit .net component and now it works fine.

If QM3 can run powershell code directly, it would be great because powershell code is very simple and it is more convenient to operate some system components. Smile

I have a question Huh

QM2 can call the POWERSHELL code, and the C# code can also be called.

Which one is faster if the same function is performed?

For process automation, I think the powershell syntax is simpler and more friendly than C# code

If the syntax of QM3 is like to powershell, it will be better, powershell is already cross-platform, very promising.
#3
In QM3 only C#.
PowerShell is much slower. Executes in new process. To measure code speed use functions PerfFirst, PerfNext, PerfOut.
#4
thank you for your reply,

In order to get faster, I am trying to achieve the same functionality using C# code.

I encountered two problems when running the code

1. Unable to register component

2. How to get the return value

The following are the code of the powershell and C#, the powershell code can run successfully, But the above problems exist in the C# code.
Any suggestions are welcome, thanks in advance

Can you combine powershell code and C# code and QM code into one file, and test the execution speed of each piece of code?
This is a good example of The QM calling a third-party C# component.   Smile


Macro QM Run PS Code
Code:
Copy      Help
;Requires 32-bit components Link: http://www.chilkatsoft.com/downloads_DotNet.asp
;Put the dll file in the location C:\Chilkat\ChilkatDotNet47.dll
str code=
;[Reflection.Assembly]::LoadFile("C:\Chilkat\ChilkatDotNet47.dll")
;
;$json = New-Object Chilkat.JsonObject
;
;#  The only reason for failure in the following lines of code would be an out-of-memory condition..
;
;#  An index value of -1 is used to append at the end.
;$index = -1
;
;$success = $json.AddStringAt(-1, "Title", "Pan's Labyrinth")
;$success = $json.AddStringAt(-1, "Director", "Guillermo del Toro")
;$success = $json.AddStringAt(-1, "Original_Title", "El laberinto del fauno")
;$success = $json.AddIntAt(-1, "Year_Released", 2006)
;
;$json.EmitCompact = $false
;$($json.Emit())

PsCmd3 code "" _s
out _s  ;;Can get the return result


Macro QM Run C# Code
Code:
Copy      Help
;Requires 32-bit components Link: http://www.chilkatsoft.com/downloads_DotNet.asp
;Put the dll file in the location C:\Chilkat\ChilkatDotNet47.dll
RegisterNetComComponent "C:\Chilkat\ChilkatDotNet47.dll" 2|4

CsExec("") ;;How to get the running result


#ret
//C# code
using System;
using Chilkat;

public class Test
{
public string Main()
{

Chilkat.JsonObject json = new Chilkat.JsonObject();

bool success;

//  The only reason for failure in the following lines of code would be an out-of-memory condition..

//  An index value of -1 is used to append at the end.
int index = -1;

success = json.AddStringAt(-1,"Title","Pan's Labyrinth");
success = json.AddStringAt(-1,"Director","Guillermo del Toro");
success = json.AddStringAt(-1,"Original_Title","El laberinto del fauno");
success = json.AddIntAt(-1,"Year_Released",2006);

json.EmitCompact = false;
//Console.WriteLine(json.Emit());
return json.Emit()
}
}
#5
CsExec  is rarely useful. Start from menu File / New / Templates / Scripting / "Scripting C#, call any functions". Delete unused examples in the code. Add some references if need. Don't need RegisterNetComComponent.

Simplified template:
Macro Scripting C#, call any functions4
Code:
Copy      Help
;create variable that manages compiled C# code
CsScript x

;optionally set some options
;x.SetOptions("debugConfig=true[]references=System.Data;System.XML")

;compile code after #ret
x.AddCode("")

;call static function
str R=x.Call("Class1.TestFunction" "test" 5)
out R


#ret
//C# code
using System;

public class Class1
{
public static string TestFunction(string s, int i)
{
,Console.Write(s); Console.Write(i); //display in QM output
,return "result";
}
}
#6
How to reference C:\Chilkat\ChilkatDotNet47.dll dll file in C#  


Macro Macro14
Code:
Copy      Help
CsScript x
x.SetOptions("references=Chilkat")
x.AddCode("")

;call static function
str R=x.Call("Class1.TestFunction")
out R


#ret
//C# code
using System;
using Chilkat;

public class Class1
{
public static string TestFunction()
{
Chilkat.JsonObject json = new Chilkat.JsonObject();

bool success;

//  The only reason for failure in the following lines of code would be an out-of-memory condition..

//  An index value of -1 is used to append at the end.
int index = -1;

success = json.AddStringAt(-1,"Title","Pan's Labyrinth");
success = json.AddStringAt(-1,"Director","Guillermo del Toro");
success = json.AddStringAt(-1,"Original_Title","El laberinto del fauno");
success = json.AddIntAt(-1,"Year_Released",2006);

json.EmitCompact = false;
//Console.WriteLine(json.Emit());
return json.Emit();
}
}
#7
Read SetOptions help.
#8
I didn't find a solution for C#, so I don't know the execution speed of C# code. Shy

Using Chilk powershell code, don't need to make any changes,  use it directly.
#9
Code:
Copy      Help
CsScript x
x.SetOptions("references=C:\Chilkat\ChilkatDotNet47.dll")
x.AddCode("")

;call static function
str R=x.Call("Class1.TestFunction")
out R


#ret
//C# code
using System;
using Chilkat;

public class Class1
{
public static string TestFunction()
{
Chilkat.JsonObject json = new Chilkat.JsonObject();

bool success;

//  The only reason for failure in the following lines of code would be an out-of-memory condition..

//  An index value of -1 is used to append at the end.
// int index = -1;

success = json.AddStringAt(-1,"Title","Pan's Labyrinth");
success = json.AddStringAt(-1,"Director","Guillermo del Toro");
success = json.AddStringAt(-1,"Original_Title","El laberinto del fauno");
success = json.AddIntAt(-1,"Year_Released",2006);

json.EmitCompact = false;
//Console.WriteLine(json.Emit());
return json.Emit();
}
}
#10
@kevin
Thank you for your help

I tested 32-bit and 64-bit components, There are still error messages:

Error (RT) in <open ":2238: /115">Macro15: 0x80131500,
Failed to load file or assembly "ChilkatDotNet47, Version=9.5.0.78, Culture=neutral, PublicKeyToken=eb5fc1fc52ef09bd" or one of its dependencies. The system can not find the file specified.
In Class1.TestFunction(). <help #IDP_ERR>?

The json component is free and does not require a token
#11
i don't know it works for me everytime.

Try moving ChilkatDotNet47.dll to the qm program folder(just the dll file)
make sure it is Chilkat 32-bit then use 
Code:
Copy      Help
CsScript x
x.SetOptions("references=$qm$\ChilkatDotNet47.dll")
x.AddCode("")

;call static function
str R=x.Call("Class1.TestFunction")
out R


#ret
//C# code
using System;
using Chilkat;

public class Class1
{
public static string TestFunction()
{
Chilkat.JsonObject json = new Chilkat.JsonObject();

bool success;

//  The only reason for failure in the following lines of code would be an out-of-memory condition..

//  An index value of -1 is used to append at the end.
// int index = -1;

success = json.AddStringAt(-1,"Title","Pan's Labyrinth");
success = json.AddStringAt(-1,"Director","Guillermo del Toro");
success = json.AddStringAt(-1,"Original_Title","El laberinto del fauno");
success = json.AddIntAt(-1,"Year_Released",2006);

json.EmitCompact = false;
//Console.WriteLine(json.Emit());
return json.Emit();
}
}
#12
thanks,
Successful, dll needs to be placed in the QM directory, why? Huh


Forum Jump:


Users browsing this thread: 1 Guest(s)