Application crashes when using hooked functions - dll
I've been trying to hook a few winapi functions, e.g. DrawText, TextOut, ExtTextOut using detours. In case of injecting my dll into windows calculator the hooked functions are working fine until i press a button on the calculator, which then causes a crash. Injecting my dll into other processes causes similar behaviour. All hooks are working fine until i trigger certain actions like opening the new file dialog in notepad.
I've tested my dll on 32 and 64 bit windows 7 systems, both shows the same behaviour.
Any ideas on what could be causing the problem?
My dll:
main.cpp
#include <Windows.h>
#include <detours.h>
#include "hookedFunctions.h"
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch (ul_reason_for_call){
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pDrawTextW, myDrawTextW);
DetourAttach(&(PVOID&)pDrawTextA, myDrawTextA);
DetourAttach(&(PVOID&)pExtTextOutW, myExtTextOutW);
DetourAttach(&(PVOID&)pExtTextOutA, myExtTextOutA);
DetourAttach(&(PVOID&)pTextOutW, myTextOutW);
DetourAttach(&(PVOID&)pTextOutA, myTextOutA);
DetourAttach(&(PVOID&)myPolyTextOutW, myPolyTextOutW);
DetourAttach(&(PVOID&)myPolyTextOutA, myPolyTextOutA);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugStringA("Detoured successfully");
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)pDrawTextW, myDrawTextW);
DetourDetach(&(PVOID&)pDrawTextA, myDrawTextA);
DetourDetach(&(PVOID&)pExtTextOutW, myExtTextOutW);
DetourDetach(&(PVOID&)pExtTextOutA, myExtTextOutA);
DetourDetach(&(PVOID&)pTextOutW, myTextOutW);
DetourDetach(&(PVOID&)pTextOutA, myTextOutA);
DetourDetach(&(PVOID&)myPolyTextOutW, myPolyTextOutW);
DetourDetach(&(PVOID&)myPolyTextOutA, myPolyTextOutA);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugStringA("Detoured successfully");
break;
}
return TRUE;
}
hookedFunctions.h
#pragma once
#include <Windows.h>
// Declare function pointers to original Windows API functions
extern int (WINAPI *pDrawTextW)(HDC, LPCTSTR, int, LPRECT, UINT);
extern int (WINAPI *pDrawTextA)(HDC, LPCSTR, int, LPRECT, UINT);
extern BOOL (WINAPI *pTextOutW)(HDC, int, int, LPCTSTR, int);
extern BOOL (WINAPI *pTextOutA)(HDC, int, int, LPCSTR, int);
extern BOOL (WINAPI *pExtTextOutW)(HDC, int, int, UINT, const RECT*, LPCTSTR, UINT, const INT*);
extern BOOL (WINAPI *pExtTextOutA)(HDC, int, int, UINT, const RECT*, LPCSTR, UINT, const INT*);
extern BOOL (WINAPI *pPolyTextOutW)(HDC, const POLYTEXTW* , int);
extern BOOL (WINAPI *pPolyTextOutA)(HDC, const POLYTEXTA* , int);
// Declare our custom functions which are used to override the original Windows API functions
int myDrawTextW(HDC, LPCTSTR, int, LPRECT, UINT);
int myDrawTextA(HDC, LPCSTR, int, LPRECT, UINT);
BOOL myTextOutW(HDC, int, int, LPCTSTR, int);
BOOL myTextOutA(HDC, int, int, LPCSTR, int);
BOOL myExtTextOutW(HDC, int, int, UINT, const RECT*, LPCTSTR , UINT, const INT*);
BOOL myExtTextOutA(HDC, int, int, UINT, const RECT*, LPCSTR , UINT, const INT*);
BOOL myPolyTextOutW(HDC, const POLYTEXTW*, int);
BOOL myPolyTextOutA(HDC, const POLYTEXTA*, int);
hookedFunctions.cpp
#include "hookedFunctions.h"
// Create and initialize function pointers to original Windows API functions
int (WINAPI *pDrawTextW)(HDC, LPCTSTR, int, LPRECT, UINT) = DrawTextW;
int (WINAPI *pDrawTextA)(HDC, LPCSTR, int, LPRECT, UINT) = DrawTextA;
BOOL (WINAPI *pTextOutW)( HDC, int, int, LPCTSTR, int) = TextOutW;
BOOL (WINAPI *pTextOutA)(HDC, int, int, LPCSTR, int) = TextOutA;
BOOL (WINAPI *pExtTextOutW)(HDC, int, int, UINT, const RECT*, LPCTSTR, UINT, const INT*) = ExtTextOutW;
BOOL (WINAPI *pExtTextOutA)(HDC, int, int, UINT, const RECT*, LPCSTR, UINT, const INT*) = ExtTextOutA;
BOOL (WINAPI *pPolyTextOutW)(HDC, const POLYTEXTW* , int) = PolyTextOutW;
BOOL (WINAPI *pPolyTextOutA)(HDC, const POLYTEXTA* , int) = PolyTextOutA;
// Custom versions of the Windows API functions, having the same parameters,
// return type, and calling convention as the versions provided by the OS.
int myDrawTextW(HDC hDC, LPCTSTR lpchText, int nCount, LPRECT lpRect, UINT uFormat)
{
OutputDebugString(lpchText);
return pDrawTextW(hDC, lpchText, nCount, lpRect, uFormat);
}
int myDrawTextA(HDC hDC, LPCSTR lpchText, int nCount, LPRECT lpRect, UINT uFormat)
{
OutputDebugStringA(lpchText);
return pDrawTextA(hDC, lpchText, nCount, lpRect, uFormat);
}
BOOL myTextOutW(HDC hdc, int nXStart, int nYStart, LPCTSTR lpString, int cchString)
{
OutputDebugString(lpString);
return pTextOutW(hdc, nXStart, nYStart, lpString, cchString);
}
BOOL myTextOutA(HDC hdc, int nXStart, int nYStart, LPCSTR lpString, int cchString)
{
OutputDebugStringA(lpString);
return pTextOutA(hdc, nXStart, nYStart, lpString, cchString);
}
BOOL myExtTextOutW(HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc,
LPCTSTR lpString, UINT cbCount, const INT *lpDx)
{
OutputDebugString(lpString);
return pExtTextOutW(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx);
}
BOOL myExtTextOutA(HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc,
LPCSTR lpString, UINT cbCount, const INT *lpDx)
{
OutputDebugStringA(lpString);
return pExtTextOutA(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx);
}
BOOL myPolyTextOutW(HDC hdc, const POLYTEXTW *pptxt, int cStrings)
{
OutputDebugString(pptxt->lpstr);
return pPolyTextOutW(hdc, pptxt, cStrings);
}
BOOL myPolyTextOutA(HDC hdc, const POLYTEXTA *pptxt, int cStrings)
{
OutputDebugStringA(pptxt->lpstr);
return pPolyTextOutA(hdc, pptxt, cStrings);
}
Problem solved - I forgot to add WINAPI to my functions. Headers must look like this: int WINAPI myDrawTextW(HDC, LPCTSTR, int, LPRECT, UINT);
Related
Arduino - passing values by reference from lamda to singleton
Hello i am bigginer in programing and i have specific problem. I have been learning a new ways to write a code in small Arduino project. that project have multiple objects like distance measuring Senzor, led diods , temperature senzor, etc. And all this objects have its own menu where you can, for example, start a calibration or just get values. What i need is singleton class that has a function enter_esc() that need a int (*funct)() parameter basically function pointer. That enter_esc(int (*funct)()) function just looping function until you press escape pin which is defined. function Calibration() have inside some private: object data types like value or cali_value. so i tried to insert function Calibration() right into enter_esc(Calibration) but it won't compile becouse i didnt pass that vlaues by reference or copy. but what i found is lambda. i made a lamda similar to a Calibration() function and i passed values by reference &{//domething;} but i had to use enter_esc(std::function<int()>& funct) whitch is only int C++ standard library and not in Arduino C/C++ so my qestion is: [is there some way how to pass values by reference by using lambda to a singleton class in Arduino ?] (i konw it can be done differently but like i said i want to learn some new ways to program, also if you have some different way to make it i will by very happy to see it) 10Q for your time :) //Class.h #pragma once class events { private: static events e_instance; int p_menu, p_enter, p_esc, p_up, p_down; int menuValue; events(); public: events(const events&) = delete; static events& Get(); int ArrowUpDown(int maxVal); int ArrowUpDown(int p_up, int p_down, int maxVal); int enter_esc(const std::function<int()>& funct); }; events events::e_instance; class deviceBase : public Printables { public: const char* a_pin; int d_pin; String type; String deviceName; bool inUse; int actualCount; public: String getType() override; int getActualCount() override; String getName() override; String getInUse() override; }; class senzor : public deviceBase { private: int Value; int triggValue; public: int p_triggValue = 10; static int allSenzors; friend events; senzor(); ~senzor(); public: int getValue(); int Calibration(); void changeTriggVal(int x); void Reset(); void nullCalibration(); void Menu(int x); void setName(String deviceName); void setInUse(bool x); int getPin(); }; int senzor::allSenzors = 0; if you have some good advice to my code writing i will be also very glad //Class.cpp #include <iostream> #include <string> #include <functional> #define LOG(x) std::cout << x << std::endl; #define PINMENU 12 #define PINENTER 8 #define PINESC 9 #define PINUP 11 #define PINDOWN 13 using String = std::string; struct Printables { virtual String getType() = 0; virtual int getActualCount() = 0; ; virtual String getName() = 0; virtual String getInUse() = 0; }; #include "Class.h" events& events::Get() { return e_instance; } int events::ArrowUpDown(int maxVal) { if (maxVal) { menuValue = menuValue < maxVal ? menuValue++ : menuValue; } if (maxVal) { menuValue = menuValue > 0 ? menuValue-- : menuValue; } return menuValue; } int events::enter_esc(const std::function<int()>&funct) { if (1) { while (!p_esc) { auto f = funct; } } return 1; } int events::ArrowUpDown(int p_up, int p_down, int maxVal) { return 666; } events::events() {}; String deviceBase::getType() { return type; } int deviceBase::getActualCount() { return actualCount; } String deviceBase::getName() { return deviceName; } String deviceBase::getInUse() { String Status; Status = inUse == 1 ? "Active" : "Deactive"; return Status; } senzor::senzor() : Value(0), triggValue(1) { a_pin = "xx"; type = "[SENZOR]"; deviceName = "[UNKNOWN]"; inUse = 0; allSenzors++; actualCount = allSenzors; a_pin = 0; } senzor::~senzor() { allSenzors = 0; } int senzor::getValue() { Value = 4; return Value; } int senzor::Calibration() { triggValue = triggValue < getValue() ? getValue() : triggValue; p_triggValue = triggValue; return p_triggValue; } void senzor::changeTriggVal(int x) { p_triggValue = x; } void senzor::Reset() { p_triggValue = triggValue; } void senzor::nullCalibration() { triggValue = 1; } void senzor::setName(String deviceName) { this->deviceName = deviceName; } void senzor::setInUse(bool x) { inUse = x; } int senzor::getPin() { return 4; } int printsss() { return 1; } ////////////////////////////////this what i was writing about////////////////////////////// void senzor::Menu(int x) { events::Get().enter_esc([&]() { triggValue = triggValue < getValue() ? getValue() : triggValue; p_triggValue = triggValue; return p_triggValue; }); } but if i use lambda in arduino with enter_esc(int (*funct)()) i get this kind of error no matching function for call to 'events::enter_esc(senzor::Menu(int)::<lambda()>)'
C#: Error calling the camera on the tablet of Microsoft Surface-win10
[DllImport("avicap32.dll")] public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer); [DllImport("avicap32.dll")] public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam); public const int WM_USER = 0x400; public const int WS_CHILD = 0x40000000; public const int WS_VISIBLE = 0x10000000; public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10; public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; public const int WM_CAP_SET_PREVIEW = WM_USER + 50; public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; byte[] lpszName = new byte[100]; byte[] lpszVer = new byte[100]; capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100); IntPtr lwndC = capCreateCaptureWindowA(lpszName, WS_VISIBLE + WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0); //When the program runs to the following code, a camera selection dialog box (Microsoft Camera Front/Microsoft Camera Rear) appears. After selecting any of them, the code in the if statement is not executed,and the camera cannot run normally. Can anyone help me? Why is this happening, thank you. if (SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0)) { SendMessage(lwndC, WM_CAP_SET_PREVIEWRATE, 66, 0); SendMessage(lwndC, WM_CAP_SET_PREVIEW, true, 0); }
Visual-C++ Win32 C++ Application. How do I print variables to the main screen?
I have tried C++11 methods and C methods to convert the string before printing and they either: Returned a string of the same characters or didn't print anything at all. I just wanted to know 3 things really: Is there a fault in the online tutorials and examples? Is there a fault in my interpretation and implementation of the code? What could I do differently to get this working? // Yu-Gi-Oh! LP Calculator.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "Yu-Gi-Oh! LP Calculator.h" #define MAX_LOADSTRING 100 #define LP UINT #define ID CHAR // Global Variables: HINSTANCE hInst; // current instance WCHAR szTitle[MAX_LOADSTRING]; // The title bar text WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name // Global Life Point Variables struct Player { LP Lp; ID Name[MAX_LOADSTRING]; }; struct Player1, Player2, Player3, Player4; // Forward declarations of functions included in this code module: BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TEMP: Remove these initializations when preferences are implemented. Player1.Lp = 8000; Player2.Lp = 8000; Player3.Lp = 8000; Player4.Lp = 8000; LoadStringA(hInstance, (UINT)"John\0", Player1.Name, MAX_LOADSTRING); LoadStringA(hInstance, (UINT)"Phil\0", Player2.Name, MAX_LOADSTRING); LoadStringA(hInstance, NULL, Player3.Name, MAX_LOADSTRING); LoadStringA(hInstance, NULL, Player4.Name, MAX_LOADSTRING); // END TEMP // TODO: Place code here. // TODO: Load preferences file. // Initialize global strings LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_YUGIOHLPCALCULATOR, szWindowClass, MAX_LOADSTRING); WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_YUGIOHLPCALCULATOR)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_YUGIOHLPCALCULATOR); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); RegisterClassExW(&wcex); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_YUGIOHLPCALCULATOR)); MSG msg; // Main message loop: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: break; case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: { PAINTSTRUCT ps; RECT rect; HDC hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code that uses hdc here... GetClientRect(hWnd, &rect); CHAR b[] = { NULL }; sprintf((char* const)&b, "%s", Player1.Name); DrawTextA(hdc, b, ARRAYSIZE(b), &rect, DT_SINGLELINE | DT_CENTER | DT_TOP); //TextOutA(hdc, rect.left, rect.top, s, ARRAYSIZE(Player1.Name)); SelectClipPath(hdc, RGN_AND); EndPaint(hWnd, &ps); } break; case WM_DESTROY: case WM_CLOSE: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
By changing: #define ID CHAR to: #define ID LPCSTR and changing: ID Name[MAX_LOADSTRING]; to: ID Name; I made Player1.Name compatible with the type of the 2nd parameter of the DrawTextA function. I then changed the 3rd parameter to -1 instead of using ARRAYSIZE(). This means that the function checks the size of the string itself? So the function is called like this: DrawTextA(hdc, Player1.Name, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_TOP); All I needed to do then was Initialize the string with my values instead of using LoadStringA so I changed: LoadStringA(hInstance, (UINT)"John\0", Player1.Name, MAX_LOADSTRING); to: Player1.Name = "John";
link redeclared as different kind of symbol in
i have a objective-c program, i added a little lib with linked list in plain c. this is the header: typedef struct { int v; int w; } Edge; Edge EDGE(int, int); typedef enum tagTipoNodo { k_casellaPolozia, k_casellaKiller } tipoNodo; typedef struct node *link; struct node { int v; link next; }; typedef struct node2v *link2v; struct node2v { int v; int val; link2v next; }; link2v NEW2v(int v, int val, link2v next); void DEL2v(int v, link2v *lista); void Update2v(int v, int val, link2v lista); link sort2x(link2v lista); link sort2xDecr(link2v lista); int maxInList(link2v lista); struct graph { int V; int E; link *adj; int *tipo;}; typedef struct graph *Graph; Graph GRAPHinit(int); void GRAPHinsertE(Graph, Edge); void GRAPHremoveE(Graph, Edge); int GRAPHedges(Edge [], Graph G); Graph GRAPHcopy(Graph); void GRAPHdestroy(Graph); void GRAPHShow(Graph G); void ingr(); link nodeInDistance(Graph G, int A, int distance); link nodeInDistanceOfType(Graph G, int A, int distance, tipoNodo tipo); int distance(Graph G, int A, int B,tipoNodo tipo); int shortestPath(Graph G, int A, int B, tipoNodo tipo, int *percorso); link shortestPathList(Graph G, int A, int B, tipoNodo tipo); int distanceForAllocation(Graph G, int A, int B, tipoNodo tipo); void setPesoForNode(int n,int poliziotto); void resetPesoForNode(int poliziotto); void resetPesoForAllNodes(); void initCasPoliz(); link NEW(int v, link next); void DEL(int v, link *lista); link copyList(link l); int lengthList(link l); int lengthListOfType(Graph G,link l,tipoNodo tipo); void deleteList(link *lista); int isPresentInList(link lista,int val); void printCaselleVietate(); in simulator all compiles well, but when i try to compile for device this error occurs link redeclared as different kind of symbol in...[...] how can i fix this? thanks
i have found that i have an #include "linkedList.h" in helloworld.h and helloworld.m... in simulator all went well...strange thing! however another strange thing...: in "linkedList.h" i have link and link2k defined in a similar way...but link2k does not gave me that error...mha! use #import instead of #include. It's main function is to remove the need to use header guards
Is there a way to hide 'System.Windows.Forms.ListBox()' border?
Is there a way to hide System.Windows.Forms.ListBox() border?
If anyone is interested... this seems to work. Another thumbs down for the compact framework. public Form1() { InitializeComponent(); ShowBorder(listView1.Handle, false); } private void ShowBorder(IntPtr handle, bool bShow) { int style = GetWindowLong(handle, GWL_STYLE); if (bShow) { style |= WS_BORDER; } else { style &= ~WS_BORDER; } SetWindowLong(handle, GWL_STYLE, style); SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); } const int GWL_STYLE = -16; const int WS_BORDER = 0x00800000; const int SWP_NOSIZE = 0x1; const int SWP_NOMOVE = 0x2; const int SWP_FRAMECHANGED = 0x20; [DllImport("coredll.dll")] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("coredll.dll")] private extern static void SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); [DllImport("coredll.dll")] private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int uflags);