-2

I want to create a class library project in C# and attach it to a procces.

I've already did this in C++, my code is

#include <windows.h>
void Thread()
{
// here i just do my stuff
}
int WINAPI DllMain(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
    if(reason==DLL_PROCESS_ATTACH)
    {
         CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Thread, 0, 0, 0);
    }
    return 1;
}

This works very well for me.

Is possible to do this in C#?

Thanks, I hope i'll find an answer and accept it.

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • .Net process or do you want to host CLR? – mtmk Jul 11 '14 at 19:46
  • Maxwell i don't really understand your comment. I want to make a mod for a game. Using the C++ code above, I get a .dll file, add it to the game folder and the dll will be attached and that thread will start. I want to do the same with C#. – Boldijar Paul Jul 11 '14 at 19:50
  • What I meant was: is it all c# or mix of c++ and c#? – mtmk Jul 11 '14 at 19:53
  • I want to do all in C#. – Boldijar Paul Jul 11 '14 at 19:55
  • This is probably what you need: http://stackoverflow.com/questions/1137781/c-sharp-correct-way-to-load-assembly-find-class-and-call-run-method – mtmk Jul 11 '14 at 19:56
  • I don't want to create a DLL dynamically.. i just want to create it normaly, with F6.. and I want it when is attached to a procces to start a thread.. – Boldijar Paul Jul 11 '14 at 20:01
  • Not sure what F6 does but anyway.. That doesn't create a DLL. Loads an already existing one. And I found this as well. Seems better option than static constructors: http://stackoverflow.com/questions/505237/net-running-code-when-assembly-is-loaded ..anyway, interesting question. Good luck. – mtmk Jul 11 '14 at 20:07
  • You code is broken in C++ anyway. That's not a thread proc. The fact you had to cast it is a tell tale sign. – David Heffernan Jul 11 '14 at 20:07
  • @MaxwellTroyMiltonKing do you know how can I implement that? Copying some code from will only give me errors.. – Boldijar Paul Jul 11 '14 at 20:15
  • 1
    @Paul Module initializers aren't supported in C# - you'd need to use C++/CLI... – Reed Copsey Jul 11 '14 at 21:10

3 Answers3

2

C# doesn't have a mechanism such as DllMain where you can execute code when referenced or used from an executable. The executable will need to explicitly call a method on (or at least use) a type within your library in order for your code to execute.

If you know a type within your library is going to be used, however, you can "cheat" and get your thread to start by placing it within the static constructor for that type. This will cause it to execute at or before the first usage of that type.

That being said, if you're writing the exe that's going to use this C# library, you can just call a method on a type that begins your thread and starts your work.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Is not working...I saved the library as a .DLL file (well I did F6), copied the DLL to the game folder that i want to load it (GTA). And when I start the game i hear no beep...if I create a DLL with C++ and the code in the question it works.. – Boldijar Paul Jul 11 '14 at 20:03
1

Follow the discussion here.

Give your class a static constructor and do your initialization there. It will run the first time anybody calls a static method or property of your class or constructs an instance of your class.

Community
  • 1
  • 1
etr
  • 1,252
  • 2
  • 8
  • 15
1

Soulds like you can use module initializers. Unfortunately C# doesn't support them. You will have to inject the IL, for example using a tool like Module Initializer or Fody

mtmk
  • 6,176
  • 27
  • 32