(C++) Basic Dll Help

Questions about programming languages and debugging
Post Reply
bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

(C++) Basic Dll Help

Post by bubzuru »

ok i know a few guys here code c++. soo

im trying to write a dll that kills the process that loads it. (seems stupid but i have my reasons)

Code: Select all

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

BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:      
        OutputDebugString("Hello From The Dll");
        ExitProcess(-69);   
        break;
      case DLL_PROCESS_DETACH:
        ExitProcess(-69);
        break;
      case DLL_THREAD_ATTACH:
        ExitProcess(-69);
        break;
      case DLL_THREAD_DETACH:
        ExitProcess(-69);
        break;
    }
    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}
the person that sent me the code also sent a compiled dll (that works) but
when i compile the code then load the dll it doesnt work

here is the program im using to load the dll

Code: Select all

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    char* DllName = "TestDll.dll"; /* Name of the dll to be loaded */
    
    /* Load the dll */
    printf("trying to load %s \n", DllName);
    HMODULE EmployeDll = LoadLibrary(DllName);  
    
    /* Give some output */
    if(EmployeDll != 0) 
    {
       printf("%s is loaded into memory \n\n", DllName);
      //FreeLibrary(EmployeDll); /* Unload the dll from memory */
    }
    else
      printf("error loading %s into memory \n\n", DllName);
      
    /* And then say goodbye */
    system("PAUSE");
    return EXIT_SUCCESS;
}
any ideas why the code is not working ?
[img]http://www.slackware.com/~msimons/slackware/grfx/shared/greymtlSW.jpg[/img]

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: (C++) Basic Dll Help

Post by Gogeta70 »

It can be a real pain in the ass to get a dll to work sometimes. Here's a DLL template that i wrote some time ago that works for me. Try it and see what happens.

Code: Select all

//#include "main.h"
#include <windows.h>


DWORD InjectThread(void* param);

extern "C" BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
		{
			CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) InjectThread, 0, 0, NULL);
		}
		break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

DWORD InjectThread(void *param)
{
	MessageBox(0, "Success!", "Message", MB_OK);
	
	return 0;
}

¯\_(ツ)_/¯ It works on my machine...

Post Reply