Tutorial Creating CLI C++ Class Library For Your C# Projects

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

SICGames88

Newbie
Full Member
Nobleman
Sep 6, 2015
70
768
0
How long you been coding/hacking?
Since 8 years old but on and off. Not consistent.
You could create a C++ dynamic library and then use inside your C# Application:

C++:
[DLLImport("HookEngine.dll")]
private extern static void Hook();
Or you could enjoy both worlds. Creating a CLI C++ Class Library to interact with your C# Application. This is just a template. Basic understanding how everything works.

When you launch VS 2017. You want to create a C# Application. It's under Other Languages, C#, Windows Form Application. You could use it in a WPF Form if you want. But for now, we're doing it in Windows Form Application.

image01-createCSharpApp.png


Name the application however you want. The next thing we'll do is adding a new project to our created solution. Right mouse click on the solution (inside the Solution Explorer) click on Add then click on New Project. Here, we're going to create our CLR Class Library. Under C++, CLR, Class Library. If you can't find CLR, you're going to have to install it. Go under Tools, Get Features and Tools. Tthen Individual Components, C++ CLR.

image02-addCLR_CPP_TO_PROJECT.png


image03-CreateCLIClassProject.png


Name however you want your CLR Wrapper to be called.

You'll notice it looks similar to C# coding. The good news is we can combine native code to this class library. For now, I have a public function called: SayHelloToEveryone. You're going to need to marshal a System::String to a LPCWSTR.

C++:
#pragma once
#include <Windows.h>
#include <vcclr.h> //-- needed to marshal String to LPCWSTR
#pragma comment(lib, "user32.lib") //-- needed to define MessageBox.

using namespace System;

namespace CSharpCLIWrapper {
    public ref class CSharpCLIWrapper
    {
    public: //-- just like native C++. We have to make it public. Or else
            //-- our function won't be visible.
            //-- Rule of thumb. Struct makes everything by default public.
            //-- Classes by default make everything private.
        
        void SayHelloToEveryone(System::String ^ message) {
            WIN32_FIND_DATA data;
            pin_ptr<const wchar_t> wMessage = PtrToStringChars(message);
            MessageBox(0, wMessage, L"Greeting!", 0);
        }
        // TODO: Add your methods for this class here.
    };
}
Compile and build the CLI C++ Class Library. Once that's done, head over to your C# project. Click on the References. Right mouse click and click on Add Reference. Here we're adding our newly compiled CLI C++ Class Library.

image04-AddCLIReferenceToCSharpProject.png


We're going to browse to the path the CLI C++ Class Library was compiled in. For me, it was the project's solution folder under Debug. Double click on that, it'll be automatically checked. Click on OK. Inside the Form1.cs. We're going to have to add a few lines of code.

Form1.cs:
using CSharpCLIWrapper; //-- the name of the namespace you've chosen for your
                        //-- CLI Class Library.
Double click on the form inside Form Designer. That'll generate the Form1_Load event.

C#:
CSharpCLIWrapper.CSharpCLIWrapper csharpCLIWrapper = new CSharpCLIWrapper.CSharpCLIWrapper();         
csharpCLIWrapper.SayHelloToEveryone("Hello Guided Hacking Forum.");
Normally you shouldn't name your namespace and class with the same name. Never, really. So keep them different. When you compile your C# Application and launch it. It'll show the greeting message.

Why not just use DLLImport? You could. But it'll be much easier to create your own class library that's made to intertwine with C#. Only thing is you'll need to marshal (convert) native declarations to C#. Since .NET Framework uses Strings instead of LPCWSTR or LPCSTR. The .NET Framework needs to know how to interpret it.

This is only for those who want to combine their native functions to a C# application. If they're making a C# Aimbot injector or what not and have native functions they've made, they could use CLR C++ to directly be tied in their C# application.

And lastly, if you want to make sure the CLR Class Library is depended on the C# application to run. Right mouse click on the C# project, click on Build Dependencies. Then check mark the CLR C++ Class Library. This will cause the program to throw an exception in case the CLR C++ Class Library isn't within the application's startup path.

This simple tutorial was just a template for others to use. In case, they plan on making their own Aimbot engine or whatever.

I'll try to think of more stuff to write.
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,180
78,998
2,398
This is awesome, we needed a guide on this for a long time!
 
  • Like
Reactions: SICGames88
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods