Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSScript-related function generates an EXE.
#1
Running the macro CSreplace_T in QM works fine, but after compiling it into an EXE, the following error occurs.

Warning (RT) in <open "CSreplace /41">CSreplace:  script empty.    <help #IDP_OPT>?
Error (RT) in <open "CSreplace /41">CSreplace:  0x80131600, 
    Public class  not found.    <help #IDP_ERR>?


Is there any way to resolve this issue?
Thanks in advance for any suggestions and help.

Function CSreplace_T
Code:
Copy      Help
out

str s =
;aaa 123 ccc

str p =
;\d+

str t =
;BBB

mes CSreplace(s, p, t)

Function CSreplace
Code:
Copy      Help
function~ str's str'p str't

str code
ret CsFunc(code s p t)

#ret
using System.Text.RegularExpressions;

string replace(string s, string p, string t)
{
,Regex rx = new Regex(p);
,return Regex.Replace(s, p, t);
}

Using the above approach to create C# functions is very concise. For example:
many `using` directives can be omitted, there's no need to create a class, function modifiers can be left out, and so on.
However, the only issue is that generating an `.exe` results in an error. I noticed the following in the help documentation:

1. When the `code` variable is empty, the code after `#ret` is used automatically.
2. The `csscript` class supports `.exe`.
#2
Compiler does not add text after #ret to exe. But adds strings.

Function CSreplace
Code:
Copy      Help
function~ str's str'p str't

str code=
;using System.Text.RegularExpressions;
;
;string replace(string s, string p, string t)
;{
,;Regex rx = new Regex(p);
,;return Regex.Replace(s, p, t);
;}
ret CsFunc(code s p t)
#3
Thanks for your help.
When placing the code into a variable, the generated EXE runs successfully, but the syntax highlighting is lost.
Is it possible to automatically assign the code below #ret to the code variable when generating the EXE?
That would indeed make things much more convenient.
#4
No.

Move C# code to a cs file, and edit in a C# editor.

Function CSreplace
Code:
Copy      Help
function~ str's str'p str't

str code.getfile(":1 C:\path\CSreplace.cs")
ret CsFunc(code s p t)

In "Make exe" dialog check "Add files".
#5
With this method, the code is stored in the resource, not in a variable. This seems insecure — can the code be viewed using tools?
#6
The same. Strings and resources are visible in exe using tools.
#7
Thanks again for your reminder.
Now I've generated the C# code as an external DLL file and used the `Call` function to invoke it — it works well on Windows 10.

However, on some Windows 7 systems where .NET 4 is not installed, I set the runtime option (`net_clr_version = "v2.0.50727"`), but it doesn't work — it still reports an error saying the .NET 4.0 assembly could not be loaded.

Also, how to catch this runtime error? I'd like to execute a function to install the .NET 4 runtime when this error occurs.

Macro Macro7
Code:
Copy      Help
out 
RTOPTIONS xrt; xrt.net_clr_version="v2.0.50727"; RtOptions 32 xrt

str s="aaa 123 ccc"

str p=
;\d+

str t="BBB"

CsScript x.Load(":10 $desktop$\spt.dll")

mes x.Call("replace" s p t)
#8
Code:
Copy      Help
if dir("$windows$\Microsoft.NET\Framework\v4.*" 1)
,out ".NET 4.x is installed"

Or try to compile the dll for .NET 3.5 and use net_clr_version="v2.0.50727".
#9
I don't have Visual Studio or Visual Studio Code installed on my computer.
I compiled my DLL on a Windows 10 system using the command x.Compile(code, "$desktop$\spt.dll", 1|0x300) in QM, which is very convenient.
However, there doesn’t seem to be any option in compilerOptions to control the runtime version (e.g., .NET 3.5).
Does this mean I must install Visual Studio in order to compile for .NET 3.5?
#10
https://chatgpt.com/share/6841184e-39e8-...2bc3a3cc0a
#11
thank you so much!

Using C# in QM is mainly to address some issues with QM's regular expressions. With the method mentioned above, solving these problems has become much more convenient.


Forum Jump:


Users browsing this thread: 1 Guest(s)