Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to embed interpreter 1

Status
Not open for further replies.

mbaranski

Programmer
Nov 3, 2000
421
0
0
US
OK, I've got a VC++ application, and I need to do some data conversions.

Now, I'm thinking I want to have a script file, call it x.js. When the data conversion is going to happen, I want my VC++ app to read in x.js, set a variable called inputValue to the original data

x.js will modify inputValue, and set a variable called outputValue. The VC++ app will then extract the outputValue variable, and have the converted data.

Can anyone point me at how to do this in VC++. I've done it with java/jython before, but I'm sort of new to this.
 
Get the script and the C++ app to both read/write the values from another text file. The hard part might be to synchronize them so they know when to read or write to the file (i.e. so they don't step on each other's toes).
 
Have you considered doing the .js stuff in C++ directly?

/Per
[sub]
www.perfnurt.se[/sub]
 
perfnurt - The idea is to be able to change the data translation without a re-build and re-install of the service

cpjust - Yes, I've already got that, but how do I run it.

Consider the psudo-code:


JavaScriptInterpreter *interp = new JavaScriptInterpreter();
interp->SetScript(myScript);
interp->SetVariable("inputValue", "42");
interp->Run();
return interp->GetVariable("outputValue");

This is what I want to do.

Mike.
 
As for setting the variable one simple approach could be to make a simple preprocessing of the .js in the C++ app, like
Code:
// scriptTemplate.js
// Variables modified my the C++ app:
$VARS$

function blabla()
{
  // ...
}
// etc
Code:
  CString s;
  readFileIntoString("scriptTemplate.js",s);
  
  CString vars;
  // Build the vars section. Pseudo code:
  for each variable v
  {
    vars+="var "+v.name + "=" + v.value +'\n'
  }

  s.Replace("$VARS$",vars);
  
  writeStringToFile("script.js",s);

  // Call the parser, could for example internally be 
  // ShellExecute(0,"open","script.js",...); or how you do it today...
  executeScript("script.js");

As for getting result back, I'm a bit stumped...I guess you could let the C++ app provide an IDispath derived COM interface the .js could interact with (through WScript.CreateObject("objName")) but now it's getting quite complicated...

Or you could actually pipe the script to another C++ app that have some clever way of communicating (could be COM, shared memory or whatever) with the original C++ app, like
Code:
  cscript script.js | someApp.exe



/Per
[sub]
www.perfnurt.se[/sub]
 
Or as cpjust mentioned, let the .js write output to some dedicated file.

The C++ app could register for detecting changes to the file, search MSDN/Google on the subject

Some relevant keyword to use in search:
Code:
RegisterWaitForSingleObject
FindFirstChangeNotification
FILE_NOTIFY_CHANGE_LAST_WRITE

/Per
[sub]
www.perfnurt.se[/sub]
 
The correct way of executing scripts and interacting with them are the recommended by wendors ones. In MSDN is a family of IActiveScript* interfaces. This is about script hosts and script engines. If you have MSDN installed you could use this link:
ms-help://MS.MSDNQTR.2003FEB.1033/script56/html/scripting.htm

Ion Filipski
1c.bmp
 
I guess I should have mentioned that this is all managed C++, and it does not look like the IActiveScript stuff is managed...
 
anyway, you may read microsoft recommendations for scripting for .NET. ActiveX is portable. But if you wouldn't use ActiveX you could use IVsaEngine, IVsaSite...
Look this MSDN link:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dnclinic/html/scripting06112001.htm

So, in the MSDN tab "look for" you will enter "Script for the .NET Framework, compared to Windows Script" and you will see how to use script with .NET. I guess in MC, VB.NET and C# is almost the same. Just different syntax.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top