<p>更新-我写了一个更通用的例子(加上一个指向包含整个VS2008项目的zip文件的链接)作为我博客上的条目<a href="http://oldschooldotnet.blogspot.com/2011/04/scripting-ccli-with-ironpython-visual.html" rel="nofollow" title="here.">here.</a></p>
对不起,我玩得太晚了,但这里是我如何将IrPython集成到VisualStudio 2008 .NET 3.5中的C++应用程序中。(实际上C/C++混合模式应用程序)</P>
<p>我为用汇编编写的地图制作应用程序编写附加组件。API被暴露,因此可以编写C/C++附加组件。我将C/C++与C++和CLI混合。本例中的一些元素来自API(例如XPCALL和CmdEnd()-请忽略它们)</p>
<pre><code>///////////////////////////////////////////////////////////////////////
void XPCALL PythonCmd2(int Result, int Result1, int Result2)
{
if(Result==X_OK)
{
try
{
String^ filename = gcnew String(txtFileName);
String^ path = Assembly::GetExecutingAssembly()->Location;
ScriptEngine^ engine = Python::CreateEngine();
ScriptScope^ scope = engine->CreateScope();
ScriptSource^ source = engine->CreateScriptSourceFromFile(String::Concat(Path::GetDirectoryName(path), "\\scripts\\", filename + ".py"));
scope->SetVariable("DrawingList", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingList::typeid));
scope->SetVariable("DrawingElement", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingElement::typeid));
scope->SetVariable("DrawingPath", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingPath::typeid));
scope->SetVariable("Node", DynamicHelpers::GetPythonTypeFromType(AddIn::Node::typeid));
source->Execute(scope);
}
catch(Exception ^e)
{
Console::WriteLine(e->ToString());
CmdEnd();
}
}
else
{
CmdEnd();
}
}
///////////////////////////////////////////////////////////////////////////////
</code></pre>
<p>如您所见,我向IronPython公开了一些对象(DrawingList、DrawingElement、DrawingPath和Node)。这些对象是我创建的C++对象,它们将“事物”暴露给了iRyPython。在</p>
<P>当C++调用/CLI源-gt;执行(范围)行时,唯一的Python线
跑步是图纸列表.RequestData. 在</p>
<p>RequestData接受委托和数据类型。在</p>
<P>当C++执行CLI代码时,它调用指向
函数“diamond”</p>
<p>在函数diamond中,它通过调用
DrawingList.RequestedValue()呼叫图纸清单.附录(dp)添加
应用程序可视化数据库的新元素。在</p>
<p>最后是DrawingList.EndCommand()告诉FastCAD引擎
清理并结束插件的运行。在</p>
^{pr2}$
<p>我希望这就是你想要的。在</p>