[C#] auto load dll on injection

10/07/2012 20:56 tdr2010#1
I have a dll(made by me in c#) and I want to auto load a function when it is injected into a process. How can I do this? An little example would be perfect for me.

PS: Sorry for my english..
10/09/2012 00:24 nkkk#2
thats not posible in C#.

you have to write native code thats calls your function. The easiest is to use c++/Cli because that allows you to merge nativ code (with a dllmain) with managed code that calls your function, without having to initialize the .net by yourself.
10/09/2012 19:16 tdr2010#3
ok...how cand I make a simple c++ native dll? (using Visual C++?) an example?
10/09/2012 19:30 nkkk#4
hm i use us untimate but i think its possible with vc++ too.

you make a new c++ cli dynamic library, and then add the following file:
Code:
#pragma unmanaged

#include<Windows.h>
#include "managedStart.h"

DWORD WINAPI ThreadProc(LPVOID lpParameter);

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		CreateThread(NULL,NULL,ThreadProc,NULL,0,NULL);
		break;
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
	Sleep(200);
	ManagedStart();
	return 0;
}
ManagedStart is a managed! function declared in managedStart.h, and defined in managedStart.cpp which calls your function from your C# dll.
10/16/2012 10:01 Naworia#5
Use key events if possible in dynamic libraries :D
10/16/2012 18:30 MoepMeep#6
Quote:
Originally Posted by nkkk View Post
thats not posible in C#.

you have to write native code thats calls your function. The easiest is to use c++/Cli because that allows you to merge nativ code (with a dllmain) with managed code that calls your function, without having to initialize the .net by yourself.

And whats hard about starting the CLR-Host by urself? :<