C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,470 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,734 people online right now. Registration is fast and FREE... Join Now!




Is it possible to write DLL using C++

2 Pages V  1 2 >  

Is it possible to write DLL using C++

shadachi

2 Jul, 2009 - 05:51 PM
Post #1

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

Is it possible to write DLL using C++ and use that dll to kill a specific process when detected.

It like i have a game , and i open a hack , the hack will be listed in the process list as hack.exe

So i would like that dll to detect the hack.exe and terminate it upon opening .

At least a guidance would be quite helpful. I'm still a newbie and want to learn more =)

User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Is It Possible To Write DLL Using C++

2 Jul, 2009 - 05:59 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,233



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
You'll need to find the process id. I wrote this program a few years ago, but it was written to run on Windows 98, so I have no idea what it would do on XP. You don't really have to have your program in a DLL, any reason why you want to write it as such? It sounds complicated enough without forcing to to be a DLL.
User is online!Profile CardPM
+Quote Post

c.user

RE: Is It Possible To Write DLL Using C++

2 Jul, 2009 - 07:33 PM
Post #3

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
dll.c
CODE


/* for dll you need special project for dll-building */

#include <stdio.h>

__declspec (dllexport) int Hello(const char *l)
{
    return printf("Hello, %s\n", l);
}


hello.c
CODE

#include <stdio.h>
#include <windows.h>

__declspec (dllimport) int Hello(const char *l);

/* downloads dll and runs function from it */
main()
{
    void *plib;
    int (*pHello)(const char *);

    if ((plib = LoadLibrary("library.dll")) == NULL)
       return 1;
    pHello = (void *) GetProcAddress(plib, "Hello");
    (*pHello)("Library");
    FreeLibrary(plib);
    return 0;
}


this is for C, but you can remake it to C++ simply
User is offlineProfile CardPM
+Quote Post

jeff666

RE: Is It Possible To Write DLL Using C++

3 Jul, 2009 - 04:53 AM
Post #4

D.I.C Head
**

Joined: 30 Dec, 2008
Posts: 161



Thanked: 6 times
My Contributions
This has nothing to do with DLL (!)
Just get the pid from the exe name and kill it with Win32 TP api.

This post has been edited by jeff666: 3 Jul, 2009 - 04:54 AM
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

3 Jul, 2009 - 07:58 AM
Post #5

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

Well , 1st of all this hack.exe uses hook if not mistaken to a game of mine .

the game.exe is pre-compiled and i do not have the source code of the exe.

How to Kill it with win32 TP api . I want it to be a dll so that it cannot be detected by the User .

And something like if Hack.exe shows up in process , it kills it . Automatically .

How bout using psapi.dll to list down the process .

Well , i found a code in the net but have no idea how to build it . Tried compiling but Gave me errors .

using psapi, this function list all process, check name and if the boolean is true check the filename too

(use the longname (if&gt;8 char) without the .exe), then for your sample just call GetProcessIDByName(L"notepad", true);to find it.


CODE

#include &lt;Psapi.h&gt;

DWORD GetProcessIDByName(LPWSTR szName, bool bCheckFileName) {
DWORD aProcesses[i], cbNeeded, cProcesses;
unsigned int i;

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;

long l = wcslen(szName);
WCHAR szProcessToFind[MAX_PATH];
wcscpy(szProcessToFind, szName);
if(wcsicmp(&szName[l-4], L".exe")) {
wcscat(szProcessToFind, L".exe");
}

// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);

// Print the name and process identifier for each process.
for ( i = 0; i &lt; cProcesses; i++ ) {
WCHAR szProcessName[MAX_PATH] = L"unknown";

// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);

// Get the process name.
if(hProcess) {
HMODULE hMod;
DWORD cbNeeded;

if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
GetModuleBaseNameW(hProcess, hMod, szProcessName, sizeof(szProcessName));
if(wcsicmp(szProcessToFind, szProcessName) == 0) {
CloseHandle( hProcess );
return aProcesses[i];
} else if(bCheckFileName) {
WCHAR *p, szFileName[MAX_PATH] = L"unknown", szLongFileName[MAX_PATH] = L"unknown";

GetModuleFileNameExW(hProcess, hMod, szFileName, sizeof(szFileName));
p = wcsrchr(szFileName, '\\');
if(wcsicmp(szProcessToFind, p+1) == 0) {
CloseHandle( hProcess );
return aProcesses[i];
}
if(wcschr(szFileName, '~')) {
if(GetLongPathNameW(szFileName, szLongFileName, sizeof(szLongFileName))) {
p = wcsrchr(szLongFileName, '\\');
if(wcsicmp(szProcessToFind, p+1) == 0) {
CloseHandle( hProcess );
return aProcesses[i];
}
}
}
}
}
}
CloseHandle(hProcess);
}
return 0;
}



This post has been edited by shadachi: 3 Jul, 2009 - 08:07 AM
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

5 Jul, 2009 - 02:46 AM
Post #6

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

Can anyone help me?? I have Errors in compiling it
User is offlineProfile CardPM
+Quote Post

AndyH1963

RE: Is It Possible To Write DLL Using C++

5 Jul, 2009 - 02:56 AM
Post #7

D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 197



Thanked: 21 times
My Contributions
I don't like the sound of the content of this thread and the way it appears to be going. Words such as"... so it cannot be detected by the user ..." seem to imply that you are attempting to write some Trojan type of process and I don't think anyone on here should be assisting in such activities.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Is It Possible To Write DLL Using C++

5 Jul, 2009 - 03:19 AM
Post #8

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(AndyH1963 @ 5 Jul, 2009 - 02:56 AM) *

I don't like the sound of the content of this thread and the way it appears to be going. Words such as"... so it cannot be detected by the user ..." seem to imply that you are attempting to write some Trojan type of process and I don't think anyone on here should be assisting in such activities.

Not really, if it's a game hack he probably won't be concerned with the user, more anti-cheats for said game.

Will give you my code shortly... Just gonna tie it up in a console app.

EDIT: Here you go... http://www.muppetalert.com/InjectionNutshell.rar
It will be much better if you start the process that you want to inject into from the EXE also, but this is just an example.

This post has been edited by MageUK: 5 Jul, 2009 - 03:43 AM
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

7 Jul, 2009 - 08:41 PM
Post #9

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

@andyh1963

I'm not writing a trojan virus . Why would i ? Doesn't benefit me at all .

Anyways . The code that i provide above used psapi to detect processes and kill it . But the sad thing is when i try to compile it gave me errors.

@MageUK
Thank you for the DLLinjector But the thing now is i cant compile the code as it has numerous Errors .

PLus injectors need to manually inject the dlls into the Exe right ? If that's the case , how can i make it auto inject or something .

As i said before , it serves as a anti-hack dll . It will search the process list find the hack.exe . Once found , it will kill the process thus safe-guarding the game.exe

Your Help is very very much appreciated . I'm grateful to all members that helped me . I now can see the end of the path clearly already =) .
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 01:08 AM
Post #10

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

bump..anyone can help ?
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 01:34 AM
Post #11

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Sorry, I didn't subscribe to the thread..

If you want it to auto-inject.. You'd have to create another process which would monitor for the game process to inject the hack... Which kindof defeats the object of having a DLL since you'd still have another application running for the "auto-inject".

The DLL Injector/DLL works fine.. You're compiling with VS, right?

(Subscribed this time)
User is offlineProfile CardPM
+Quote Post

Kanvus

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 02:04 AM
Post #12

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 451



Thanked: 39 times
Dream Kudos: 50
My Contributions
Dx i cant help cus im learning dll atm but have run into enough antihacks to see what youre doing. wats the game name?
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 02:36 AM
Post #13

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

@mageUK

yes , im compiling with VS.net 2008 usint C++ . tried to include the SDK Psapi.h and link it with psapi.lib with no results.

@kanvus

Well , the exe that i named it is Game.exe for now . to test it . = )
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 03:28 AM
Post #14

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Well I'll be blunt and say that if a cheater wanted to get around your "anti-cheat", unless you have significant knowledge in code randomisation (ie. polymorphing), using virtual addresses/functions (that are constructed at runtime etc) it will be easily gotten around.
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 03:30 AM
Post #15

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

Well , I'm just a newbie in this type of things. May this small anti-cheat system be my 1st stepping stone .

Can you compile the codes? i tried to compile but all errors .

Maybe i compile in wrong project file . i used win32 project with dll checked .

included the psapi.dll and linked to psapi.lib also useless
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

10 Jul, 2009 - 10:31 PM
Post #16

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

MageUK . I've written another dll apart from this . Then i tried to use ur DLL injector but it gave me Error 6 occured .. the handle is invalid .

i only change the game.exe and dll name . i placed the dll in the same folder inside the injector but it is the same .

std::string GameName = "game.exe";
std::string DllName = "my.dll";

This post has been edited by shadachi: 10 Jul, 2009 - 10:42 PM
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Is It Possible To Write DLL Using C++

11 Jul, 2009 - 03:16 AM
Post #17

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Is game.exe started?? That's the only reason you'd get handle is invalid.
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

19 Jul, 2009 - 01:17 PM
Post #18

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

Yes MageUK the process has started .. Weird . i can find the program's Pid using process viewer.

Weird thing is that i can inject into notepad but cant inject the dll into game.exe
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Is It Possible To Write DLL Using C++

19 Jul, 2009 - 01:19 PM
Post #19

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
It's possible that the game's running as admin and the injection isn't, and that could be why it's not finding it, have you tried it?
User is offlineProfile CardPM
+Quote Post

shadachi

RE: Is It Possible To Write DLL Using C++

19 Jul, 2009 - 01:20 PM
Post #20

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 21

but im using XP not vista.. admin rights shud be default right ?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic

Time is now: 11/8/09 02:42AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month