Why rundll gives missing entry? [duplicate] - c++

This question already has answers here:
How does RunDll32 work?
(2 answers)
Closed 8 years ago.
I have created a dead-simple DLL in Visual Studio 2010, a win32 project of type DLL.
Then I changed the DllMain to this:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBox(0,L"Hey there!",0,0);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
and I used rundll32 vahid-win32.dll,dllmain to run it. Message box shows, but after that it gives
Error in vahid-win32.dll
Missing entry: dllmain
What is wrong with my DLL? or with me? :-)
Thanks in advance

Your messagebox doesn't come from you passing DllMain function name. Rather it is automatically called. But Rundll32 looking for a export function with name DllMain with dllexport declaration as given below.
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBox(0,L"Hey there!",0,0);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec(dllexport) void mydllmain()
{
MessageBox(0,L"Hey there again!",0,0);
}
when you call RunDll32 with parameter mydllmain, it does me give both the message box with out error.

There's no reason to call DllMain via RunDLL, it's called automatically when the DLL is loaded.
Rather try running a custom function.
Aside from that, the problem is probably the exported name. You need to write a .def file for the DLL.
DllMain it is caused automatically, always. it to cause through rundll32, it will be caused, for this purpose it and is "entry point", you repeatedly try to cause it.

Related

CreateThread in DLL Terminating Prematurely

I am trying to load a DLL from Console Application. The simple console application is shown below:
#include <iostream>
#include <windows.h>
int main(){
HMODULE handleDll = LoadLibraryA("C:\\Tools\\TestDLL.dll");
if (handleDll)
{
std::cout << "DLL Loaded at Address: " << handleDll << std::endl;
}
FreeLibrary(handleDll);
}
The DLL is supposed to a POP a MessageBox which it does but just flashes on the screen instead of waiting for user input. The DLL code is below:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <Windows.h>
DWORD WINAPI ThreadProc( __in LPVOID lpParameter )
{
MessageBox(NULL, L"Hi From The Thread!", L"Pop a Box!", MB_OK);
return 0;
}
extern "C" __declspec(dllexport)
VOID PopMessageBox()
{
DWORD ThreadID;
HANDLE handleThread;
handleThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &ThreadID);
CloseHandle(handleThread);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
PopMessageBox();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
My question is..how do I make the code in the thread function fully execute without prematurely terminating or causing painful deadlocks? Apologies for my imperfect English and inexperience.
The reason is that you are doing something unsafe in your DllMain: you are calling CreateThread.
You are very limited in what you can do from within DllMain in response to a process attach, a fact that the documentation calls out:
There are significant limits on what you can safely do in a DLL entry point. See General Best Practices for specific Windows APIs that are unsafe to call in DllMain. If you need anything but the simplest initialization then do that in an initialization function for the DLL. You can require applications to call the initialization function after DllMain has run and before they call any other functions in the DLL.
The warning links you to "General Best Pratices" which, among other things, says to "[c]all CreateThread. Creating a thread can work if you do not synchronize with other threads, but it is risky."
Even without the risks associated with synchronizing with other threads, this code is flakey in other ways: for example, your main simply calls FreeLibrary and exits. The thread you had spawned in the DLL, which may literally be mid-execution, will have the code it's supposed to run unmapped. You're literally pulling the rug out from under it!

Attaching a DLL to a Game

I would like to attach my DLL to a game to add more features.
The DLL is 95% done, the problem is in finding the best and easy way to load this DLL from within the game.
My idea is to use this technique:
dinput_ori.dll (old dll)
dinput.dll (my dll that points to dinput_ori.dll)
I don't need to access any members of original DLL, but only load my new DLL.
I am searching for a generic DLL source code that can do the following:
bool WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
std::string DLLFileOri = "dinput_ori.dll";
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
// Load dll
LoadOriDll(DLLFileOri);
MsgBox("This DLL was loaded.");
break;
case DLL_PROCESS_DETACH:
// Close the DLL
UnloadOriDll(DLLFileOri);
break;
}
return true;
}
In this case the name of my DLL is "dinput.dll".
Is there a generic source code that can do this ?
Thanks!

calling functions inside DLLMAIN()

im trying to create DLL file and im having problem running functions inside DLLMAIN().
i want to do somthing like this :
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
INT APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved){
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
foo1();
break;
case DLL_PROCESS_DETACH:
foo2()();
break;
}
return true;
}
void foo1(){
//code
}
void foo2(){
// code
}
but it does not work.
when I'm trying to build the file im getting
'foo1' identifier not found
'foo2' identifier not found
what I'm doing wrong here?
thanks!
Probably because you have not declared the functions. Put a
void foo1();
void foo2();
before yout DllMain and it will compile.
But be warned. You should not do anything substantial in DllMain. Especially anything involving loading libraries directly or indirectly might result in deadlocks.

FreeLibrary blocks my application

Good day, everyone!
I wrote some dll, which I use in my project. In constuctor of class I load library lib = LoadLibrary(L"library.dll");, in destructor I free it using
if (lib)
FreeLibrary(lib);
Some times applications blocks when FreeLibrary is called, what I am doing wrong?
I implement dllMain but this is not solve my problem =(
in .h file
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved );
extern "C" {
static projector::CProjCorrectionsClient* corrections;
void DLLPROJECTOR_EXPORT CorrectionsInit (const char* configFile);
void DLLPROJECTOR_EXPORT CorrectionsApply ();
}
in cpp file
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
{
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
qDebug() << "DLL_PROCESS_ATTACH";
corrections = new projector::CProjCorrectionsClient();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
qDebug() << "DLL_PROCESS_DEATTACH";
delete corrections;
qDebug() << "Corrections delete success";
break;
}
qDebug() << "Out side dllmain switch";
return TRUE;
}
On console I see this messages:
DLL_PROCESS_ATTACH
Out side dllmain switch
<...>
Try to release library
DLL_PROCESS_DEATTACH
Corrections delete success
Out side dllmain switch
There is no message after FreeLibrary call and applications freeze.
Make sure you are not waiting for some thread inside DLL_PROCESS_DETACH.
When DllMain is called, system aquires internal critical section, which can cause deadlock if your code inside DllMain waits for some thread T to finish, this thread when finishing will also want to do DLL_PROCESS_DETACH, but since system critical section is aquired it will wait infinitelly causing deadlock.
The question should maybe be - 'what is the dll doing wrong?'. If the library is going to be unloaded because its ref count has reached zero then FreeLibrary will give the dll a chance to clean up, and will call DllMain with DLL_PROCESS_DETACH. Perhaps try debugging the dll to see whats going on when this event occurs.
Also if there is any static data in the DLL that could run destructors then perhaps the issue lies there.

How can i set an entrypoint for a dll

First i thought entry point in dlls DLLMain but then when i try to import it in C# i get an error that entrypoint wasn't found Here is my code:
#include <Windows.h>
int Test(int x,int y)
{
return x+y;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBox(0,L"Test",L"From unmanaged dll",0);
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
How can i set an entry point for my dll? And if you dont mind can you give me little explanation about entry point?
Like do i have to set import the same dll again and changing the entry point so i can use other functions in same dll? thanks in advance.
In your example, it seems you intend Test() to be an entry point however you aren't exporting it. Even if you begin exporting it, it might not work properly with C++ name "decoration" (mangling). I'd suggest redefining your function as:
extern "C" __declspec(dllexport) int Test(int x,int y)
The extern "C" component will remove C++ name mangling. The __declspec(dllexport) component exports the symbol.
See http://zone.ni.com/devzone/cda/tut/p/id/3056 for more detail.
Edit: You can add as many entry points as you like in this manner. Calling code merely must know the name of the symbol to retrieve (and if you're creating a static .lib, that takes care of it for you).