locked
How to use DEBUG const in VB.Net DLL RRS feed

  • Question

  • User1800473205 posted

    Hi friends,

    I want to know how to use DEBUG const in DLL.

    ->  I have a web application in vb.net

    -> I have a DLL whith some function.

    Now i have reference this DLL into my web application. And now i want to check in dll whether my application is in DEBUG mode or in Realease mode.

    Second thing i want to check some validation in constructor based on that constructor will throw exception and i want to show it on page without handling. I have used try catch in constructor and not used in page, so it will give me error in dialog box not in page.

    Thanks in advanced!!!

    Monday, May 3, 2010 7:42 AM

Answers

  • User-1364446067 posted

    Ok. I got it.

    I made a small app to check app status using four different techniques, and these are the results i got.


    This is the class library code for the three method i used:


    public static class Class1
        {
            public static bool CheckAppMode()
            {
                #if (DEBUG)
                    return true;
                #else
                    return false;
                #endif
            }
    
            public static bool CheckAssemblyMode(System.Reflection.Assembly asmbly)
            {
                object[] objAttribs = asmbly.GetCustomAttributes(typeof(System.Diagnostics.DebuggableAttribute), false);
                return ((System.Diagnostics.DebuggableAttribute)objAttribs[0]).IsJITTrackingEnabled;
            }
    
            public static bool CheckIsDebuggerAttached()
            {
                return System.Diagnostics.Debugger.IsAttached;
            }
        }




    When dll built in debug mode


    application run in Debug mode:

    true from CheckAppMode
    true from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached



    application run in release mode:

    true from CheckAppMode
    false from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached


    application run in published mode (from virtual directory):

    true from CheckAppMode
    true from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    false from CheckIsDebuggerAttached



    When dll built in release mode



    application run in debug mode:

    false from CheckAppMode
    true from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached



    application run in release mode:

    false from CheckAppMode
    false from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached



    application run in published mode (from virtual directory):

    false from CheckAppMode
    false from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    false from CheckIsDebuggerAttached


    Try it in this way, I think CheckIsDebuggerAttached and CheckAssemblyMode are the methods which can be used by you...


    --

    Mark as answer, if it answers you..

    --



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 4, 2010 3:17 AM
  • User-1364446067 posted

    From my web application what should i have to pass in this function:

    CheckAssemblyMode(System.Reflection.Assembly asmbly

    You can pass this: System.Reflection.Assembly.GetExecutingAssembly()

    and call that method in the dll like this:

    TestClassLibrary.Class1.CheckAssemblyMode(System.Reflection.Assembly.GetExecutingAssembly())

    And another thing if this gives me just status of DLL whether it  is in DEBUG or RELEASE then it's useless i want status of Web Application.

    No, as you are passing the executing assembly, which is of the application, so it'll tell the status of your application.


    --

    Mark as answer, if it answers you..

    --



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 4, 2010 5:10 AM

All replies

  • User-1659704165 posted

    Hi,

     #If DEBUG Then
        MsgBox("DEBUG Compiler Constant defined")
     #End If

    have u tried the above syntax

    Monday, May 3, 2010 12:10 PM
  • User1800473205 posted

    Hi avinash,

    thanks for your reply.

    Yes i have tried this but i want this code in DLL.

    I have reference that DLL in a web application and i want to check whether this Web application is in DEBUG mode or RELEASE mode.

    and i want this check in DLL. 

     

    My requirement is like licensing a DLL for developer only not after release that application.

    So When i give this dll to any developer he need to pass a license key to create a object of class which is in DLL. and not validate this key it after released.

    So when object is creating i want to check in constructor whether the application in which this DLL is referenced is in DEBUG mode or in RELEASE mode.

     

     

    Tuesday, May 4, 2010 1:00 AM
  • User-1659704165 posted

    if saying that u want know is dll is relased Mode or Debug Mode..

    http://stackoverflow.com/questions/194616/how-to-tell-if-net-app-was-compiled-in-debug-or-release-mode

    chk the above link


    Tuesday, May 4, 2010 1:07 AM
  • User-1364446067 posted

    I want to know how to use DEBUG const in DLL.

    ->  I have a web application in vb.net

    -> I have a DLL whith some function.

    Now i have reference this DLL into my web application. And now i want to check in dll whether my application is in DEBUG mode or in Realease mode.

    Hi Shailesh,

    If you write the DEBUG const in your dll, and your application uses that dll. The debug const will still work for your application in the same way as it has been written in the code of your app directly. Just try it. I check it a while ago..

    the dll's code

    namespace TestClassLibrary
    {
        public static class Class1
        {
            public static bool CheckAppMode()
            {
                #if (DEBUG)
                    return true;
                #else
                    return false;
                #endif
            }
        }
    }


    The page's .cs code

    protected void Page_Load(object sender, EventArgs e)
            {
                // This will return the result according to your application running mode
                TestClassLibrary.Class1.CheckAppMode();
            }




    --

    Mark as answer, if it answers you..

    --



    Tuesday, May 4, 2010 1:26 AM
  • User1800473205 posted

    Avinash,

    No, i don't want to check whether DLL is in debug mode or release mode.

    But i want to check whether a Web application is in debug or release mode in which this DLL is referenced and this check i want in DLL.

    Example:

    Suppose   i have a DLL "X" which is completed.

    I have a web application "Y" in which this "X" dll is reference.

    So when i create a object in "Y" i want to validate in constructor of class of "X" dll that application "Y" is in DEBUG mode or in Release mode.

    Tuesday, May 4, 2010 1:31 AM
  • User1800473205 posted

    Hi,

    I have tried your code but it is working fine,but it DEBUG is check whether DLL is in build in DEBUG mode or RELEASE mode.

    but i want status of web application in DLL whether application is in DEBUG or RELEASE mode. 

    Tuesday, May 4, 2010 2:06 AM
  • User-1364446067 posted

    No. The code is not checking the dll status, it is checking the status of application. The code doesnot involve to look for any assembly to check its status, its rather just considering the current execution process. So the method in dll, is just a service you use to check about the running application.


    By the way, did you check the behavior using 'HttpContext.Current.IsDebuggingEnabled' or 'System.Diagnostics.Debugger.IsAttached' too ??

    --

    Mark as answer, if it answers you..

    --



    Tuesday, May 4, 2010 2:41 AM
  • User1800473205 posted

    This is my code in one of the class of DLL . I have tried...

    Public Class MasterClass
        Public IsInDebug As Boolean
        Public Shared strLicense As String = String.Empty
    
        ''' <summary>
        ''' constructor
        ''' </summary>
        ''' <param name="strLicenseCode">License code</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strLicenseCode As String)
            Try
    #If DEBUG Then
                IsInDebug = True 
    #Else
                IsInDebug = False
    #End If
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
    End Class


     Now when i build this dll in DEBUG mode and reference in web application IsInDebug will always true....

    and if i build in Release mode it will return me false always...

    following is my aspx.vb code...

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                Dim m1 As New MasterClass("1")
                If m1.IsInDebug Then
                    Response.Write("Debug Mode")
                Else
                    Response.Write("Release Mode")
                End If
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End Sub

    I want whether my application is in release mode or debug mode in dll. Can i check this by DEBUG?

    Tuesday, May 4, 2010 2:52 AM
  • User-1364446067 posted

    Ok. I got it.

    I made a small app to check app status using four different techniques, and these are the results i got.


    This is the class library code for the three method i used:


    public static class Class1
        {
            public static bool CheckAppMode()
            {
                #if (DEBUG)
                    return true;
                #else
                    return false;
                #endif
            }
    
            public static bool CheckAssemblyMode(System.Reflection.Assembly asmbly)
            {
                object[] objAttribs = asmbly.GetCustomAttributes(typeof(System.Diagnostics.DebuggableAttribute), false);
                return ((System.Diagnostics.DebuggableAttribute)objAttribs[0]).IsJITTrackingEnabled;
            }
    
            public static bool CheckIsDebuggerAttached()
            {
                return System.Diagnostics.Debugger.IsAttached;
            }
        }




    When dll built in debug mode


    application run in Debug mode:

    true from CheckAppMode
    true from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached



    application run in release mode:

    true from CheckAppMode
    false from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached


    application run in published mode (from virtual directory):

    true from CheckAppMode
    true from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    false from CheckIsDebuggerAttached



    When dll built in release mode



    application run in debug mode:

    false from CheckAppMode
    true from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached



    application run in release mode:

    false from CheckAppMode
    false from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    true from CheckIsDebuggerAttached



    application run in published mode (from virtual directory):

    false from CheckAppMode
    false from CheckAssemblyMode
    true from HttpContext.Current.IsDebuggingEnabled
    false from CheckIsDebuggerAttached


    Try it in this way, I think CheckIsDebuggerAttached and CheckAssemblyMode are the methods which can be used by you...


    --

    Mark as answer, if it answers you..

    --



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 4, 2010 3:17 AM
  • User1800473205 posted

    Hi,

    From my web application what should i have to pass in this function:

    CheckAssemblyMode(System.Reflection.Assembly asmbly

    please guide me i dont know much more about Reflection.

     

    And another thing if this gives me just status of DLL whether it  is in DEBUG or RELEASE then it's useless i want status of Web Application.

    Tuesday, May 4, 2010 4:51 AM
  • User-1364446067 posted

    From my web application what should i have to pass in this function:

    CheckAssemblyMode(System.Reflection.Assembly asmbly

    You can pass this: System.Reflection.Assembly.GetExecutingAssembly()

    and call that method in the dll like this:

    TestClassLibrary.Class1.CheckAssemblyMode(System.Reflection.Assembly.GetExecutingAssembly())

    And another thing if this gives me just status of DLL whether it  is in DEBUG or RELEASE then it's useless i want status of Web Application.

    No, as you are passing the executing assembly, which is of the application, so it'll tell the status of your application.


    --

    Mark as answer, if it answers you..

    --



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 4, 2010 5:10 AM