When is DLL Main executed?

I am trying to load a dll and make it run code as soon as it is loaded without having anything called. Is dll main like a regular main where it starts running as soon as it is launched If not, how can I create that kind of effect


Answer this question

When is DLL Main executed?

  • whytepaul

    Sounds very interesting. I have never really programmed a DLL before, if you could give me some example code like how to execute a message box when the dll is loaded that would be great!

  • Mike1900

    Here you are.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=214754&SiteID=1

    Be aware that this is a little heavy duty.

    The class itself is complex... (but it is a great file I/O class)
    There are steps in dealing with the DLL such as Ngen which are not a necessity.

    If you need any assistance, please ask.



  • Fabio Mattiussi

    You can also have the constructor on a module in a DLL.

    This I only learnt recently. But it seems fairly logical as the module is really a class with an attribute on which makes it shared and all its methods shared.


  • Soccer8

    I really appreciate your help. My defenition of remote process is a process on the same machine. I have a process (notepad.exe) running. Then I have loader.exe and it injects the dll into the address space of notepad.exe. The dll has now been loaded. I wish for code to be executed at that point. I will not be able to call the dll at all. I will not be able to call any contructors, anything...

  • ACorbs

    If I'm correct you cant have a DLL execute code as soon as it load up. A DLL is an assembly that contains classes.

    You can have code execute upon the first instance of the class (or module as its really a class underneath) by putting a sub new in the class with no parameters. You can put this in either a class or module and this default constructor will be called when the class is first used.

    With this in mind, if you are simply storing methods in the dll - create this default constructor method and put the rest of you methods in this one module/class.

    When that class is first used it will run the default constructor which will run the code you want run. Note this will occur on first usage of the class which will be when the DLL is loaded.


  • mithya

    When a dll is understood as a class library, there can be a constructor, that is a public routine that is executed when the class is instantiated.

    So that when you say

    dim a as New MyDll that constructor is executed.



  • ImperfectTommy

    Hold on... I did a file I/O class dll on these boards for users. Let me see if i can find it.



  • EasyJeans

    DllMain is specific to native dlls (like those created using C/C++). .NET dlls don't have a way to specify a DllMain function. Even if there was a way to add a DllMain function to your dll injecting a managed dll into another process will not have the effect of starting the .NET runtime in that process. Doing so can have negative effects like trying to load different .NET Framework versions in the same process which is not supported.

     


  • KSchram

    I know more specifically what I want now. I need to find a way to run dllmain, a function that is called by the system on certian events. I searched the internet and found the following code, but it dosent do anything for the dll. I have a friend who used almost identical code in c++ and it worked, i even loaded his dll with my injector. Code:

    Module Module1

    Public Const DLL_PROCESS_DETACH = 0

    Public Const DLL_PROCESS_ATTACH = 1

    Public Const DLL_THREAD_ATTACH = 2

    Public Const DLL_THREAD_DETACH = 3

    Public Function DllMain(ByVal hInst As Long, ByVal fdwReason As Long, _

    ByVal lpvReserved As Long) As Boolean

    Select Case fdwReason

    Case DLL_PROCESS_DETACH

    MsgBox("Omg Mine Works TOO!")

    ' No per-process cleanup needed

    Case DLL_PROCESS_ATTACH

    MsgBox("Omg Mine Works TOO!")

    DllMain = True

    Case DLL_THREAD_ATTACH

    MsgBox("Omg Mine Works TOO!")

    ' No per-thread initialization needed

    Case DLL_THREAD_DETACH

    MsgBox("Omg Mine Works TOO!")

    ' No per-thread cleanup needed

    End Select

    End Function

    End Module

    Now, I dont know if I should have this code in a module, or in class 1, or what. I have never made a DLL before and I am pretty lost. All i know is that this code works somehow and I have not yet found that how. My Goal is to activate a messagebox when my dll is launched, should be the DLL_Process_Attach event.



  • newxplanet

    I created a solution here of a DLL which was callable from an EXE. The DLL and the EXE are separate components and the DLL can be taken and used with any other application you wish to write. It just so happened that in the example I created both in the same solution.

    A default constructor cannot have any parameters but will give you an method which will let you know that the class library has been loaded whenever the first method in that library is called. In this case I used your method and called it from the applciation and as your method takes parameters it cannot be a default constructor.

    But when called it showed that the msgbox("first load") was only called first when the class library was first loaded. You could put any other methods or properties into this module and whenever you first use any of these methods and the module is loaded for the the first load the messagebox would appear. Whatever you want to do in the constructor is is up to you.

    This code was merely meant to demonstrate the use of a default constructor. To show when the class library (DLL) is loaded.

    So you load this DLL into a remote process - whats your definition of remote process Different Application , Different Machine

    This was not mentioned previously and it really doesnt make that much of a difference as soon as the class library is called and therefore loaded into memory on the remote process the default constructor gets called.

    Depending upon your scenario as to what the remote process is, it may involve remoting which then is a completely different subject which ultimately to answer you initial question about knowing when a dll is loaded - it would still occur on the remote process when the first access was made. The only difference would really be the the means by which the method on the remote machine is called.


  • Bryan Morgan

    OK

    You are going to create a Class Library project called ClassLibrary1 and put the following code in there

    Public Module WhateverModule
    Public Const DLL_PROCESS_DETACH = 0
    Public Const DLL_PROCESS_ATTACH = 1
    Public Const DLL_THREAD_ATTACH = 2
    Public Const DLL_THREAD_DETACH = 3

    Sub New()
    MsgBox("First Load")
    End Sub

    Public Function DllMain(ByVal hInst As Long, ByVal fdwReason As Long, ByVal lpvReserved As Long) As Boolean
    Select Case fdwReason
    Case DLL_PROCESS_DETACH
    MsgBox("Omg Mine Works TOO!")
    ' No per-process cleanup needed

    Case DLL_PROCESS_ATTACH
    MsgBox("Omg Mine Works TOO!")
    DllMain = True

    Case DLL_THREAD_ATTACH
    MsgBox("Omg Mine Works TOO!")
    ' No per-thread initialization needed

    Case DLL_THREAD_DETACH
    MsgBox("Omg Mine Works TOO!")

    ' No per-thread cleanup needed
    End Select
    End Function
    End Module

    Then you are going to Add another project to your solution which is a windows application and on the form you are going to put a button on it.

    Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim b As Boolean = WhateverModule.DllMain(ClassLibrary1.DLL_THREAD_ATTACH, 1, 1)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    End Class

    The you are going to add a reference on the windows application to the classlibrary1. It will be a project reference. My Project -> Reference -> Project

    And then you are going to make the Windows Application the startup project - by selecting the windowsApplication project in solution explorer and right clicking and selecting set as startup project.

    Run the code click the button. This will call the DLL function but as its an initial load it will display two messagebox boxes

    the first will say First Load, followed by one which says "Omg Mine Works TOO!". When you click the button a 2nd / 3rd / 4th .... time it will only say "Omg Mine Works TOO!"

    Hopefully that walkthrough will demonstrate creating a class library and the default constructor.




  • Simon Guest

    This is interesting, but it dosent satisfy my needs. I am loading my DLL into a remote process, one other then the process that is loading it. I will have no way to touch it at all once it is loaded, but I need it to execute, some, ANY code, for the matter of simplicity, a messagebox. I am not 100% sure what the code you have me was supposed to do, but I tried it and it did not serve my needs. I need to on load (thats what DLLMain is, a function called by the system on load, unload) the dl to run some code.



  • When is DLL Main executed?