Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how to find packages when using NETLINK

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54465] Re: [mg54343] how to find packages when using NETLINK
  • From: Todd Gayley <tgayley at wolfram.com>
  • Date: Mon, 21 Feb 2005 03:44:36 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

At 01:31 AM 2/19/2005, Andreas Tanner wrote:
>Hi,
>Mathematica doesn't find my packages when I call it from Visual C#. In
>fact, the Path variable returns "Null". Here is the code I try:
>
>##############
>System.Threading.Thread.CurrentThread.CurrentCulture = new
>System.Globalization.CultureInfo("en-us");
>
>IKernelLink ml = MathLinkFactory.CreateKernelLink("-linkmode launch
>-linkname 'c:\\program files\\wolfram
>research\\mathematica\\4.2\\mathkernel'");
>
>ml.Evaluate("$Path");
>ml.WaitForAnswer();
>Console.WriteLine(ml.GetObject().ToString());
>###############
>
>this throws a System.NullReferenceException.


Andreas,

Are you aure that is the exact code that gives you a 
NullReferenceException? When I run it, it doesn't throw an exception (nor 
should it), but it doesn't do what you want, either. Your program is 
missing a call to WaitAndDiscardAnswer() right after the CreateKernelLink() 
call. WaitAndDiscardAnswer() reads and discards the InputNamePacket that 
the kernel will send when it is first launched. Without that call to 
WaitAndDiscardAnswer(), your WaitForAnswer() call after Evaluate("$Path") 
stops at the initial InputNamePacket, and then ml.GetObject().ToString() 
returns "In[1]:=" instead of the result, which is still waiting on the link.

You probably don't want to call GetObject().ToString() either, since the 
default .NET implementation of ToString() won't do anything particularly 
useful for an array of strings (which is what you will get from reading the 
result of $Path when you fix the other problem). If you want a way to print 
out arbitrary Mathematica expressions in InputForm, read them as Exprs instead:

     Console.WriteLine(ml.GetExpr());  // .NET automatically calls ToString().


>I also tried to set the Path via
>###############
>string s = "$Path=Flatten[{\"C:\\Documents and Settings\\Andreas
>Tanner\\My Documents\",  $Path}]";
>                         ml.Evaluate(s);
>###############
>but this doesn't work either, that is, a package in that directory
>doesn't load via <<MyPackage.
>
>Any hints?
>???


The problem is that you need 4 backslashes for directory separators, not 2:

     string s = "AppendTo[$Path, \"C:\\\\Documents and Settings\\\\Andreas 
Tanner\\\\My Documents\"]";

Remember that if you were to type this line directly into Mathematica you 
would use 2 backslashes. But the C# compiler will remove one level of 
quoting when it parses this string literal, so you have to start with 4 to 
have 2 left by the time the string gets to Mathematica. Another solution is 
to use the "verbatim string literal" syntax in C#, which starts with an @:

     string s = @"AppendTo[$Path, ""C:\\Documents and Settings\\Andreas 
Tanner\\My Documents""]";

But what is even easier is to just use / instead of \ for the directory 
separator. Mathematica can handle this just fine:

     string s = "AppendTo[$Path, \"C:/Documents and Settings/Andreas 
Tanner/My Documents\"]";

This saves a lot of headaches with counting backslashes.


Todd Gayley
Wolfram Research


  • Prev by Date: Re: Bug Report - Two numerical values for a same variable
  • Next by Date: Re: Re: Re: Re: Re: Bug Report - Two numerical values for a same variable
  • Previous by thread: how to find packages when using NETLINK
  • Next by thread: Show command trims bitmap edges? (newbie)