PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VB - Detect if you are running in the IDE...-VBForums
Results 1 to 24 of 24

Thread: VB - Detect if you are running in the IDE...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092

    VB - Detect if you are running in the IDE...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, _
    4.                                                                                       ByVal lpFileName As String, _
    5.                                                                                       ByVal nSize As Long) _
    6.                                                                                       As Long
    7. Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
    8.  
    9. Private Function InIDE() As Boolean
    10. Dim s As String
    11.  
    12.     s = Space$(255)
    13.    
    14.     Call GetModuleFileName(GetModuleHandle(vbNullString), s, Len(s))
    15.    
    16.     InIDE = (UCase$(Trim$(s)) Like "*VB6.EXE*")
    17.    
    18. End Function
    19.  
    20. 'usage
    21. MsgBox InIDE

    This is for VB6, for versions below 6, change the bolded section to the file name of the VB executable (VB5.exe, etc).
    Last edited by si_the_geek; Sep 27th, 2010 at 08:02 AM. Reason: corrected issue caused by change in code tags
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,241
    Here is an equivalent function, but this does not require any API functions to be declared, and does not need any alterations based on the version of VB you run.
    VB Code:
    1. Option Explicit
    2.  
    3. Function RunningInVB() As Boolean
    4. 'Returns whether we are running in vb(true), or compiled (false)
    5.  
    6.     Static counter As Variant
    7.     If IsEmpty(counter) Then
    8.         counter = 1
    9.         Debug.Assert RunningInVB() Or True
    10.         counter = counter - 1
    11.     ElseIf counter = 1 Then
    12.         counter = 0
    13.     End If
    14.     RunningInVB = counter
    15.  
    16. End Function
    17.  
    18. 'Usage:
    19. MsgBox RunningInVB

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092
    Originally posted by si_the_geek
    Here is an equivalent function, but this does not require any API functions to be declared, and does not need any alterations based on the version of VB you run.
    Your code works, however, if you use my code in an ActiveX Control/DLL, it will return true even if the AX project is not loaded in the IDE, but the project that uses the reference/component is running from VB. This can be useful if you have subclassing or whatever that you want to be able to turn off when not running fully compiled.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: VB - Detect if you are running in the IDE...

    Here is another method (from vbAccelerator):
    VB Code:
    1. ' modular level variable:
    2. Private m_bInIDE As Boolean
    3.  
    4. Public Property Get InIDE() As Boolean
    5.    Debug.Assert (IsInIDE())
    6.    InIDE = m_bInIDE
    7. End Property
    8.  
    9. Private Function IsInIDE() As Boolean
    10.    m_bInIDE = True
    11.    IsInIDE = m_bInIDE
    12. End Function

    I don't like static variables that much (don't ask why) so I think this method is somewhat cool. The only problem is that you need more lines of code.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5
    Lively Member
    Join Date
    Jul 2005
    Posts
    127

    Re: VB - Detect if you are running in the IDE...

    hi,
    you can do the same with

    App.LogMode

  6. #6
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: VB - Detect if you are running in the IDE...

    I think you can like this, but i don't have vb with me to check:

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim mode
    3.     mode = App.LogMode
    4.         If mode = 0 Then
    5.             MsgBox "Running In IDE Version"
    6.         ElseIf mode = 1 Then
    7.             MsgBox "Running in Compiled Version"
    8.         End If
    9. End Sub
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: VB - Detect if you are running in the IDE...

    since it's not been mentioned, here's another way:

    VB Code:
    1. Public InIDE As Boolean
    2.  
    3. Private Sub Main()
    4.   On Error Resume Next
    5.   Debug.Print 1 / 0
    6.   InIDE = Err.Number
    7.  
    8. End Sub

  8. #8
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - Detect if you are running in the IDE...

    I like bushmobiles best, but I've always used:

    ' Declarations:
    VB Code:
    1. Public InIDE            As Boolean
    ' In Form Load or Sub Main:
    VB Code:
    1. InIDE = False               ' for clarity
    2.     Debug.Assert CheckForIDE    ' set value of InIDE to true if in Dev Environment
    ' In form or module
    VB Code:
    1. Prublic Function CheckForIDE() As Boolean
    2. ' identifies app running in DEV environment
    3.     InIDE = True
    4.     CheckForIDE = True
    5. End Function
    Debug.Assert will only run in the IDE. Check InIDE for True = in IDE.
    Last edited by rjbudz; Jan 26th, 2007 at 05:19 PM.

  9. #9
    New Member
    Join Date
    Dec 2010
    Posts
    1

    Wink Re: VB - Detect if you are running in the IDE...

    another way..

    With no API declaration, no outside variable declaration, only a single function, tested in ActiveX DLL!

    VB Code:
    1. Public Function InIDE() As Boolean
    2.     Static i As Byte
    3.     i = i + 1
    4.     If i = 1 Then Debug.Assert Not InIDE()
    5.     InIDE = i = 0
    6.     i = 0
    7. End Function
    Last edited by marcusmiris; Dec 30th, 2010 at 03:23 PM. Reason: I fix a bug...

  10. #10
    Addicted Member *PsyKE1*'s Avatar
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    243

    Re: VB - Detect if you are running in the IDE...

    vb Code:
    1. Public Function InIDE() As Boolean
    2.     InIDE = CBool(App.LogMode = 0)
    3. End Function

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,469

    Re: VB - Detect if you are running in the IDE...

    Quote Originally Posted by *PsyKE1* View Post
    vb Code:
    1. Public Function InIDE() As Boolean
    2.     InIDE = CBool(App.LongMode = 0)
    3. End Function
    LogMode maybe?
    See post #5 & #6 above
    Last edited by LaVolpe; Jan 2nd, 2011 at 12:47 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    Addicted Member *PsyKE1*'s Avatar
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    243

    Re: VB - Detect if you are running in the IDE...

    xD Sorry men
    I had not noticed
    And yes, it should be : LogMode
    Not LongMode! xD

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,469

    Re: VB - Detect if you are running in the IDE...

    I know this is a very old thread, but I like all the different approaches listed above.

    Thought I'd add one more. This applies to usercontrols only

    1) VB only. Not guaranteed to apply to other "IDEs": Word, Access, IE, etc
    2) Your usercontrol is uncompiled and you want to know if whatever is hosting it is in design-view

    Normally, we'd use Ambient.UserMode which would return False indicating the host is in design view, not run-time. However, while designing, let's say you create a second usercontrol (UC) that hosts the first UC. Everything is in design view, neither UC is compiled.

    What's the problem? The 1st UC that's hosted in the 2nd UC will return Ambient.UserMode = True. This may be an issue if you are using that return value to start subclassing or API timers or something else that shouldn't be run in design view. UCs in design view are not guaranteed to get a Terminate event in design view; so subclassing, etc, should be handled carefully. You can test this by placing a debug.print statement in the control's Terminate event, then
    1) On the form, right click on the form and choose menu: update controls. Debug.Prints show
    2) Now open a UC code window & hit carriage return anywhere to force cross-hatching & close the UC
    3) On the form, update controls again: No Debug.Prints show. Any subclassing may be crashing IDE
    4) Compiled UCs get terminate events.

    Solution. If you create a Private Sub Main(), in a bas module, and set your UC to start with Sub Main via the UC properties dialog, then you can use that to your advantage. Add a public boolean variable to that module and in Sub Main() set that variable to True. Where you were testing for Ambient.UserMode, now test for that public boolean's value instead.

    While the UC is uncompiled and host is in design view, VB does not call the Sub Main() of the UC, but the Ambient.UserMode can change as described earlier.

    Simple example
    1) Start a new project (exe type)
    2) Click on IDE menu: File | Add Project. This will create a group project
    3) Choose Active-X Control project
    4) In that Active-X project, add 1 module, set the UC's AutoRedraw to True
    5) In the module, paste this code
    Code:
    Public g_UserMode As Boolean
    Private Sub Main()
       g_UserMode = True
    End Sub
    6) In the UC add this in the Show event
    Code:
    Print "Ambient.UserMode = "; Ambient.UserMode
    Print "g_UserMode "; g_UserMode
    7) In the UC's properties, set the startup to Sub Main. IDE Menu: Project | Project2 Properties
    8) Close all the UC windows opened in design view
    9) Place the UC on the form. You should see that the printed lines return the same value, both in design view & run-time

    When done playing, return to IDE and close all windows. Then
    1) Add 1 more project to the group and make it an Active-X Control also
    2) Change the control's name in the property sheet from UserControl1 to UserControl2
    3) In that new UC, draw the 1st UC in it and close all IDE windows
    4) Open the form in design view and draw the 2nd UC on the form

    You will notice the 2 UC's display different Ambient.UserMode values in design time, but the same in run time
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: VB - Detect if you are running in the IDE...

    similar to Post#1

    Code:
    Public Declare Function InIDE Lib "kernel32" Alias "GetModuleHandleA" (Optional ByVal lpModuleName As String = "vba6.dll") As Long

  15. #15
    Lively Member
    Join Date
    Feb 2015
    Location
    Colorado USA
    Posts
    105

    Re: VB - Detect if you are running in the IDE...

    I know this thread is old but I have run the various techniques for determining whether the code is executing in the IDE or in a compiled form. In each case I put the code into a function that returned True if the code was executing in the IDE and False otherwise. The times below are the number of milliseconds required for each function to run 100 million times.

    Code:
                                                         Un-compiled    Compiled
    vbAdvance "In_IDE" conditional compilation command      4,510 ms      196 ms
    Using Debug.Assert (similar to post #4)                10,490         196
    Using App.Log (see posts #5 & 6)                       12,807      10,340
    Debug.Print a  /0 (see post #7)                        42,520       7,330
    Look for IDE window (see post #16)                    478,000      49,600
    The above techniques are all based on doing something in the IDE that doesn't exist in the compiled code. The vbAdvance method overrides a compilation constant at compile time. The others use Debug.Assert, Debug.Print and App.LogMode. It shouldn't be a surprise that those in the IDE run are slower in the checking code because 1) compiled code runs faster and 2) the code that doesn't exist in compiled mode such as Debug.Print does exists in the IDE and it takes time to run.

    If for no other reason than visual styles, most of us start with Sub Main and we have general initialization code there where we can determine whether we are in the IDE or not and then save that setting for later checking. I use the 2nd technique because I don't have to set a compilation constant and because it performs just as fast in compiled code as technique #1. I do use vbAdvance for a lot of things, just not this.
    Last edited by MountainMan; Nov 26th, 2017 at 10:38 PM. Reason: Adjust for post #16 below & fix some typos.

  16. #16
    Lively Member
    Join Date
    Jul 2015
    Location
    Belarus, Minsk
    Posts
    99

    Re: VB - Detect if you are running in the IDE...

    Another one:

    Code:
    'in a module
    
    Public gIde As Boolean
    
    Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal lhWnd As Long, lProcessId As Long) As Long
    
    Private Sub Main()
        Dim x As Long
        GetWindowThreadProcessId FindWindowA("IDEOwner", vbNullString), x 'search for VB6 IDE window and get process identifier
        gIde = (GetCurrentProcessId = x)
    End Sub
    Last edited by hwoarang; Nov 26th, 2017 at 11:27 AM.

  17. #17
    Addicted Member
    Join Date
    Jan 2015
    Posts
    172

    Re: VB - Detect if you are running in the IDE...

    i have a test of the above method, should be all of them.
    VB Code:
    1. 'modIDE.bas
    2. Private m_bInIDE As Boolean    ' module level variable:
    3.  
    4. Public Declare Function InIDE8 Lib "kernel32" Alias "GetModuleHandleA" (Optional ByVal lpModuleName As String = "vba6.dll") As Long
    5.  
    6. Private Declare Function GetModuleFileNameA& Lib "kernel32" (ByVal hModule&, ByVal lpFileName$, ByVal nSize&)
    7. Private Declare Function GetModuleHandleA& Lib "kernel32" (ByVal lpModuleName$)
    8.  
    9. Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    10. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal lhWnd As Long, lProcessId As Long) As Long
    11. Private Declare Function FindWindowA Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    12.  
    13.  
    14. Public Function InIDE1() As Boolean
    15.     Dim s As String
    16.     s = Space$(255)
    17.     Call GetModuleFileNameA(GetModuleHandleA(vbNullString), s, Len(s))
    18.     InIDE1 = (UCase$(Trim$(s)) Like "*VB6.EXE*")
    19. End Function
    20.  
    21. Public Function InIDE2() As Boolean
    22.     Static counter As Variant
    23.     If IsEmpty(counter) Then
    24.         counter = 1
    25.         Debug.Assert InIDE2() Or True
    26.         counter = counter - 1
    27.     ElseIf counter = 1 Then
    28.         counter = 0
    29.     End If
    30.     InIDE2 = counter
    31. End Function
    32.  
    33.  
    34. Public Property Get InIDE3() As Boolean
    35.    Debug.Assert (IsInIDE())
    36.    InIDE3 = m_bInIDE
    37. End Property
    38. Private Function IsInIDE() As Boolean
    39.    m_bInIDE = True
    40.    IsInIDE = m_bInIDE
    41. End Function
    42.  
    43. Public Function InIDE4() As Boolean
    44.   On Error Resume Next
    45.   Debug.Print 1 / 0
    46.   InIDE4 = Err.Number
    47. End Function
    48.  
    49.  
    50. Public Function InIDE5() As Boolean
    51.     Static i As Byte
    52.     i = i + 1
    53.     If i = 1 Then Debug.Assert Not InIDE5()
    54.     InIDE5 = i = 0
    55.     i = 0
    56. End Function
    57.  
    58. Public Function InIDE6() As Boolean
    59.     InIDE6 = CBool(App.LogMode = 0)
    60. End Function
    61.  
    62. Public Function InIDE7() As Boolean
    63.     Dim x As Long
    64.     GetWindowThreadProcessId FindWindowA("IDEOwner", vbNullString), x 'search for VB6 IDE window and get process identifier
    65.     InIDE7 = (GetCurrentProcessId = x)
    66. End Function

    VB Code:
    1. 'frmIDE.frm
    2. Private Sub Form_Load()
    3.     Me.AutoRedraw = True
    4.     testIDE
    5. End Sub
    6.  
    7. Sub testIDE()
    8.     Dim i&, bool As Boolean
    9.     Const n As Long = 1000
    10.    
    11.     mTimer = 0
    12.     For i = 1 To n
    13.         bool = InIDE1
    14.     Next i
    15.     Print 1 & vbTab & bool & vbTab & Round(mTimer, 3)
    16.  
    17.     mTimer = 0
    18.     For i = 1 To n
    19.         bool = InIDE2
    20.     Next i
    21.     Print 2 & vbTab & bool & vbTab & Round(mTimer, 3)
    22.    
    23.     mTimer = 0
    24.     For i = 1 To n
    25.         bool = InIDE3
    26.     Next i
    27.     Print 3 & vbTab & bool & vbTab & Round(mTimer, 3)
    28.    
    29.     mTimer = 0
    30.     For i = 1 To n
    31.         bool = InIDE4
    32.     Next i
    33.     Print 4 & vbTab & bool & vbTab & Round(mTimer, 3)
    34.    
    35.     mTimer = 0
    36.     For i = 1 To n
    37.         bool = InIDE5
    38.     Next i
    39.     Print 5 & vbTab & bool & vbTab & Round(mTimer, 3)
    40.  
    41.     mTimer = 0
    42.     For i = 1 To n
    43.         bool = InIDE6
    44.     Next i
    45.     Print 6 & vbTab & bool & vbTab & Round(mTimer, 3)
    46.    
    47.     For i = 1 To n
    48.         bool = InIDE7
    49.     Next i
    50.     Print 7 & vbTab & bool & vbTab & Round(mTimer, 3)
    51.  
    52.     For i = 1 To n
    53.         bool = InIDE8
    54.     Next i
    55.     Print 8 & vbTab & bool & vbTab & Round(mTimer, 3)
    56. End Sub

    VB Code:
    1. 'modTiming
    2. Option Explicit
    3.  
    4. Private Declare Function QueryPerformanceCounter Lib "kernel32" (x As Currency) As Boolean
    5. Private Declare Function QueryPerformanceFrequency Lib "kernel32" (x As Currency) As Boolean
    6.  
    7. Dim m_Time As Double          '
    8. Dim m_TimeStart As Currency
    9. Dim curFreq As Currency      
    10. Dim m_TimeFreq As Double
    11.  
    12. Public Property Get mTimer() As Double
    13.     Dim curTime As Currency
    14.     QueryPerformanceCounter curTime
    15.     'Debug.Print curTime; m_TimeStart
    16.     mTimer = 1000 * (curTime - m_TimeStart) * m_TimeFreq + m_Time    'minisections
    17. End Property
    18.  
    19. Public Property Let mTimer(ByVal newValue As Double)
    20.     Dim curOverhead As Currency
    21.     m_Time = newValue
    22.     QueryPerformanceFrequency curFreq
    23.     m_TimeFreq = 1 / curFreq
    24.     QueryPerformanceCounter curOverhead
    25.     QueryPerformanceCounter m_TimeStart
    26.     m_TimeStart = m_TimeStart + (m_TimeStart - curOverhead)
    27. End Property

    Code:
    i have test 3 options in my computer:
    1st: InIDE
    1	True	18.83
    2	True	.452
    3	True	.378
    4	True	2.029
    5	True	.587
    6	True	.523
    7	True	40.376
    8	True	42.668
    
    2nd: InEXE with "Advanced optimization"
    1	False	9.936
    2	False	.262
    3	False	.006
    4	False	.287
    5	False	.007
    6	False	.438
    7	False	38.77
    8	False	40.839
    
    3rd: InEXE without "Advanced optimization"
    1	False	15.369
    2	False	.3
    3	False	.007
    4	False	.32
    5	False	.023
    6	False	.505
    7	False	52.38
    8	False	54.775
    
    it seems in my computer InIDE3 is the best option.

  18. #18
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,610

    Re: VB - Detect if you are running in the IDE...

    Performance is not really an issue when you cache the result in a global/local variable.
    Set it at the start up of the application and then you only have a single boolean variable.

  19. #19
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,585

    Re: VB - Detect if you are running in the IDE...

    InIDE3 FTW!

    You can even remove the "global" m_bInIDE altogether like this

    thinBasic Code:
    1. Public Property Get InIde() As Boolean
    2.     Debug.Assert pvSetTrue(InIde)
    3. End Property
    4.  
    5. Private Function pvSetTrue(bValue As Boolean) As Boolean
    6.     bValue = True
    7.     pvSetTrue = True
    8. End Function
    cheers,
    </wqw>

  20. #20
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,902

    Re: VB - Detect if you are running in the IDE...

    I agree with Arnoutdv: it doesn't matter the method, you need to store the result in a valriable. I usually use a Static variable.

    Code:
    Public Function InIDE() As Boolean
        Static sValue As Long
        
        If sValue = 0 Then
            Err.Clear
            On Error Resume Next
            Debug.Print 1 / 0
            If Err.Number Then
                sValue = 1
            Else
                sValue = 2
            End If
            Err.Clear
        End If
        InIDE = (sValue = 1)
    End Function

  21. #21
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,805

    Re: VB - Detect if you are running in the IDE...

    This is the one I currently use:

    Code:
    
    Public Function InIDE(Optional ByRef b As Boolean = True) As Boolean
        ' NEVER specify the Optional b when calling.
        If b = True Then Debug.Assert Not InIDE(InIDE) Else b = True
    End Function
    
    

    It was inspired by something I saw The Trick do. It's nice in that no disk I/O is performed. And it's very fast and terse. Also, it won't tamper with the Err object. I've used maybe half-a-dozen through the years, and I think the above is best.

    EDIT1: I suppose you could add a Static to it to make it slightly faster, but it's pretty fast as it is. I'll let loquat test if he likes.
    Last edited by Elroy; Sep 2nd, 2019 at 01:04 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  22. #22
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    271

    Re: VB - Detect if you are running in the IDE...

    Prior art:

    Quote Originally Posted by Bonnie West View Post
    Code:
    
    Private Function InIDE(Optional ByRef B As Boolean = True) As Boolean   'Very efficient function for testing whether
        If B Then Debug.Assert Not InIDE(InIDE) Else B = True               'this code is running in the IDE or compiled
    End Function                                                            'Based on the original by Vesa Piittinen
    

  23. #23
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,585

    Re: VB - Detect if you are running in the IDE...

    You cannot make InIDE3 faster by adding a static as the single Debug.Assert eventually gets removed, so in compiled executable it gets reduced to a no-code function w/ no parameters.

    The only way to beat this is if you don't call anything at all I suppose :-))

    cheers,
    </wqw>

  24. #24
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,902

    Re: VB - Detect if you are running in the IDE...

    Quote Originally Posted by wqweto View Post
    You cannot make InIDE3 faster by adding a static as the single Debug.Assert eventually gets removed, so in compiled executable it gets reduced to a no-code function w/ no parameters.

    The only way to beat this is if you don't call anything at all I suppose :-))

    cheers,
    </wqw>
    Compiled yours and Elroy's one are quite faster than mine.

    Compiled, 100,000,000 iterations:

    Code:
    InIDE3               0.101
    mine                 2.226    
    Elroy's              0.156
    global variable      0.046
    Uncompiled, 100,000,000 iterations:

    Code:
    
    InIDE3               8.031
    mine                 4.789
    Elroy's              8.304
    global variable      0.507
    But if speed is really an issue, the best (of course) is to use just a variable and set it up when the program starts.

    PS: I didn't test others.
    Attached Files Attached Files
    Last edited by Eduardo-; Sep 2nd, 2019 at 10:38 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width