Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dll LibMain Function?

Dll LibMain Function?

Scheduled Pinned Locked Moved C#
csharpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OptiPlex
    wrote on last edited by
    #1

    Hey I want to inject DLL made in C# into an Application. Ive been looking for this for a long time, but still cant find the answer. In C you would use it like this

    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    {
    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
    // attach to process
    MessageBox(0, "Test", "Test", MB_OK);
    break;

        case DLL\_PROCESS\_DETACH:
            // detach from process
            break;
    
        case DLL\_THREAD\_ATTACH:
            // attach to thread
            break;
    
        case DLL\_THREAD\_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
    

    }

    Is there a way to do this in C#? Like when I inject it, that it also shows the MessageBox at successfull injection? Thanks in Advance - opx

    L G 2 Replies Last reply
    0
    • O OptiPlex

      Hey I want to inject DLL made in C# into an Application. Ive been looking for this for a long time, but still cant find the answer. In C you would use it like this

      BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
      {
      switch (fdwReason)
      {
      case DLL_PROCESS_ATTACH:
      // attach to process
      MessageBox(0, "Test", "Test", MB_OK);
      break;

          case DLL\_PROCESS\_DETACH:
              // detach from process
              break;
      
          case DLL\_THREAD\_ATTACH:
              // attach to thread
              break;
      
          case DLL\_THREAD\_DETACH:
              // detach from thread
              break;
      }
      return TRUE; // succesful
      

      }

      Is there a way to do this in C#? Like when I inject it, that it also shows the MessageBox at successfull injection? Thanks in Advance - opx

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      LoadLibrary[^] will call DllMain for you, so you can use P/Invoke for that task. regards

      O 1 Reply Last reply
      0
      • L Lost User

        LoadLibrary[^] will call DllMain for you, so you can use P/Invoke for that task. regards

        O Offline
        O Offline
        OptiPlex
        wrote on last edited by
        #3

        Thank you for your kind reply, but I still dont get it. Im not really familiar with LoadLibrary... This is what I got so far

        using System;
        using System.Collections.Generic;
        using System.Text;
        using System.Runtime.InteropServices;

        namespace MyDll
        {
        public class clsMain
        {
        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string lpFileName);

            // LibMain here...
        }
        

        }

        L 1 Reply Last reply
        0
        • O OptiPlex

          Thank you for your kind reply, but I still dont get it. Im not really familiar with LoadLibrary... This is what I got so far

          using System;
          using System.Collections.Generic;
          using System.Text;
          using System.Runtime.InteropServices;

          namespace MyDll
          {
          public class clsMain
          {
          [DllImport("kernel32.dll")]
          public static extern IntPtr LoadLibrary(string lpFileName);

              // LibMain here...
          }
          

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          So did you actually call the method?

          LoadLibrary("YourDll.dll");

          if this returns some value != IntPtr.Zero then the call was successful. regards

          O 1 Reply Last reply
          0
          • L Lost User

            So did you actually call the method?

            LoadLibrary("YourDll.dll");

            if this returns some value != IntPtr.Zero then the call was successful. regards

            O Offline
            O Offline
            OptiPlex
            wrote on last edited by
            #5

            Thank you You see, im using a program called Winject to inject the dll. I guess that really does it all. When it gets injected, it automatically calls the LibMain function and executes the code in my Dll. But when I try to inject the dll I made in c#, it doesnt get executed, this is the code im using:

            using System;
            using System.Collections.Generic;
            using System.Text;
            using System.Runtime.InteropServices;

            namespace MyDll
            {
            public class clsMain
            {
            [DllImport("kernel32.dll")]
            public static extern IntPtr LoadLibrary(string lpFileName);

                \[DllImport("user32.dll")\]
                public static extern int MessageBox(int hwnd, string lpText, string lpTitle, int style);
            
                // LibMain here...
                public void LibMain(IntPtr hInstance, int fdwReason, int reserved)
                {
                    MessageBox(0, "Injected from C#", "C# hello world", 0);
                }
            }
            

            }

            Thank you very much again! - opx

            L 1 Reply Last reply
            0
            • O OptiPlex

              Thank you You see, im using a program called Winject to inject the dll. I guess that really does it all. When it gets injected, it automatically calls the LibMain function and executes the code in my Dll. But when I try to inject the dll I made in c#, it doesnt get executed, this is the code im using:

              using System;
              using System.Collections.Generic;
              using System.Text;
              using System.Runtime.InteropServices;

              namespace MyDll
              {
              public class clsMain
              {
              [DllImport("kernel32.dll")]
              public static extern IntPtr LoadLibrary(string lpFileName);

                  \[DllImport("user32.dll")\]
                  public static extern int MessageBox(int hwnd, string lpText, string lpTitle, int style);
              
                  // LibMain here...
                  public void LibMain(IntPtr hInstance, int fdwReason, int reserved)
                  {
                      MessageBox(0, "Injected from C#", "C# hello world", 0);
                  }
              }
              

              }

              Thank you very much again! - opx

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I'm confused, do you have a native DLL or a .NET DLL? First, you presented a native DLL, now you're showing me the code of a .NET DLL, which works totally different and where LoadLibrary has no effect on.

              O 1 Reply Last reply
              0
              • L Lost User

                I'm confused, do you have a native DLL or a .NET DLL? First, you presented a native DLL, now you're showing me the code of a .NET DLL, which works totally different and where LoadLibrary has no effect on.

                O Offline
                O Offline
                OptiPlex
                wrote on last edited by
                #7

                I wanted to make a Dll like c but in c#, so yes a native Dll. I want it to be injected into a process, and not called. But I guess C# is not really a good language for this, right? Thanks for your replies - opx

                L D 2 Replies Last reply
                0
                • O OptiPlex

                  Hey I want to inject DLL made in C# into an Application. Ive been looking for this for a long time, but still cant find the answer. In C you would use it like this

                  BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
                  {
                  switch (fdwReason)
                  {
                  case DLL_PROCESS_ATTACH:
                  // attach to process
                  MessageBox(0, "Test", "Test", MB_OK);
                  break;

                      case DLL\_PROCESS\_DETACH:
                          // detach from process
                          break;
                  
                      case DLL\_THREAD\_ATTACH:
                          // attach to thread
                          break;
                  
                      case DLL\_THREAD\_DETACH:
                          // detach from thread
                          break;
                  }
                  return TRUE; // succesful
                  

                  }

                  Is there a way to do this in C#? Like when I inject it, that it also shows the MessageBox at successfull injection? Thanks in Advance - opx

                  G Offline
                  G Offline
                  Giorgi Dalakishvili
                  wrote on last edited by
                  #8

                  See if these helps: EasyHook - The reinvention of Windows API hooking[^]

                  Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

                  1 Reply Last reply
                  0
                  • O OptiPlex

                    I wanted to make a Dll like c but in c#, so yes a native Dll. I want it to be injected into a process, and not called. But I guess C# is not really a good language for this, right? Thanks for your replies - opx

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    OptiPlex wrote:

                    I want it to be injected into a process, and not called.

                    I'd rather use a native DLL, then. regards

                    1 Reply Last reply
                    0
                    • O OptiPlex

                      I wanted to make a Dll like c but in c#, so yes a native Dll. I want it to be injected into a process, and not called. But I guess C# is not really a good language for this, right? Thanks for your replies - opx

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      If you want to inject your DLL code into another process, which you do not control, your .DLL MUST be written in a non-managed language (non-.NET). That means that you cannot use C# to write the .DLL. .DLL injection only works with native-code, not managed.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007, 2008

                      O 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        If you want to inject your DLL code into another process, which you do not control, your .DLL MUST be written in a non-managed language (non-.NET). That means that you cannot use C# to write the .DLL. .DLL injection only works with native-code, not managed.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007, 2008

                        O Offline
                        O Offline
                        OptiPlex
                        wrote on last edited by
                        #11

                        Okay, I got the answer for my question now. Thank you all!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups