Solved

How to detect debug mode vs. run mode?

Posted on 1998-09-23
10
362 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now