Posts: 726
Threads: 99
Joined: Mar 2018
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
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
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
Posts: 726
Threads: 99
Joined: Mar 2018
06-17-2019, 02:26 PM
(This post was last modified: 06-17-2019, 02:35 PM by win.)
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.
I have a question
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.
Posts: 12,071
Threads: 140
Joined: Dec 2002
In QM3 only C#.
PowerShell is much slower. Executes in new process. To measure code speed use functions PerfFirst, PerfNext, PerfOut.
Posts: 726
Threads: 99
Joined: Mar 2018
06-17-2019, 10:51 PM
(This post was last modified: 06-18-2019, 12:21 AM by win.)
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.
Macro QM Run PS Code
;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
;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()
}
}
Posts: 12,071
Threads: 140
Joined: Dec 2002
06-18-2019, 04:45 AM
(This post was last modified: 06-18-2019, 04:45 AM by Gintaras.)
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
;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";
}
}
Posts: 726
Threads: 99
Joined: Mar 2018
How to reference C:\Chilkat\ChilkatDotNet47.dll dll file in C#
Macro Macro14
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();
}
}
Posts: 12,071
Threads: 140
Joined: Dec 2002
Posts: 726
Threads: 99
Joined: Mar 2018
06-18-2019, 08:17 AM
(This post was last modified: 06-18-2019, 08:20 AM by win.)
I didn't find a solution for C#, so I don't know the execution speed of C# code.
Using Chilk powershell code, don't need to make any changes, use it directly.
Posts: 1,336
Threads: 61
Joined: Jul 2006
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();
}
}
Posts: 726
Threads: 99
Joined: Mar 2018
06-20-2019, 05:30 AM
(This post was last modified: 06-20-2019, 05:31 AM by win.)
@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
Posts: 1,336
Threads: 61
Joined: Jul 2006
06-20-2019, 11:54 AM
(This post was last modified: 06-20-2019, 11:56 AM by Kevin.)
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
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();
}
}
Posts: 726
Threads: 99
Joined: Mar 2018
thanks,
Successful, dll needs to be placed in the QM directory, why?
|