-3

I'm a new to the world of C and programming generally! Trying to create a simple DLL file that prints a messagebox Hello World!

Here's the cpp

//main.cpp    


#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{

switch (fdwReason)
{
    case DLL_PROCESS_ATTACH:
        // attach to process
        // return FALSE to fail DLL load
        break;

    case DLL_PROCESS_DETACH:
        // detach from process
        break;

    case DLL_THREAD_ATTACH:
        // attach to thread
        break;

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

and here the h

//main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

The point is that once i make a console that loads the DLL i have to call the function from it! How could i do to simply export all functions without giving them a name but just load everything from the dll in the console? like what if someone wanna use my dll and doesn't know what to call? or if i got an exe calling a dll e want to create a custom dll with my functions to be called? This is not for hacking porpuses! sorry for skills thx

  • Almost certainly not the issue, but `__MAIN_H__` is UB. Don't use a double underscore, or a single underscore followed by a capital letter. – Bathsheba Jul 25 '17 at 10:53
  • maybe have a look at DllMain? – The Techel Jul 25 '17 at 10:54
  • 1) DLLs are not meant to be "launched", as you seem to want. Executables are launched. DLL is just that - dynamically-linked library. It is library of functions, and the user of the library decides what they want to use. 2) Explicit statement _This is not for hacking porpuses_ suggests otherwise. – Algirdas Preidžius Jul 25 '17 at 11:03
  • 1
    @TheTechel Well, creating `MessageBox` inside `DllMain` is **not** recommended. You can safely call functions only from kernel32.dll inside DllMain (even those, with exceptions), and `MessageBox` is defined in `user32.dll`. – Algirdas Preidžius Jul 25 '17 at 11:07
  • I can't really find a question in here. An executable contains `int main()`, which is called automatically. It may (directly or indirectly) call `SomeFunction` from the DLL above. But `SomeFunction` is executed when and only when it's called. – MSalters Jul 25 '17 at 11:28

1 Answers1

0

"what if someone wanna use my dll and doesn't know what to call" - if somebody uses your DLL, he needs to know what to call, there are not simple ways around (therefore you usually provide the *.h with the function declarations).

There actually are ways to enumerate functions provided by a DLL (see e.g. here: Win32 API to enumerate dll export functions?), however that is not how most DLL libraries are normally used.

Note that you can enumerate the function names, but without the header it might not be always easy to find out which parameters to pass to each function and what type the functions returns. It can in general be done with C++ exported functions (the C++ name mangling provides the parameter count and type information), but not as easily if the function is exported as plain "C" function (where the name is a simple function name without mangling).

EmDroid
  • 5,918
  • 18
  • 18