ScriptControl Eval

Proctor

New member
Joined
Oct 14, 2020
Messages
4
Programming Experience
Beginner
Hi,
I am somewhat beginner in coding, so please excuse me, if my question is easier.
I wrote an application, to read a text file which includes some variables, like this:

"Text1" + var1 + "Text2"
"Text3" + var2 + "Text4"
"Text5" + var3 + "Text6"

I used the following code:

MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBSCRIPT";
string strAppDir = System.IO.Directory.GetCurrentDirectory();
string strFile = "TextFile.txt";
string tmpFile = Path.Combine(strAppDir, strFile);
string tmpText = File.ReadAllText(tmpFile);

string var1 = "string01";
string var2 = "string02";
string var3 = "string03";

Object result = sc.Eval(tmpText);
MessageBox.Show(result.ToString());

The result is not what I would like to be:
Text1Text2
Text3Text4
Text5Text6
This is what I get in result.

This is what I would like to get
Text1String01Text2
Text3String02Text4
Text5String03Text6
The variables are not included in the result.
Is there any kind of solution, using the Eval function?

Thanks. Appreciate any aswers.
 
The script knows nothing about your VB.NET code. It only knows what's in the script. You need to declare and set those variables in script itself for them to be part of the evaluation.
 
The script knows nothing about your VB.NET code. It only knows what's in the script. You need to declare and set those variables in script itself for them to be part of the evaluation.
Thanks for your reply.
I admit that I don't know much about the Scriptcontrol procedure. Could you give some xample how to declare a variable and evaluate?
Thank you.
 
How would you normally write a VBScript to do what you want to do? That's what you need to do here because that's what you're executing. If you don't know how to write a self-contained VBScript then that's what you need to learn but this is not the place to do that because this is a VB.NET forum, not a VBScript forum. If you want to learn how to replace some placeholders in a String that contains VBScript code then we could help you with that but replacing one value in a String with another value is the same no matter what the String contains.

One of the most important and often overlooked steps in solving a problem is identifying what the problem is. Writing a VBScript is one problem and replacing placeholders in a String in VB.NET is another completely separate problem. You ought to solve the first before you worry about the second and, as I said, a VB.NET forum is not the place for that.
 
How would you normally write a VBScript to do what you want to do? That's what you need to do here because that's what you're executing. If you don't know how to write a self-contained VBScript then that's what you need to learn but this is not the place to do that because this is a VB.NET forum, not a VBScript forum. If you want to learn how to replace some placeholders in a String that contains VBScript code then we could help you with that but replacing one value in a String with another value is the same no matter what the String contains.

One of the most important and often overlooked steps in solving a problem is identifying what the problem is. Writing a VBScript is one problem and replacing placeholders in a String in VB.NET is another completely separate problem. You ought to solve the first before you worry about the second and, as I said, a VB.NET forum is not the place for that.
As I mentioned, I am a beginner with not much knowledge with coding. I am here to ask something that I don't know.
I wrote an application in .NET, but I included the Scriptcontrol to simulate the Eval function.
If you have some other ways to import the text file mentioned, and assign values to variables, please let me know.
Thanks
 
If you expect to be able to execute a VBScript then the first thing you need to be able to do is write a VBScript. This is a C# forum so it is not the place to ask about how to do that. The C# component of this is simply replacing part of a String with another String. If you don't have that String in the first place, the C# part is irrelevant. Until you have that String, i.e. a working VBScript, the C# part is irrelevant. Once you have a working VBScript, then you have a question to ask us. Until then, you don't.
 
Last edited:
Ill try to make it simpler. If you want to execute a VBScript, then you need to write that VBScript. If you want a variable in a VBScript to have a value, you need to assign a value to that variable in the script. How to do that is not a C# question. I know that you wish it was but it's not. Until you have a working VBScript, asking us how to write C# code to run such a script is a moot point.
 
Ill try to make it simpler. If you want to execute a VBScript, then you need to write that VBScript. If you want a variable in a VBScript to have a value, you need to assign a value to that variable in the script. How to do that is not a C# question. I know that you wish it was but it's not. Until you have a working VBScript, asking us how to write C# code to run such a script is a moot point.
This is a simple text file, not VBScript. I would like to read it, and assign values to variables.
But if you don't want to help, thank you for not to help me.
Bye
 
This is a simple text file, not VBScript. I would like to read it, and assign values to variables.
But if you don't want to help, thank you for not to help me.
Bye
I'm afraid that you don't understand your own problem. It IS VBScript. What exactly do you think this is doing:
C#:
sc.Language = "VBSCRIPT";
That explicitly instructs the ScriptControl to interpret the text file you provide as VBScript. I repeat, you need to have working VBScript code in order to execute it. It doesn't matter how much you wish it were otherwise. If you can't write VBScript code that does what you want then you can't execute VBScript code that does what you want. It's not that I don't want to help you. I'm perfectly happy to help you with a C# question. There's a C# question in here but you haven't actually got to the stage that you can ask it because you don't have working VBScript code yet. This forum is not a place to learn how to write VBScript, no matter much you wish it was.
 
It's been years since I've had to deal with IActiveScript. It's niche knowledge that I don't really want to have to try to recall, but a lot of good information can be gleaned from the various Dr. Dobbs articles regarding script hosting.

As noted above, the brute force fix is to just do some string substitution.

The next best solution is to have the script engine read in a VB script string where you set the values, and then after that do continue with what you are doing to execute the user's script.

The most elegant solution is to implement IActiveScriptSite::GetItemInfo() and give back an interface which returns the values of various variables/symbols which the script engine cannot resolve. This is the approach I took many years ago because of the dynamic nature of what we needed in our project.

As an aside, just reading in that text file and just evaluating or executing it would not have made anybody blink an eye back in the early 2000's. Nowadays, this would cause all kinds of security concerns. How do you know nobody injected malicious code in that file?
 
Back
Top Bottom