How can I code "IF Studio"?
Studio's Output window is interactive, and code can ask questions there if it is a Studio environment. How do I check for that?
Comments
While you are in terminal $IO="|TRM|:|10468" or similar
from Studio it is "|TCP|1972|11096" with 1972 as Superserver port
It's one of the differences
Thanks, Robert!
Unfortunately, that does not work. It does indeed contain 1972 when code is not running and does not contain it once it is running. Below is a copy-paste from the Output window.
w $I
|TCP|1972|50524
Debugger executing 'patchTM^FUNCT'
Executing patchTM^FUNCT
w $I
|TCP|59056
Debugger stopped.
At least, there are four different ways to get that info
ClassMethod StudioTest()
{
write"Call from Studio1: ",..InStudio1(),!
write"Call from Studio2: ",..InStudio2(),!
}
/// Is the invocation from the Studio?ClassMethod InStudio1()
{
for i=$st:-1:1if$st(i,"PLACE")["%Studio.General." ret 1
ret 0
}
/// Is there a connection to Superserver?ClassMethod InStudio2()
{
set port=1972// see iris.cpf, section [Startup], DefaultPort=...quit$p($v(-1,$j),"^",3)[("|TCP|"_port_"|")
}
Compiling routine DC.Util.1
Compilation finished successfully in 0.024s.
do##class(DC.Util).StudioTest()
Call from Studio1: 1
Call from Studio2: 1
ICINDY:USER>; from PuTTY -------------------
ICINDY:USER>do##class(DC.Util).StudioTest()
Call from Studio1: 0
Call from Studio2: 0
The other two methods are: checking for the presence of
a) a dedicated variable or
b) a dedicated object
but both require a usage of undocumented functions
Thanks, Julius!
Unfortunately, this code does not work in Cache 2017 either. The return values when run in Studio are 0. 1972 apparently does not work for the same obscure reasons Robert's suggestion did not work.
Port 1972 is the default port, your actual port may be different. That's why I added a comment to the answer above. Check the parameter file of your installation:
for IRIS : see iris.cpf, section [Startup], DefaultPort=...
for Cache: see cache.cpf, section [Startup], DefaultPort=...
1972 is the default port, checked. InStudio2 does no work in our environment.
I'm just curious, what do you get, if you type write $view(-1,$job) in the output window of Studio?
See my screenshot, red=my input, yellow=Studio output
Using your stack idea, code below seems to work both on Iris and Cache 2017:
When run from Studio $STACK(0,"PLACE")["DebugStub" is 1. The same is 0 in the terminal
More info, please. What is $stack(0,...) supposed to represent?
I am not sure what exactly does it represent but here is the full value when code is run from Studio 2017: $STACK(0,"PLACE")=zDebugStub+30^%Debugger.System.1 +N. When the same code is run from Terminal the value is $STACK(0,"PLACE")=@ +N1. N and N1 are integers, not sure if always the same.
I only have back to Cache 2018, not 2017, but can you try:
set myproc = ##class(%SYS.ProcessQuery).%OpenId($J)
if myproc.ClientExecutableName = "CSTUDIO.EXE"{
//You are in Cache Studio
}
else{
//You are not in Cache Studio
}Your lines can be shortened to
if$system.Process.ClientExecutableName($j) = "..." { ... } else { ... }justmy2cents
In my case ClientExecutableName is empty when run from Studio. I also run ZW myproc but did not see anyting definitive inside.
Check the result outputs of the following code from Studio and from Terminal:
<FONT COLOR="#0000ff">w $zu</FONT><FONT COLOR="#000000">(67,13,</FONT><FONT COLOR="#0000ff">$j</FONT><FONT COLOR="#000000">)</FONT>
@Vitaliy Serdtsev empty in both cases
Strange. I just checked on Caché Studio 2014.1.5 and Caché 2014.1.5 (logged in under superuser) - output
CSTUDIO.EXE
if ($this = "%Studio.General") { ... }