Code:
#include <tchar.h>
#include <windows.h>
typedef INT (WINAPI * lolMessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
lolMessageBoxA UchwytMessageBoxA;
UchwytMessageBoxA = lolMessageBoxA(GetProcAddress(GetModuleHandle("User32.dll"),"MessageBoxA"));
UchwytMessageBoxA(NULL,"TEST WINDOWS","TEXT 2", MB_OKCANCEL | MB_ICONWARNING);
return 0;
}
When i called MessageBoxA normally before making hook the hooking part worked good. But when i erased that part and leaved only the 'hooking' part it fails...
Solved:
#include <tchar.h>
#include <windows.h>
typedef INT (WINAPI * lolMessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
lolMessageBoxA UchwytMessageBoxA;
LoadLibraryA("User32.dll");
UchwytMessageBoxA = lolMessageBoxA(GetProcAddress(GetModuleHandle("User32.dll"),"MessageBoxA"));
UchwytMessageBoxA(NULL,"TEST WINDOWS","TEXT 2", MB_OKCANCEL | MB_ICONWARNING);
return 0;
}
As presented here, there's no reason for the process to have loaded user32.dll. So the call to GetModuleHandle will return NULL. Then the call to GetProcAddress will also fail and return NULL. And well, you can see what happens next.
When you called MessageBoxA directly, the linker would have produced an import table that forced the loader to load user32.dll. But when you removed the call to MessageBoxA, there was no need for it to do so.
You should replace the call to GetModuleHandle with a call to LoadLibrary, and so force the library to be loaded.
Finally, the real reason for you having asked this question is that your code neglects error checking. Don't do that. Check the values returned by the Win32 function calls. Had you done so you would have been able to work this out for yourself.
Related
I have been able to fine the WinMain function. Now I'm trying to make a win32 app:
#include <Windows.h>
int WINAPI WinMain() {
MessageBox(NULL,"Test", "Hello World", NULL);
return 0;
}
and then I get error C2731.
You haven't provided any parameters to the WinMain() method, and the error C2731 is saying that this method cannot be overloaded.
From MSDN:
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow);
Error C2664 is most likely your call to MessageBox, so try changing to something like:
MessageBox(NULL, L"Test", L"Hello World", NULL);
I figured it out I wasn't useint main.cpp as my main source code file and my WinMain needed to be switched to wWinMain
I have a project which causes two assertion failures in succession, AFTER the 'main'-function is completed. This is very problematic because it doesn't show me the piece of code causing the problem.
I was able to narrow the problem down somewhat. With an empty main-function:
int main(int argc,char* argv[])
{
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
return main(__argc,__argv);
}
everything is working just fine. However as soon as I use any class or function from a specific dll (which is part of the project), the problem starts occurring:
int main(int argc,char* argv[])
{
Color col(255,0,0,255);
col.r += 1;
int r = HeapValidate(GetProcessHeap(),0,nullptr);
std::cout<<r<<std::endl;
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
return main(__argc,__argv);
}
the 'Color' class is part of that dll, but it's not the cause of the issue. 'HeapValidate' returns 1, which means the heap is valid according to the documentation.
The dll in question contains hundreds of thousands lines of code, which makes debugging even more difficult.
On a similar question, I found the suggestion to use WinDBG, but I'm not sure what to do with the result:
http://pastebin.com/zV27b51Z
It confirms that the issue is some sort of memory corruption, but I still don't know where it originates from.
Is there any way to reliably find the origin, or am I stuck using trial and error?
My OS is Windows 8.1, I'm using Visual Studio 2013.
Run the application under debugger and press Retry button in assertion messagebox. DebugBreak will be called and the debugger will take coltrol.
I upgraded an old project from VC6 to VS2008, and now I get this compile error:
error C2731: 'wWinMain' : function cannot be overloaded
At these lines of code:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
The same project compiles fine under VC6.
Thanks everyone, I finally found the real culprit, it's a typo, I use LPSTR lpCmdLine instead of LPTSTR lpCmdLine. The real mystery is why it compiled at all under VC6 - it did use wWinMain, but somehow it was OK for lpCmdLine to be char * instead of WCHAR *.
Now I changed it to:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
And it works under VS2008 too.
Edit: I successfully compiled and even ran the program with this function definition under VC6:
int APIENTRY wWinMain(int *hInstance, float hPrevInstance, int *lpCmdLine, float nCmdShow)
{
MessageBox(0,L"Running.",0,0);
return 0;
}
Interestingly, replacing float nCmdShow to double nCmdShow does give a linker error, I assume because float is 32-bits but double is not.
For me it worked after changing WinMain to wWinMain in VS 2019
I had the same error with a Win32 Console Application. The fix was:
Open Project > Properties ...,
Expand Configuration Properties > Linker > System
Set SubSystem to Not Set
Click OK
I have been trying to learn multithreading for the first time but most of the examples I saw had a thread.h header file. Its not there in Dev C++.
I found this article, which says you can do it using windows.h.Sadly it doesn't provide any example programs. Can somebody please provide an example program which uses thread functions in windows.h or any other header file which is widely used and is present in Dev C++?
Thank You
Best option is to do threading in c or c++ use pthread.h header file
example work on gcc or dev c++
#include <stdio.h>
#include <stdlib.h>
#include<pthread.h>
int i=0;
void* fun()
{
for(i=0;i<100;i++)
printf("\nThe thread 1 is running");
}
void* van()
{
for(i=0;i<100;i++)
printf("\nthread 2 is running ");
}
int main()
{
pthread_t t1,t2;
pthread_create(&t1,NULL,fun,NULL);
pthread_create(&t2,NULL,van,NULL);
printf("\nI'm in main\n");
pthread_join(t2,NULL);
return 0;
}
Well windows.h has a function called CreateThread().
It's signature and other info can been seen here.
You need a callback function, that will start executing when you start the thread.
NULL can be passed to the first lpThreadAttributes argument.
HANDLE WINAPI CreateThread(
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_opt_ LPDWORD lpThreadId
);
I am new to C++, trying to make bootstrapper for an executable. I wanted to send additional parameters along with lpCmdLine
typedef int (__cdecl *BooterMain_t)(HINSTANCE, HINSTANCE, LPSTR, int);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
I load the library
HMODULE Program = LoadLibraryExA(".\\bin\\program.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
this is how I launch it
BooterMain_t procBooterMain = (BooterMain_t)GetProcAddress(Program,"BooterMain");
procBooterMain(hInstance, hPrevInstance, InternalParameters, nCmdShow);
May I know how to pass InternalParameters along with lpCmdline? Currently it seems to be ignored.
Edit: About Internal Parameters,
char InternalParameters[MAX_PATH] = {0};
_snprintf_s(InternalParameters, _TRUNCATE, "-verify -console");