Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


.dll injection with C#

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anesah
How do I cheat?
Reputation: 0

Joined: 09 Nov 2013
Posts: 3

PostPosted: Sun Mar 08, 2015 11:20 pm    Post subject: .dll injection with C# Reply with quote

Hello, everyone. I am a amateur (though not so amateur-ish) modder of games and software developer by education and trade. I've been trying to wrap my head around writing my first .dll for injection, but I've run into a wall at what (I hope) will be the final few steps.

Hopefully more advanced people out there can answer the few questions I have. I've written all the game-modifying code I need, but I'm stuck on the actual injection technique. If I had started in C++, this might be easier, but I can't for the life of me figure out the usage of __declspec(dllexport) in C#. Maybe it's too primitive and there is something different that I'm missing. From the examples I found on the internet, my class declaration looks something like:

Code:

public class __declspec(dllexport) Injection : DecompiledType {
//some code
}


This, of course, will not compile. If this has been answered by someone else, at another time, I apologize for wasting time and space. Thank you in advance for any help you can provide.
[/code]
Back to top
View user's profile Send private message AIM Address
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 893

PostPosted: Mon Mar 09, 2015 1:34 am    Post subject: Re: .dll injection with C# Reply with quote

Anesah wrote:
I can't for the life of me figure out the usage of __declspec(dllexport) in C#.

AFAIK, you can't just export a C# function/method/class and expect it to be callable by a native process (pretty sure you can, however, use the normal dllexport tags in managed C++). If you use C#, you have to hack your assembly to manually emit the .export ilasm. You can try using dll_tool.

Also, did you bother to Google before asking?

_________________
A nagy kapu mellett, mindig van egy kis kapu.
----------------------
Come on...
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Mar 10, 2015 12:20 am    Post subject: Reply with quote

C/C++ and C# generate two separate kinds of DLLs. C/C++ generates native, unmanaged DLLs. C# generates managed DLLs. That said, you cannot just inject a C# dll and overwrite functions and create jumps etc. like you can in C/C++.

In C# if you want to inject your DLL there are a handful of extra steps that need to happen before you can even do anything with the DLL. For example you must either:
- A. Manually load the .NET runtime into the process.
- B. Hook onto the existing loaded .NET runtime.

Afterward you then have to load your managed (C#) DLL through the said runtime that you either loaded manually or hooked onto. Then you need to create an instance of a type existing inside of your DLL in order for a function to be called such as a DllMain of your own type.

At that point, your DLL is then loaded and can make modifications.

It is a lot easier to just use a C/C++ DLL instead since the injection is very straight forward and does not need the extra .net framework steps needed.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 893

PostPosted: Tue Mar 10, 2015 1:44 am    Post subject: Reply with quote

atom0s wrote:
In C# if you want to inject your DLL there are a handful of extra steps that need to happen before you can even do anything with the DLL. For example you must either:
- A. Manually load the .NET runtime into the process.
- B. Hook onto the existing loaded .NET runtime.


Not certain if that reply was meant for him or me... the tool I linked for the OP just injects very simple wrappers for public static members (functional object pattern) in the manner of managed C++. When the linker sees the corflags, it loads the .NET runtime for you. It is kludgy, though, and I 100% agree that C# is the wrong tool for the job.

_________________
A nagy kapu mellett, mindig van egy kis kapu.
----------------------
Come on...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites