Solved

How to detect debug mode vs. run mode?

Posted on 1998-09-23
10
363 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

912 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now