Solved

How to detect debug mode vs. run mode?

Posted on 1998-09-23
10
364 Views
Last Modified: 2008-02-01
I want to be able to log errors to a logfile if the exe version of my code is running but pop a message box up if the debug version is running (in the VB IDE).  How can I detect the difference?
0
Comment
Question by:debbyt
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:fguerreiro_inix
ID: 1436236
In your error handler put this:

errornumber = err.number            'save err number
err=0
on error resume next
debug.print errors(errornumber)
if err<>0 then                             'you are in exe mode
     logtofile
else                                            'you are in development mode
     msgbox errors(errornumber)
endif

This only works because using debug object in an exe causes
an error


Hope this helps
0
 

Author Comment

by:debbyt
ID: 1436237
Is there a property in VB that tells you whether or not the application is in debug mode (IDE) or run mode (exe)?  
0
 
LVL 1

Expert Comment

by:DennisH
ID: 1436238
The easiest way to do this debbie is to use a conditional compile flag in the Declarations section of the module being tested.

#Const Debugging = True

Then in your error handler use code like this:
#If Debugging then
    LogToFile
#Else
    MsgBox
#EndIf
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

Author Comment

by:debbyt
ID: 1436239
Does this make the detection of run mode vs. debug mode automatic?  Maybe I'm not understanding what you're saying but when I tried your suggestion I am always getting Debugging to be true regardless of whether I'm in run mode or debug mode.

Please Clarify.  Thanks.
0
 
LVL 2

Expert Comment

by:BergJC
ID: 1436240
I think with DennisH's answer you'll have to set that #Const Debugging = True when you are in the IDE, and then change it to #Const Debugging = False when you compile your .EXE
0
 
LVL 2

Expert Comment

by:BergJC
ID: 1436241
I think with DennisH's answer you'll have to set that #Const Debugging = True when you are in the IDE, and then change it to #Const Debugging = False when you compile your .EXE
0
 

Author Comment

by:debbyt
ID: 1436242
That makes sense.  However, does this mean there  is no  way to check for run vs. debug modes in code without having to manually change a parameter?  That's really what I'm looking for.
0
 
LVL 1

Expert Comment

by:DennisH
ID: 1436243
I don't know of any way.
0
 

Accepted Solution

by:
hackerpii earned 50 total points
ID: 1436244
Option Explicit
'     ' ************************************************
'     ' Win16-specific declarations and code
'     ' ************************************************

       #If Win16 Then
              '      '
              '      ' Win16 API calls used to get executing filename
              '      '
               Private Declare Function GetModuleFileName Lib "Kernel" _
               (ByVal hModule As Integer, ByVal lpFilename As String, _
               ByVal nSize As Integer) As Integer
               Private Declare Function GetWindowWord Lib "User" _
               (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer
              '      '
              '      ' Window field offsets for GetWindowWord()
              '      '
               Private Const GWW_HINSTANCE = -6
               Function vbCompiled(hWndMain As Integer) As Boolean
               Dim hInst As Integer
               Dim nRtn As Integer
               Dim Buffer As String
              '      '
              '      ' Get instance associated with main form
              '      '
               hInst = GetWindowWord(hWndMain, GWW_HINSTANCE)
              '      '
              '      ' Create buffer for filename and retrieve
              '      '
               Buffer = Space$(128)
               nRtn = GetModuleFileName(hInst, Buffer, Len(Buffer))
               Buffer = Left(Buffer, nRtn)
              '      '
              '      ' Compare module filename with known VB environment name
              '      '
               If Right(Buffer, 7) = "\VB.EXE" Then
               vbCompiled = False
               Else
               vbCompiled = True
               End If
               End Function
       #End If

'     ' ************************************************
'     ' Win32-specific declarations and code
'     ' ************************************************

       #If Win32 Then
              '      '
              '      ' Win32 API call used to get executing filename
              '      '
               Private Declare Function GetModuleFileNameA Lib "kernel32" _
               (ByVal hModule As Long, ByVal lpFilename As String, _
               ByVal nSize As Long) As Long
               Function vbCompiled(Optional hWndMain As Variant) As Boolean
               Dim nRtn As Long
               Dim Buffer As String
              '      '
              '      ' Create buffer for filename and retrieve
              '      '
               Buffer = Space$(256)
               nRtn = GetModuleFileNameA(0&, Buffer, Len(Buffer))
               Buffer = UCase(Left(Buffer, nRtn))
              '      '
              '      ' Compare module filename with known VB environment name
              '      '
               If Right(Buffer, 9) = "\VB32.EXE" Then
               vbCompiled = False
               Else
               vbCompiled = True
               End If
               End Function
       #End If

This should do it.

HackerPII
0
 

Author Comment

by:debbyt
ID: 1436245
Sounds like what I'm looking for.  Thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question