I am trying to get properties from an HWND. I'm used information from Using Window Properties, but the example below is not working for me. I'm getting an error while compiling my code.
argument of type "BOOL (__stdcall *)(HWND hwndSubclass, LPCSTR lpszString, HANDLE hData)" is incompatible with parameter of type "PROPENUMPROCEXW"
Here is my callback function
BOOL CALLBACK PropEnumProcEx(HWND hwndSubclass, LPCSTR lpszString, HANDLE hData) {
return TRUE;
}
and this how I'm using it
EnumPropsEx(hwnd, PropEnumProcEx, NULL);
Does someone have any suggestions of how this can be fixed?
LPCSTR lpszString should be LPTSTR lpszString. This argument should accept a pointer to either ANSI or Unicode null-terminated string. PROPENUMPROCEXW indicates that you are building Unicode application so EnumPropsEx macro expands to EnumPropsExW call so you need to provide callback accepting wide string as an argument. Typically you should always explicitly call Unicode variants of API functions.
Also you are missing last argument ULONG_PTR dwData.
So your callback should look like:
BOOL CALLBACK
PropEnumProcEx(HWND hwndSubclass, LPTSTR lpszString, HANDLE hData, ULONG_PTR dwData)
{
return TRUE;
}
Related
I am trying to hook StartDocW to intercept printing via mhook. I use AppInit_DLLs to load my library.
DLL code is simple:
#include <windows.h>
#include "mhook/mhook-lib/mhook.h"
using StartDocPtr = int(*)(HDC, const DOCINFO*);
StartDocPtr orig;
int HookedStartDocW(HDC hdc, const DOCINFO* lpdi) {
return orig(hdc, lpdi);
}
BOOL WINAPI DllMain(__in HINSTANCE, __in DWORD Reason, __in LPVOID) {
orig = (StartDocPtr)GetProcAddress(GetModuleHandle("gdi32"), "StartDocW");
switch (Reason)
{
case DLL_PROCESS_ATTACH:
Mhook_SetHook((PVOID*)&orig, &HookedStartDocW);
break;
case DLL_PROCESS_DETACH:
Mhook_Unhook((PVOID*)&orig);
break;
}
}
Hook is working and printing is done OK. But If I change HookStartDocW to following:
int HookedStartDocW(HDC hdc, const DOCINFO* lpdi) {
char buf[40];
GetModuleFileName(NULL, buf, 40);
return orig(hdc, lpdi);
}
Programs on printing will crash immediately. Even if I just leave char buf[40] and comment GetModuleHandle - program will hang. Why is this happening?
Moreover, if program crashes\hangs on printing (if I add anything besides return orig(hdc, lpdi)) - PC starts to behave very weirdly, refusing to run programs, etc. If I reboot it - Windows just endlessly spins on boot screen, the only way to bring it back to live - is to boot via liveCD and rename\delete my hook DLL.
Printing programs: Excel 2016, notepad.
Compiler - MSVC 2015, x64 release DLL compilation, using MBCS instead of unicode.
Your hook is declared wrong.
Look at the actual declaration of StartDocW() in Wingdi.h:
__gdi_entry WINGDIAPI int WINAPI StartDocW(__in HDC hdc, __in CONST DOCINFOW *lpdi);
You can ignore __gdi_entry. WINGDIAPI simply resolves to __declspec(dllimport). What is important in this declaration is the WINAPI.
Like almost all Win32 API functions, StartDocW() uses the __stdcall calling convention. The WINAPI macro resolves to __stdcall.
Your code does not specify any calling convention at all, so it uses your compiler's default, which is usually __cdecl instead. So you are mismanaging the call stack. That is why your code crashes.
You are also using DOCINFO when you should be using DOCINFOW instead. It is clear in your code that you are compiling for MBCS and not for UNICODE, so DOCINFO maps to DOCINFOA. You can't pass a DOCINFOA to StartDocW(), it expects a DOCINFOW instead.
You need to fix your declarations, eg:
#include <windows.h>
#include "mhook/mhook-lib/mhook.h"
using StartDocPtr = int (WINAPI *)(HDC, const DOCINFOW*);
StartDocPtr orig = nullptr;
int WINAPI HookedStartDocW(HDC hdc, const DOCINFOW* lpdi) {
//...
return orig(hdc, lpdi);
}
BOOL WINAPI DllMain(__in HINSTANCE, __in DWORD Reason, __in LPVOID) {
orig = (StartDocPtr) GetProcAddress(GetModuleHandle(TEXT("gdi32")), "StartDocW");
switch (Reason)
{
case DLL_PROCESS_ATTACH:
Mhook_SetHook((PVOID*)&orig, &HookedStartDocW);
break;
case DLL_PROCESS_DETACH:
Mhook_Unhook((PVOID*)&orig);
break;
}
}
I haven't been programming in c++ very long, but need to make an Win32 application for my school. The teacher helped me a lot with information but after a few days of trying I am still stuck.
Errors:
error C2440: '=' : cannot convert from 'const char [11]' to 'LPCWSTR'
error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'
error C2664: 'TextOutW' : cannot convert parameter 4 from 'char *' to 'LPCWSTR'
IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
Don't know if all the other suff is right, but i only get those 4 error now
cpp file:
/* Hoofdstuk 10, User Interface */
#include "Callback_NYCM.h"
// UI
int WINAPI WinMain(HINSTANCE thisInstance,HINSTANCE prevInstance,LPSTR lpCmdLine,int nShowCmd)
{
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
HWND hwnd;
WNDCLASSEX wndclassex; //struct_WNDCLASSEX via windows.h
// toekenning
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = thisInstance;
wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor(thisInstance,IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = "WNDCLASSEX";
wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// functie aanroep
RegisterClassEx(&wndclassex);
// IfThen -> CreateWindows
if(!(hwnd = CreateWindowEx(NULL,"WNDCLASSEX","Hoofdstuk 10",WS_OVERLAPPEDWINDOW
| WS_VISIBLE,50,50,650,300,NULL,NULL,thisInstance,NULL)))
{
return 0;
}
// logische structuur
while(GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
};
header file:
/*Hoofdstuk 10, Deelnemer.h*/
//Declaratie
class Deelnemer
{
private:
char* nm;
public:
//Constructor
Deelnemer(){
}
//Methoden = prototype
void Deelnemer::Invoeren();
char* Deelnemer::WeergevenNaam();
};
//Implemenmtatie.Invoeren
void Deelnemer::Invoeren()
{
nm = "Roy";
}
//.Weergeven
char* Deelnemer::WeergevenNaam()
{
return nm;
}
callback_NYCM.h:
/*Hoofdstuk 10, Callback_NYCM*/
#include "Windows.h"
#include "Deelnemer.h"
// prototype
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam);
//Implementatie
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
//Constructie
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
WNDCLASSEX wndclassex;
// HWND hwnd;
Deelnemer deelnemer1;
//UI
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,&ps);
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
{
return DefWindowProc(hwnd,message,wparam,lparam);
}
break;
}
return 0;
}
I think my constructor or something like that is wrong and my return value for char* Deelnemer::WeergevenNaam()
Could somebody explain me what is wrong in my code so I know how to get it working?
UPDATE:
Updating your application requires to use UNICODE string literals
throughout, i.e. L"MyString" instead of "MyString". You also need to
use WCHAR/wchar_t in place of char
But how do i do this with my code, could somebody help?
UPDATE 2:
That solved al lot of errors!
But i have some more errors left in this part
Deelnemer deelnemer1;
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,&ps);
return 0;
}
so the errors are on line: deelnemer1.WeergevenNaam()
-TextOutW' : cannot convert parameter 4 from 'char *' to 'LPCWSTR'
-IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
UPDATE 3:
After some testing i found a solution (like you guys said below) But now i only got this one left:
TextOut (hdc,50,50,deelnemer1.WeergevenNaam(), // on the deelnemer1.weergevenNaam()
with error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char *' to 'LPCWSTR'
Your code is written to compile as ANSI but your solution settings include _UNICODE/UNICODE. You can either set your solution to use ANSI encoding by changing Character Set (on the General node of the Configuration Properties) from Use Unicode Character Set to Use Multi-Byte Character Set or update your application code (the latter is recommended).
Updating your application requires to use UNICODE string literals throughout, i.e. L"MyString" instead of "MyString". You also need to use WCHAR/wchar_t in place of char (where applicable) and call the wide versions of the Windows API. For many API calls there exists a wide version that has a W at the end, e.g. CreateWindowExW. If you are using the Standard C++ Library you will also want to make sure to use the UNICODE variants where character encoding is necessary (e.g. std::wstring instead of std::string). Additional information can be found at Text and Strings in Visual C++.
A bit more background about what is going on here: The Windows API and Microsoft's CRT implementation can be used to compile code using ANSI/Multi-Byte character set or UNICODE character set. To support both character encodings the C/C++ preprocessor replaces the respective character types and API calls with the specific implementations depending on whether or not the _UNICODE and UNICODE preprocessor symbol is defined.
For example, the call CreateWindowEx is expanded to either CreateWindowExA or CreateWindowExW. Both implementations have different parameter types for string arguments (char* and wchar_t* respectively). To use ANSI/Multi-Byte encoding the call would be CreateWindowExA(NULL,"WNDCLASSEX",...). For UNICODE it would look like CreateWindowExW(NULL,L"WNDCLASSEX",...).
To see what the code looks like after the preprocessor is through you can use the /P or /E compiler switches (assuming you're using the Microsoft Compiler).
Note: Don't forget to read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
simple answer - with no explanation of why
change quoted strings to L"foo"
change char to wchar_t
LPCWSTR -> Long Pointer to Constant Wide Character String.
All of your error messages reduce to some form of char[XX] is not the same type as wchar_t[XX].
Microsoft's Win32 API's are designed to compile either with ANSI strings, char, or wide character strings, wchar_t. This is why many Win32 function have two signatures, one ending in A, the other in W.
Example:
CreateWindowExW
CreateWindowExA
CreateWindowEx - A macro that expands to one of the above signatures depending on your build configuration. If you define UNICODE or _UNICODE it will expand to the W version.
Typically you use the macros when you are programming in Win32, and wrap all of your string literals with the _T macro. When defining character arrays that are strings, you'll want to use LPCTSTR and LPTSTR. These are also macros which will expand to either char * or wchar_t * depending on your build settings.
I have it working. The use wchar_t * and L"string". The problem with my code was more the structure because copy/pasting to a new project solved my errors. Thanks guys!
By choosing "not set" on Character Set in Project > Property > Configuration Properties > General, I was able to remove errors C2440 and C2264.
I'm learning DirectX for a project for university, My compiler however shows this error:
"10 C:\Dev-Cpp\projectdirectx\main.cpp cannot convert `const wchar_t*' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'"
I don't understand this at all, I don't know what I did wrong, What i'm attempting to do is a simple messagebox, Here's the code:
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
MessageBox(NULL, L"testing testing",
L"C++", MB_OK);
return 0;
}
What do I do?
You are building an ANSI build, but using a Unicode literal. Either use regular string literals (drop L prefix in front), or switch to a Unicode build.
Just remove the L prefix from the strings.
The problem is that you are trying to invoke wide character version of MessageBox function, which is not the case in your build environment. Just remove the L macro in the function call and it should work.
Hello friends how can I convert type "int" to type "LPCSTR"? I want to give the variable "int cxClient" to the "MessageBox" function's second parameter "LPCSTR lpText". Following is the sample code:
int cxClient;
cxClient = LOWORD (lParam);
MessageBox(hwnd, cxClient, "Testing", MB_OK);
But it does not work. The following function is the method signature of the "MessageBox" function:
MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType);
Convert the int to a string by using the right sprintf variant
TCHAR buf[100];
_stprintf(buf, _T("%d"), cxClient);
MessageBox(hwnd, buf, "Testing", MB_OK);
you need <tchar.h>.
I think _stprintf is the quick answer here - but if you want to go pure C++ like David suggests, then
#ifdef _UNICODE
wostringstream oss;
#else
ostringstream oss;
#endif
oss<<cxClient;
MessageBox(0, oss.str().c_str(), "Testing", MB_OK);
You need
#include <sstream>
using namespace std;
using std::to_string
std::string message = std::to_string(cxClient)
http://en.cppreference.com/w/cpp/string/basic_string/to_string
I want to modify the native functions available in Windows API, such as CreateWindowEx or ShowWindow in a way that when an application is compiled with those functions included, it will instead call my functions, perform the tasks there, and then call the original native function.
In other words, I want to in a way proxy the functions, while still using the same names (so if a program was written to be compiled with the native API, simply adding these functions would modify the way those native functions are handled)
HWND WINAPI CreateWindowEx(
__in DWORD dwExStyle,
__in_opt LPCTSTR lpClassName,
__in_opt LPCTSTR lpWindowName,
__in DWORD dwStyle,
__in int x,
__in int y,
__in int nWidth,
__in int nHeight,
__in_opt HWND hWndParent,
__in_opt HMENU hMenu,
__in_opt HINSTANCE hInstance,
__in_opt LPVOID lpParam
) {
//my custom code here....
// done with my custom code... so now I want to run the native function
return CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}
This (for obvious reasons) gives a stack overflow, as it keeps on calling itself over and over. What I would want it to do is, when it is called, it runs through the custom function I've created, and thereafter runs the native functions available in Windows API.
I am quite new to c++, but for example in many other languages, I could store a reference to the native function under another name, which I could then call inside my custom function. Is there anything similar available in c++?
As I've written in the comment, the parent of many hooking libraries was probably the microsoft Detours
Now that it isn't free anymore there are various alternatives. Here there is a comparison of some of them (link removed. I'm not sure it was safe. Try googling for "Microsoft Detours is a library utilized in the particular interception" and select a source or more simply for Detours Alternatives.
Mmmh it seems the only free alternative at this time are http://easyhook.codeplex.com/ and http://www.codeproject.com/KB/system/mini_hook_engine.aspx
There is a SO question: Detours alternative for Registry interception if you are interested.
One interpretation of your question is that you have a project, with source code, and you want to change that project so it uses your own versions of certain winapi functions.
Here is a solution which you can implement for each imported API function. Example here is for ShowWindow:
#define ShowWindow Deleted_Winapi_ShowWindow // prevent windows.h from defining ShowWindow
#include <windows.h>
#undef ShowWindow
namespace HiddenWinapi
{
extern "C"
{
// Do what windows.h does, but hide it inside a namespace.
WINUSERAPI BOOL WINAPI ShowWindow( __in HWND hWnd, __in int nCmdShow);
}
}
// make your own function to be called instead of the API, and delegate to the actual API in the namespace.
BOOL WINAPI ShowWindow(HWND hwnd, int nCmdShow)
{
// ... do stuff ...
// call the original API
return HiddenWinapi::ShowWindow(hwnd, nCmdShow);
}
To use this solution for CreateWindowEx, you need to stub the actual imported function name (e.g. CreateWindowExW), because CreateWindowEx is just a macro which expands to CreateWindowExW or CreateWindowExA.
Here is a solution which replaces the macro with your own, but I think in all cases it would be better to use the above solution.
#include <windows.h>
#undef CreateWindowEx
// Note that this is a unicode-only version. If your app mixes A and W versions, see
// the solution below for non-macro APIs.
HWND WINAPI CreateWindowEx(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
// ... do stuff ...
// call the REAL function.
return CreateWindowExW(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}
If you want to do this yourself the easiest way is to modify the import address table in the PE (portable executable) header. This is not trivial, though.
However, I believe there's a standard library for what you want called Detours. I've never used that one myself, though, because it wasn't around when I started doing this, so I have a - not for public consumption - library for doing it via the import table when I need it.