Dll LibMain Function?
-
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
-
LoadLibrary[^] will call DllMain for you, so you can use P/Invoke for that task. regards
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... }
}
-
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... }
}
-
So did you actually call the method?
LoadLibrary("YourDll.dll");
if this returns some value
!= IntPtr.Zero
then the call was successful. regardsThank 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
-
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
-
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.
-
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
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
-
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
-
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
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 -
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