<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Here&#8217;s another simple code snippet that&#8217;d let you do the same&#8230;<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:blue'>using</span><span style='font-size:10.0pt;
font-family:"Courier New"'> System;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:blue'>using</span><span style='font-size:10.0pt;
font-family:"Courier New"'> Microsoft.Scripting.Hosting;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:blue'>using</span><span style='font-size:10.0pt;
font-family:"Courier New"'> System.Scripting;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:blue'>namespace</span><span style='font-size:
10.0pt;font-family:"Courier New"'> ConsoleApplication1 {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp; <span style='color:blue'>class</span> <span
style='color:#2B91AF'>Program</span> {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:blue'>public</span> <span
style='color:blue'>static</span> <span style='color:blue'>void</span> Main(<span
style='color:blue'>string</span>[] args) {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:#2B91AF'>ScriptEngine</span>
pyEng = <span style='color:#2B91AF'>ScriptRuntime</span>.Create().GetEngine(<span
style='color:#A31515'>&quot;py&quot;</span>);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:blue'>string</span>
testSrc = <span style='color:#A31515'>@&quot;<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:#A31515'>class FooClass:<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:#A31515'>&nbsp;&nbsp;&nbsp;&nbsp; 'A simple test class'<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:#A31515'>&nbsp;&nbsp;&nbsp;&nbsp; def f(self):return 'Hello World'<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:#A31515'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:#A31515'>fooTest = FooClass()<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:#A31515'>def bar(): return fooTest.f();&quot;</span><span
style='font-size:10.0pt;font-family:"Courier New"'>;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:green'>// load script
with bar() function def<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:#2B91AF'>ScriptSource</span>
source = pyEng.CreateScriptSourceFromString(testSrc, <span style='color:#2B91AF'>SourceCodeKind</span>.Statements);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:green'>// Create
scope<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:#2B91AF'>ScriptScope</span>
scope = pyEng.CreateScope();<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; source.Execute(scope);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:blue'>object</span>
fooTest = scope.GetVariable(<span style='color:#A31515'>&quot;fooTest&quot;</span>);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:#2B91AF'>Func</span>&lt;<span
style='color:blue'>string</span>&gt; sayHello = pyEng.Operations.GetMember&lt;<span
style='color:#2B91AF'>Func</span>&lt;<span style='color:blue'>string</span>&gt;&gt;(fooTest,
<span style='color:#A31515'>&quot;f&quot;</span>);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:green'>// Now call
fooTest's object member function 'f'<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='color:blue'>string</span>
result = sayHello();<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>&nbsp;&nbsp;&nbsp; }<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>}<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>

<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> users-bounces@lists.ironpython.com
[mailto:users-bounces@lists.ironpython.com] <b>On Behalf Of </b>Dody Gunawinata<br>
<b>Sent:</b> Saturday, June 28, 2008 7:05 AM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> Re: [IronPython] Call Python function from C#...<o:p></o:p></span></p>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-bottom:12.0pt'>Hi Bob,<br>
<br>
This blog post shall answer all your questions <a
href="http://spellcoder.com/blogs/dodyg/archive/2008/04/15/12273.aspx">http://spellcoder.com/blogs/dodyg/archive/2008/04/15/12273.aspx</a><br>
<br>
Dody G.<o:p></o:p></p>

<div>

<p class=MsoNormal>On Fri, Jun 27, 2008 at 5:51 PM, Bob Rosembob &lt;<a
href="mailto:brosembob@yahoo.com">brosembob@yahoo.com</a>&gt; wrote:<o:p></o:p></p>

<div>

<div>

<div>

<div>

<div>

<div>

<p class=MsoNormal>Hi there,<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>I apologize if this is a duplicate message. I sent this
message last night but don't see it posted. Perhaps I did something wrong and
message got lost. <o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>I need to call Python function from C# to get&nbsp;a value.
When&nbsp;I call the script it executes and calculates some value, then I need
to call some function to get the result. How do I do that? I have the following
snippet of code that seems to execute the script, but I don't know and can't
find on line, how to call a function and get value back.<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>I added references to my project to the following dlls:<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>- IronPython.dlll<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>- IronMath.dll<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>In the code I'm using the following:<o:p></o:p></p>

</div>

<div>

<p><span style='font-size:10.0pt;color:blue'>using<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt'>IronPython.Runtime;<span
style='color:blue'> <o:p></o:p></span></span></p>

<p><span style='font-size:10.0pt;color:blue'>using<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt'>IronPython.Runtime.Types;<span
style='color:blue'> <o:p></o:p></span></span></p>

<p><span style='font-size:10.0pt;color:blue'>using<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt'>IronPython.Runtime.Operations;<span
style='color:blue'> <o:p></o:p></span></span></p>

<p><span style='font-size:10.0pt;color:blue'>using<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt'>IronPython.Hosting;<span
style='color:blue'> <o:p></o:p></span></span></p>

<p><span style='font-size:10.0pt;color:blue'>using<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt'>IronPython.Modules;</span> <o:p></o:p></p>

<p>&nbsp;<o:p></o:p></p>

<p>Here is the code:<o:p></o:p></p>

<p>&nbsp;<o:p></o:p></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:#2B91AF'>PythonEngine</span><span
style='font-size:10.0pt'> engine = <span style='color:blue'>new</span> <span
style='color:#2B91AF'>PythonEngine</span>(); <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:#2B91AF'>EngineModule</span><span
style='font-size:10.0pt'> module = engine.CreateModule(<span style='color:#A31515'>&quot;Test&quot;</span>,
<span style='color:blue'>true</span>); <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:blue'>try</span><span
style='font-size:10.0pt'> <o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>{<o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>&nbsp;&nbsp;&nbsp; engine.ExecuteFile(<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:#A31515'>@&quot;Test\Test.py&quot;</span><span
style='font-size:10.0pt'>, module); <o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>}<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:blue'>catch</span><span
style='font-size:10.0pt'> (<span style='color:#2B91AF'>Exception</span> ex) <o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>{<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:#2B91AF'>&nbsp;&nbsp;&nbsp;
Console</span><span style='font-size:10.0pt'>.WriteLine(<span style='color:
#A31515'>&quot;Failed to execute <a href="http://test.py/" target="_blank">Test.py</a>
file&quot;</span>); <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;color:blue'>&nbsp;&nbsp;&nbsp;
return</span><span style='font-size:10.0pt'>; <o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>}<o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>&nbsp;<o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>//call the function????<o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>&nbsp;<o:p></o:p></span></p>

<p>So how do I do the call to&nbsp;the function?<span style='font-size:10.0pt'><o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>&nbsp;<o:p></o:p></span></p>

<p>Thanks for your help.<span style='font-size:10.0pt'><o:p></o:p></span></p>

<p>Bob<span style='font-size:10.0pt'><o:p></o:p></span></p>

<p><span style='font-size:10.0pt'>&nbsp;<o:p></o:p></span></p>

</div>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

</div>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

<p class=MsoNormal style='margin-bottom:12.0pt'><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com"
target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><o:p></o:p></p>

</div>

<p class=MsoNormal><br>
<br clear=all>
<br>
-- <br>
<a href="http://nomadlife.org">nomadlife.org</a><o:p></o:p></p>

</div>

</body>

</html>