Is there a way to write an statement that checks to see if my application is being ran from the VB IDE or if it is running on its own as an exe file?
Printable View
Is there a way to write an statement that checks to see if my application is being ran from the VB IDE or if it is running on its own as an exe file?
OK, so can this just not be done or was my question not clear?
What I'm lookinng for is something like:
Does that make sense? Is this just a dumb question?Code:If RunningFromIDE Then
MsgBox "Running From IDE"
Else
MsgBox "Running EXE"
End If
Any knowledgable response is appreciated. :)
I believe that even if you run your app from within VB, VB still compiles it before running it.
Therefore, I don't think you'll be able to distinguish between the two.
Why would you want to anyway?
I don't think there is any way to check it because VB compilers the actual program into an EXE and then runs it. So there is no way to check if the program's "runner" is vb IDE or it's running itself.
I am just curious why would need this kind of useless thing?
if you are running VB in NT then you can check for the process list and check if it is 'Your project Name - Visual Basic' or it is just 'your Project Name'.
But i am also searching for code to get the process list.
Please post it if you find it.
Try rephrasing your question, show me exactly what you mean.
Your question was not completely clear to me, but here is a general answer...
There are so many different things you could mean.
Like there are in-process and out-of process applications such
as COM objects and ActiveX .dll's, etc... The "in-process" application uses some of the same resources as the application using it, the out-of-process is more independent.
There are many things you could mean here.
There are ways to tell in which state an application is running such as an OLE/DDE process, ActiveX, or .exe thread. There might be an easier way then API calls, but that is the only one I can think of. There may be a way to also tell exactly what program or function launched the application (whether it was launched via Shell API or otherwise)... I am not sure about the latter.
The only way I know how to get detailed information about a process and threads (the overall application) is with intercepting window's messages or using API calls to determine the current application's state. I do not remember which API calls to use, but there is a good listing of them on this link. This type of information is probably found in an API book also, such as one of Daniel Appleman's API guides.
http://www.vbapi.com/ref/index.html
VB Code:
Private Function InIDE() As Boolean On Error Resume Next Debug.Print 1 \ 0 InIDE = Err.Number End Function
Sorry my response took so long. The reason that I want to know is because I would like to skip certain procedures that run in my program when I am testing it on my developer machine. So far I have just been commenting the unwanted procedure calls during testing, but if I could put in code to check, that would be nice.
maybe u could use conditional compilation....
Quote:
MSDN
USING CONDITIONAL COMPILATION
You can use conditional compilation to selectively run blocks of code. The following sample macro uses conditional compilation:
VB Code:
Sub Test() #Const Debugging = 1 Dim Name As String: Name = "Nancy" ' If you are debugging, change the Debugging constant to 0. #If Debugging = 0 Then ' This debug statement is not executed unless the Debugging ' constant is equal to zero. Debug.Print Name #End If Name = "Kerry" End Sub
The behavior of the #If...Then...#Else conditional compilation directive is the same as the If...Then...Else statement. However, code that is excluded during conditional compilation is completely omitted from the final executable file; so, using conditional compilation has no size or performance disadvantages.
I appreciate the responses from everyone.
Nucleus; I see what you are doing, This works? I can't test it yet myself cuz I'm not on my programming 'puter.
CoderGuy, abhid; Good information from both. If there is a way to do this with API that would be preferred.
Just a little additional info. The program that I am writing is a utility that simplifies centralizes the changing of a number of hard-to-access O/S settings. I would like bypass some of the startup procedures that are ran during testing so they are not ran every time I run a test. Some procedures are only intended to be ran once during boot-up so running them repeatedly during normal operations has caused a couple problems.
Yep
seems that u'r sig tells the truth Nucleus
nice :)
Thanks Peet but where is your sig? We demand some Norweigan aphorisms :)
Ok I understand what you are trying to do, but I still do not understand in which way you need to determine the state of the application.
I am a VB programmer with 7 years experience...
So here's my 7 years of experience thoughts (for the penny it's worth)
First off if you know which all things are giving you problems it is so so easy to avoid running something other then when it gives you problems.
Just make one Public Integer Array or a Long Array if needed
in the General Declarations Section
Then use an in-code ID for each setting you change or program you run or anything for that matter that needs to not be ran again... some kind of unique ID such as numbers -- like below I used the # 21...
Here's what I mean by coding it:
'In General Declarations Section
Public DontRunAgain() as integer ' dynamic integer array
Private Function Fun_CheckIfAlreadyRan(InCodeID as Integer) _ as Boolean
Dim X as Integer
Dim DONTRUN as Boolean
For x = 1 to uBound(pubAryDontRunAgain)
If pubAryDontRunAgain(x) = InCodeID then
DONTRUN = True
else
End If
Next x
If DONTRUN = True then
Exit Sub
Else
Redim Preserve pubAryDontRunAgain(Ubound + 1)
pubAryDontRunAgain(Ubound+1) = 21
End If
The Redim Preserve part redimensions the array to + 1 without erasing previous contents. With this code you can literally keep track of hundreds or even thousands of things. To get really fancy make it 2 dimensional and then make the 2nd dimension store something about the state of the item you wish not to run again.
So maybe some items need to be run multuiple times or during intervals, well store number of times or interval in 2nd dimension. Lots of ways to do this. Then subtract number by 1 each time you run something or whatever.... Depends what you need...
Are you saying you want to be able to tell if your VB application launched a program or if the program running was already running from the OS or current user?
For instance, your running your VB program and you need to access scandisk, well your program needs to check first if scandisk is already running ? Something like that?
Off that subject, if I were righting a program like that one of the first things I would add is error logging for my own developing purposes to make it easier on me as a developer.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
You might even need advanced error logging such as knowing your program paused too long. When a VB app gets too far behind in thread priority, it can cause an error when trying to interact with semi low-level OS settings or otherwise. Meaning, make sure your program remains responsive.
I am kind of bored so I am giving you much more information then normal. You might need to make API Thread Calls forcing your application to gain thread priority over other ones. You can also force close applications if they are affecting your program.
----CoderGuy
I once spent a year writing a program, but then I downloaded one that was just as good as the the one I wrote in 2.5 minutes!
Have fun!
The code I posted wont work as is, but it's pretty close to correct.
You need to make the function return true or false..
So instead of the Exit Sub Statement, do something like this
'''O yah and a few other corrections thrown in
Dim ub as Integer ' to store the upper bound of the array
ub = Ubound(pubAryDontRunAgain)
If DONTRUN = True then
'In otherwords it's been ran once already
'or you do not want to run it at this time
Goto EndOfCode
Else 'Redim the Array to store the UniqueID so next time you
dont run it after it is ran this time
Redim Preserve pubAryDontRunAgain(ub + 1)
pubAryDontRunAgain(ub +1) = 21
End If
''''CODE TO RUN SOMETHING GOES HERE
EndOfCode:
Fun_CheckIfAlreadyRan = DontRUN
So now say I am making a setting that I only want to change during bootup... Well I dont want to have to burn brain cells trying to figure out how to make it run once... Easy... Give it an IncodeID and pass the IncodeID to the Fun_CheckIfAlreadyRan()
Such as:
Private Sub Sub_SetThis_OS_SettingOnlyOnce()
Dim HasThisSettingRanYet as Boolean
Dim InCodeID as Integer
InCodeID = 5 ''just picking numbers here for unique ID's
HasThisSettingRanYet = Fun_CheckIfAlreadyRan(InCodeID)
If HasThisSettingRanYet = True then
Exit Sub
Else
ALL THE CODE TO SET SOME SETTING OR RUN SOMETHING
End Sub
HOPE THIS HELPS....
IF NOT I BURNED AT LEAST 2.1509837242 BRAIN CELLS
FYI: Take the little underscore (_) out of the function name, I did that because the formatting in this newsgroup window throws me off...
Hah you probably already knew that
Still a bug or two (mostly aesthetic)
But the bugs are pretty obvious....
The functionality is all there
One more thing... If you missed the point... You may be thinking well why even make an array, just make it a single static Boolean or something...
No there is a reason for the array, flexibility...
Watch this.. Say you need to store multiple conditions for each item... Well instead of using the array in single incremental storage units, use it in larger increments per ITEM
in otherwords.... Redimension it + 5 everytime or something.. Then store 5 ID's for something. You could have a DontRunID, a LastErrorID, a RanHowManyTimesID, so forth and so forth.
However, Logic Dictates the following rule...
However you use the array it has to be consistent when dimensioning (if you use 5 slots per item, you always have to use 5 slots for every item... unless you used a Unique End of Data Number in the element of the array following the data for that particular ID OR UNLESS YOU ARE VERY DISCIPLINED AND CONSISTENT WITH YOUR NUMBERING SCHEMES)
Such as:
Const EndOfData = 99999999
Dim LocalArray(1 to 5) as Integer
''''First Element stores whether or not to run me at all
''''Second Element stores how many more times to run me
''''Third Element stores whatever whatever and so on and so on
Then make LocalArray = the Public Array with a For Loop
The first element marking the start of the valid data that particular item uses would always have to be the unique InCodeID... The next 4 elements could store anything you wanted
OK I'm DONE
Just showing you optimized ways, REAL OPTIMIZED, of keeping track of things
Remember, Most In-Code VB stuff is faster then even calling an API, because most API functions do way way more then you need. Try to keep it simple and have fun!
Nucleus,
Your Code is Absolutely BRILLIANT in its simplicity!
We should have yearly awards for that! Anyways, I've been looking for something like that for a while now, too. So Thanks!
-Lou
CoderGuy,
You should be nesting your code in
[vbcode]
Your Code Here
[/vbcode]
That way, it'll be easier to understand the complexity being presented in your posts.
-Lou
hehe.. how about this :Quote:
Originally posted by Nucleus
Thanks Peet but where is your sig? We demand some Norweigan aphorisms :)
Kvinnetårer er ei mektig vasskraft trass i en fallhøgd på berre 1.65 meter
?
any good ?
Nucleus, thanks man. Your code works perfectly. :)
CoderGuy, thanks for your input, I like your ideas and I appreciate your responses, but simplicity is won this bout.
Thanks again. :)
Damn, I don't have a Norweigan to English Translator but there are some funky characters in Norweigan; so Peet can you step up to the plate?Quote:
Originally posted by peet
hehe.. how about this :
Kvinnetårer er ei mektig vasskraft trass i en fallhøgd på berre 1.65 meter
?
any good ?
Nucleus/CargoBay:
This code does not do anything other then just always return false unless you put code between "on error resume next"
and InIde and an error is raised in between. I guess that was the point? This seems to be too obvious to be asked as a question. That is like me asking how do I make a function return a value, ummmm but if you say so... This kind of stuff is in the help file, why ask questions that are in the help file?
I guess I thought you were looking for a way to do something else.
Private Function InIDE() As Boolean
On Error Resume Next
InIDE = Err.Number
End Function
Im surprised no one has seen that code before. The first time i saw it was a few months ago, i Tygur posted a function that is essentially the same as Nucleus's one. Nonetheless, its a brilliant way of doing it.Quote:
Originally posted by NotLKH
Nucleus,
Your Code is Absolutely BRILLIANT in its simplicity!
We should have yearly awards for that! Anyways, I've been looking for something like that for a while now, too. So Thanks!
-Lou
???Quote:
Originally posted by CoderGuy
Nucleus/CargoBay:
This code does not do anything other then just always return false unless you put code between "on error resume next"
and InIde and an error is raised in between. I guess that was the point? This seems to be too obvious to be asked as a question. That is like me asking how do I make a function return a value, ummmm but if you say so... This kind of stuff is in the help file, why ask questions that are in the help file?
I guess I thought you were looking for a way to do something else.
Private Function InIDE() As Boolean
On Error Resume Next
InIDE = Err.Number
End Function
Well, I don't want to misplace credit, So, Good Job, Tygur!Quote:
Originally posted by nishantp
Im surprised no one has seen that code before. The first time i saw it was a few months ago, i Tygur posted a function that is essentially the same as Nucleus's one. Nonetheless, its a brilliant way of doing it.
Its really an interesting method.
CoderGuy, You seem to miss the major element, the Debug.Print 1 \ 0.
When the module is compiled into an executable, the method calls on the Debug object are omitted, so the Debug.Print Line which is raising the Division by Zero Err number {err.num = 11 when IDE} in the Development Environment, when compiled to .EXE, is dropped out entirely, hence Err.Num = 0 {When EXE}.
Applying the Err.number to the Boolean is just a simple way of evaluating 0 as false, and any other number as True.
The way i do it is to set a command line parameter of "test" in the IDE, which will not be there if you are running from the exe. you can then just read the command line
I personally would do it the Microsoft way ; they're the ones who wrote the language ;)
Actually similar code has been posted by many other members of the forum much earlier than Tygur. Joacim as well as Martin Liss posted similar versions last year, although each algorithm differs slightly. To me it is not ground breaking code, but very useful nonetheless and better than MS suggested solution.Quote:
Originally posted by NotLKH
Well, I don't want to misplace credit, So, Good Job, Tygur!
Its really an interesting method.
CoderGuy, You seem to miss the major element, the Debug.Print 1 \ 0.
When the module is compiled into an executable, the method calls on the Debug object are omitted, so the Debug.Print Line which is raising the Division by Zero Err number {err.num = 11 when IDE} in the Development Environment, when compiled to .EXE, is dropped out entirely, hence Err.Num = 0 {When EXE}.
Applying the Err.number to the Boolean is just a simple way of evaluating 0 as false, and any other number as True.
You are complex.
Check the app.path if its in the "c:\program\devstudio\vb\"
Code:if app.path = c:\program\devstudio\vb\" then
msgbox "Running in VB"
else
msgbox "Running from EXE"
end if
Since that is the default compile folder, what if i went to compile and just pressed ok? The EXE would still be in that folder, and would still be compiles. Also, not everyone has VB installed in the same folder.Quote:
Originally posted by carlodin
You are complex.
Check the app.path if its in the "c:\program\devstudio\vb\"
Code:if app.path = c:\program\devstudio\vb\" then
msgbox "Running in VB"
else
msgbox "Running from EXE"
end if
you are all going far too complex, all you need to do is put a command line in the projec t porperties, if the project is running in IDe then the command line is whatever you set if it is not then there is no command line!!! simple as that,
I use it all the time my error checking bypasses if i am running through the IDE else error checking runs as it sould
What is complex about that?Quote:
Originally posted by Nucleus
VB Code:
Private Function InIDE() As Boolean On Error Resume Next Debug.Print 1 \ 0 InIDE = Err.Number End Function
Try it on 99 % of all the other systems in the world. I know I don't have "c:\program\devstudio\vb\" ANYWHERE on my 5 comps. MeThinks THIS method would fail.Quote:
Originally posted by carlodin
You are complex.
Check the app.path if its in the "c:\program\devstudio\vb\"
Code:if app.path = c:\program\devstudio\vb\" then
msgbox "Running in VB"
else
msgbox "Running from EXE"
end if
I see what you are saying now.
However, let me kindly disagree.
I agree the code is intelligent in the fact it is short and accomplishes something, this is a good thing.
The code is bad for a different reason.
Raising errors just to determine a condition is not only
bad coding practice, but it can be buggy as well.
I'm not saying your example would be buggy, but
I am just saying that you do not know exactly what is
going on in the background when you do something like this.
This isn't efficient as anytime an error is generated internally, it is doing much more then just initializing a variable. For instance some errors cause memory refreshes, ECC checks by the OS, and other crazy things. If you knew C++ you would know this.
- also think of the logging problems on any system that has error logging enabled through some third party management utility. This might actually trigger an event or system error log in some back-alley utility. I doubt it would, but none the less - I am just saying I wouldn't go around raising errors just to tell if it's running IDE or .exe.
I would just use a boolean value, much more efficient.
It's a quick and easy solution I guess, but I wouldn't do it.