Im writing an aimbot for a game called assault cube and I have written a function to grab all of the players in the game. Im currently trying to get the team of the player that my function gets, but for some weird reason I get an error.
The Error: Unhandled exception at 0x08ED66D0 (finalaimbot.dll) in ac_client.exe: 0xC0000005: Access violation reading location 0x000003CA.
This is really weird because the address im reading from is way higher than 0x3CA.
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <windows.h>
#include <iostream>
#include <string>
DWORD EntityList = 0x50F4F8;
DWORD Player = *reinterpret_cast<int*>(0x50F4F4);
DWORD OnlinePlayers = 0x50F500;
DWORD MouseX = 0x40;
DWORD MouseY = 0x44;
DWORD HeadX = 0x4;
DWORD HeadY = 0x8;
DWORD HeadZ = 0xC;
DWORD Team = 0x3C2;
using namespace std;
struct vec3 {
float x, y, z;
};
float GetDistance3D(vec3 m_pos, vec3 en_pos)
{
float res = (float)(sqrt(((en_pos.x - m_pos.x) * (en_pos.x - m_pos.x)) + ((en_pos.y - m_pos.y) * (en_pos.y - m_pos.y)) + ((en_pos.z - m_pos.z) * (en_pos.z - m_pos.z))));
return res;
}
void aim(vec3 pos) {// function for aiming at a position
*reinterpret_cast<float*>(Player + MouseX) = pos.x;
*reinterpret_cast<float*>(Player + MouseY) = pos.y;
}
DWORD WINAPI MainThread(LPVOID lpArgs) {
while (true) {
int onlinePlayers = *reinterpret_cast<int*>(OnlinePlayers);
int myTeam = *reinterpret_cast<int*>(Player + Team);
vec3 MyPos;
MyPos.x = *reinterpret_cast<float*>(Player + HeadX);
MyPos.y = *reinterpret_cast<float*>(Player + HeadX);
MyPos.z = *reinterpret_cast<float*>(Player + HeadX);
if (onlinePlayers > 1) {
for (DWORD x = 0x4; x <= (0x4 * (onlinePlayers - 1)); x += 0x4) {
DWORD EnemyBase = *reinterpret_cast<int*>(EntityList + x);
DWORD EnemyTeam = *reinterpret_cast<int*>(EnemyBase + Team);
vec3 EnemyPos;
EnemyPos.x = *reinterpret_cast<float*>(EnemyBase + HeadX);
EnemyPos.y = *reinterpret_cast<float*>(EnemyBase + HeadY);
EnemyPos.z = *reinterpret_cast<float*>(EnemyBase + HeadZ);
float Distance = GetDistance3D(MyPos, EnemyPos);
if (EnemyTeam == myTeam) {
MessageBoxA(0, "Enemy is on your team", "Enemy Detected", 0);
}
else {
MessageBoxA(0, "Enemy is not on your team", "Enemy Detected", 0);
}
}
}
}
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
CreateThread(0, 0, MainThread, 0, 0, 0);
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
I am trying to draw a line over the live video streaming in direct show application. Am successfully drawing a line but if I continuously want to create a line by calling the below function, i got an error E_OUTOFMEMORY from the SetAlphaBitmap(). Am calling drawlineOverlay() function one after the other and 8 of the calling is ok, but the ninth and subsequent ones do not. SetAlphaBitmap returns S_OK for the first 8 callings and then the 9th returns 8007000E. But the video streaming is going peacefully only the overlay diagram was hanged. If I continuously call that function after sometimes video streaming is stopped and hanging.
Why is this error occuring?
void drawlineOverlay(HWND m_hwndApp)
{
int cx, cy;
HRESULT hr;
HBITMAP hbm;
RECT rcClient;
GetResolution(&cx,&cy);
GetClientRect(m_hwndApp,&rcClient);
HDC hdc = GetDC(m_hwndApp);
if (hdc == NULL)
{
return E_FAIL;
}
HDC hdcBmp = CreateCompatibleDC(hdc);
if (hdcBmp == NULL)
{
return E_FAIL;
}
hbm = CreateCompatibleBitmap(hdc,cx,cy);
BITMAP bm;
if (0 == GetObject(hbm, sizeof(bm), &bm))
{
DeleteDC(hdcBmp);
return E_FAIL;
}
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);
if (hbmOld == 0)
{
DeleteDC(hdcBmp);
return E_FAIL;
}
//To draw line
drawLine1(xx1, yy1, xx2, yy2,hdcBmp,2);
VMR9AlphaBitmap bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
bmpInfo.dwFlags = VMRBITMAP_HDC | VMRBITMAP_SRCCOLORKEY;
bmpInfo.hdc = hdcBmp;
SetRect(&bmpInfo.rSrc, 0, 0, bm.bmWidth, bm.bmHeight);
bmpInfo.rDest.left = 0.f;
bmpInfo.rDest.top = 0.f;
bmpInfo.rDest.right = 1.0f;
bmpInfo.rDest.bottom = 1.0f;
// Set the transparency value (1.0 is opaque, 0.0 is transparent).
bmpInfo.fAlpha = 0.5f;
bmpInfo.clrSrcKey = RGB(0,0,0);
if(m_pVideoRender != NULL)
{
IVMRMixerBitmap9* pBmp;
hr = m_pVideoRender->QueryInterface(IID_IVMRMixerBitmap9, (LPVOID *)&pBmp);
if (SUCCEEDED(hr))
{
hr = pBmp->SetAlphaBitmap(&bmpInfo);
if(FAILED(hr))
{
PrintMessage(L"pBmp->SetAlphaBitmap hr = 0x%x GetLastError() = %d\r\n",hr,GetLastError());
}
pBmp->Release();
//SAFE_RELEASE(pBmp);
}
}
// Clean up.
ReleaseDC(m_hwndApp, hdc);
DeleteBitmap(hbm);
DeleteObject(SelectObject(hdcBmp, hbmOld));
DeleteDC(hdcBmp);
}
void drawLine1(int xx1, int yy1, int xx2, int yy2,HDC hdcBmp)
{
RECT clntRc;
int temp,s1,s2,swap;
double dx,dy,p,x,y;
x = xx1;
y = yy1;
dx = abs(xx2 - xx1);
dy = abs(yy2 - yy1);
s1 = sign(xx2 - xx1);
s2 = sign(yy2 - yy1);
swap = 0;
if (dy > dx)
{
temp = dx;
dx = dy;
dy = temp;
swap = 1;
}
p = 2*dy - dx;
for (int i = 0; i < dx; i++)
{
clntRc.left =x;
clntRc.top = y;
clntRc.right = x+g_Thickness;
clntRc.bottom =y+g_Thickness;
FillRect(hdcBmp,&clntRc,CreateSolidBrush(RGB(0,255,0)));
while (p >= 0)
{
p = p - 2*dx;
if (swap)
x += s1;
else
y += s2;
}
p = p + 2*dy;
if (swap)
y += s2;
else
x += s1;
}
}
CreateSolidBrush(RGB(0,255,0)) is leaking resources.
You need to DeleteObject the handle that is returned by CreateSolidBrush(RGB(0,255,0)): https://msdn.microsoft.com/en-us/library/windows/desktop/dd183523(v=vs.85).aspx
When Ever I compile my program it say "Debug Assertion Failed".
And when I click ignore twice my app shows up.
How can I fix it?
The whole code is below in case you want to test it for your self. The app increases in number as the scroll bar is moved to the right and decreases as the scroll bar is moved to the left. But my main problem is the debug assertion failed. If you can please post the part of the code that has problem and how I can fix it or fix it and post the code. Is there any way to get rid of that run time error with out using a try and catch. I mean to fix that problem please. This is a project and I have only a week and a half to do it. If you can help me with this project your help would be greatly appreciated. I am fairly new to visual c++. Part of the error is:
Program E:\BHCCHardwareStore\Debug\BHCCHardwareStore.exe FIle:f\dd\vctools\vc7libs\ship\atlmfc\src\mfc\appcore.cpp Line: 196. For information on how your program can cause an assertion failure see the Visual C++ documentation on asserts.
(Press Retry to debug the application).
Now when I click ignore it kind of displays the app perfectly fine. And the app seems to work just fine. How can I fix this error This code is not the entire code I am cutting and pasting?
#include "stdafx.h"
#include <strstream>
#include <afxwin.h>
#include <string.h>
const int IDC_SB1 = 100;
const int IDC_CS1 = 101;
const int IDC_CS2 = 102;
const int IDC_BUTTON = 103;
const int MIN_RANGE = 0;
const int MAX_RANGE = 100;
class CApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};
CApp App;
class CSource :public CFrameWnd
{
CScrollBar*sb1;
CStatic* cs1;
CStatic* cs2;
CButton* button;
public:
CSource();
afx_msg void OnHScroll(UINT nSBCode,
UINT nPos, CScrollBar* pSccrollBar);
afx_msg void handleButton();
DECLARE_MESSAGE_MAP();
};
BEGIN_MESSAGE_MAP(CSource, CFrameWnd)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_BUTTON, handleButton)
END_MESSAGE_MAP()
//Window cONSTRUCTOR
CSource::CSource()
{
}
void CSource::OnHScroll(UINT nSBCode,
UINT nPos, CScrollBar* pScrollBar)
{
int pos, dividend = 0, holder = 0, x = 0;
char array[9];
//;
pos = pScrollBar->GetScrollPos();
switch (nSBCode)
{
case SB_LINEUP:
pos -= 1;
break;
case SB_LINEDOWN:
pos += 1;
break;
case SB_PAGEUP:
pos -= 10;
break;
case SB_PAGEDOWN:
pos += 10;
break;
case SB_TOP:
pos = MIN_RANGE;
break;
case SB_BOTTOM:
pos = MAX_RANGE;
break;
case SB_THUMBTRACK:
pos = nPos;
break;
default:
return;
}
if (pos < MIN_RANGE)
pos = MIN_RANGE;
else if (pos > MAX_RANGE)
pos = MAX_RANGE;
sb1->SetScrollPos(pos, TRUE);
//Set the labels to the new values
char s[100];
TCHAR s1[100];
std::ostrstream ostr(s, 100);
ostr << "Decimal Value = " << pos << std::ends;
for (int i = 0; i < 100; i++){
s1[i] = (TCHAR) s[i];
}
SetDlgItemText(IDC_CS1, s1);
ostr.seekp(std::ios::beg);
dividend = pos;
for (int y = 0; y < 9; y++)
array[y] = '0';
int remainder = dividend % 2;
int quotient = dividend / 2;
array[x] = (char)(remainder + 48);
do
{
remainder = quotient % 2;
quotient = quotient / 2;
array[++x] = (char)(remainder + 48);
} while (quotient != 0);
array[8] = '\0';
ostr << "Binary Value = " << _strrev(array) << std::ends;
SetDlgItemText(IDC_CS2, s1);
}
void CSource::handleButton()
{
int result;
result = MessageBox(_T("Are you sure?"), _T("Exiting"),
MB_ICONQUESTION | MB_YESNO);
if (result == IDYES)
{
Beep(1000, 100);
DestroyWindow();
}
else
Beep(200, 100);
}
//Initialize the application and the main window
BOOL CApp::InitInstance()
{
m_pMainWnd = new CSource();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
Your code (in VS2013) produces assertion in MFC code:
BOOL CWnd::ShowWindow(int nCmdShow)
{
ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
From your call:
m_pMainWnd->ShowWindow(m_nCmdShow);
You must create your window first, before you can show it.
I just started learning C++ this year. Currently, I'm using four accelerometers to calculate human body movement for my school project. I tried to use multi-thread but the data outputs are unstable as such when I tried multiple times, some of the data would change. It does give correct output data sometimes. The output is saved to Microsoft Excel while printing on the screen simultaneously. However, When I tried the accelerometer separately, it seems to work just fine. The output would change only when I multi-thread them together. I can't seem to find what cause the output data to change periodically. I appreciate any help I could get. Pardon my grammatical mistakes for it is not my native language. Thank you =D
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <process.h>
#include <Windows.h>
#include "erslib.h"
struct SENSOR_DATA {
int ID_1, ID_2, ID_3,ID_4;
int y_1, y_2, y_3, y_4;
int z_1, z_2, z_3, z_4;
int x_1, x_2, x_3, x_4;
};
void Thread_0(void);
void Thread_1(void);
void Thread_2(void);
void Thread_3(void);
FILE *fp[4];
errno_t err[4];
int key = 0;
int MainLoopFlag = true;
SENSOR_DATA acc;
int main()
{
DWORD ThreadID_0;
DWORD ThreadID_1;
DWORD ThreadID_2;
DWORD ThreadID_3;
HANDLE Handle_0 = NULL;
HANDLE Handle_1 = NULL;
HANDLE Handle_2 = NULL;
HANDLE Handle_3 = NULL;
Handle_0 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_0, 0, 0, &ThreadID_0);
Sleep(1000);
Handle_1 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_1, 0, 0, &ThreadID_1);
Sleep(1000);
Handle_2 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_2, 0, 0, &ThreadID_2);
Sleep(1000);
Handle_3 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_3, 0, 0, &ThreadID_3);
Sleep(1000);
WaitForSingleObject(Thread_0, INFINITE);
WaitForSingleObject(Thread_1, INFINITE);
WaitForSingleObject(Thread_2, INFINITE);
WaitForSingleObject(Thread_3, INFINITE);
//////Print and Data Output//////
while(MainLoopFlag){
if(_kbhit()){
key = _getch(); //a key to start data saving//
if(key == 'a' && Save_flag==0){
printf("Preparing for saving...\n");
Sleep(5000);
printf("Saving start\n");
char * fname_0 = ("C:\\Users\\Desktop\\adam\\ID 1\\test02.csv");
err[0] = fopen_s(&fp[0], fname_0,"w+");
if (fp[0]==NULL) {
printf("%s File cannot be opened。\n",fname_0);
return -1;
}
char * fname_1 = ("C:\\Users\\Desktop\\adam\\ID 2\\test02.csv");
err[1] = fopen_s(&fp[1], fname_1,"w+");
if (fp[1]==NULL) {
printf("%s File cannot be opened。\n",fname_1);
return -1;
}
char * fname_2 = ("C:\\Users\\Desktop\\adam\\ID 3\\test02.csv");
err[2] = fopen_s(&fp[2], fname_2,"w+");
if (fp[2]==NULL) {
printf("%s File cannot be opened。\n",fname_2);
return -1;
}
char * fname_3 = ("C:\\Users\\Desktop\\adam\\ID 4\\test02.csv");
err[3] = fopen_s(&fp[3], fname_3,"w+");
if (fp[3]==NULL) {
printf("%s File cannot be opened。\n",fname_3);
return -1;
}
Save_flag=1;
while(MainLoopFlag && Save_flag==1){
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_1,acc.x_1,acc.y_1,acc.z_1);
fprintf(fp[0],"%d, %d, %d, %d \n",acc.ID_1,acc.x_1,acc.y_1,acc.z_1);
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_2,acc.x_2,acc.y_2,acc.z_2);
fprintf(fp[1],"%d, %d, %d, %d \n",acc.ID_2,acc.x_2,acc.y_2,acc.z_2);
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_3,acc.x_3,acc.y_3,acc.z_3);
fprintf(fp[2],"%d, %d, %d, %d \n",acc.ID_3,acc.x_3,acc.y_3,acc.z_3);
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_4,acc.x_4,acc.y_4,acc.z_4);
fprintf(fp[3],"%d, %d, %d, %d \n",acc.ID_4,acc.x_4,acc.y_4,acc.z_4);
if(_kbhit()){
key = _getch();
//s key to stop data saving
if(key == 's' && Save_flag==1){
printf("End Data saving\n");
Save_flag=0;
fclose(fp[0]);
fclose(fp[1]);
fclose(fp[2]);
fclose(fp[3]);
MainLoopFlag = false;}
}
}
}
}
}
// Close Handle
CloseHandle(Handle_0);
CloseHandle(Handle_1);
CloseHandle(Handle_2);
CloseHandle(Handle_3);
return 0;
}
//--------------------------------------------------------------------------
// Name: Thread_1(void)
//--------------------------------------------------------------------------
void Thread_1(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 20;
int flag=0;
int temp_char_1[7];
ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);
//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
// 1となる位置を調べる
flag=0;
while(MainLoopFlag){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++){
if((buf[i] & 0x80) == 0x80){
flag=1;
break;
}
}
if(flag==1)break;
Sleep(1);
}
// 1 となる位置から後ろをbuf_dataに入れる
n_cou = n - i;
for(j=0;j<n_cou;j++)
buf_data[j]=buf[i+j];
while(MainLoopFlag){
while(MainLoopFlag && n_cou<5){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
n_cou=n_cou+n;
}
for(i=0;i<5;i++)bin_data[i]=buf_data[i];
temp_char_1[0] = (bin_data[0] & 0x38) >> 3; //ID
acc.ID_2 = (int)temp_char_1[0];
temp_char_1[1] = (bin_data[0] & 0x07) << 7; // x = 3bit
temp_char_1[2] = (bin_data[1] & 0x38) << 4; // y = 3bit
temp_char_1[3] = (bin_data[1] & 0x07) << 7; // z = 3bit
temp_char_1[4] = (bin_data[2] & 0x7F) | temp_char_1[1];
acc.x_2 = (int)temp_char_1[4];
temp_char_1[5] = (bin_data[3] & 0x7F) | temp_char_1[2];
acc.y_2 = (int)temp_char_1[5];
temp_char_1[6] = (bin_data[4] & 0x7F) | temp_char_1[3];
acc.z_2 = (int)temp_char_1[6];
for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];
n_cou=n_cou-5;
}
ERS_CloseAll();
}
//--------------------------------------------------------------------------
// Name: Thread_2(void)
//--------------------------------------------------------------------------
void Thread_2(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 15;
int flag=0;
int temp_char_2[7];
ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);
//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
// 1となる位置を調べる
flag=0;
while(MainLoopFlag){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++){
if((buf[i] & 0x80) == 0x80){
flag=1;
break;
}
}
if(flag==1)break;
Sleep(1);
}
// 1 となる位置から後ろをbuf_dataに入れる
n_cou = n - i;
for(j=0;j<n_cou;j++)
buf_data[j]=buf[i+j];
while(MainLoopFlag){
while(MainLoopFlag && n_cou<5){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
n_cou=n_cou+n;
}
for(i=0;i<5;i++)bin_data[i]=buf_data[i];
temp_char_2[0] = (bin_data[0] & 0x38) >> 3; //ID
acc.ID_3 = (int)temp_char_2[0];
temp_char_2[1] = (bin_data[0] & 0x07) << 7; // x = 3bit
temp_char_2[2] = (bin_data[1] & 0x38) << 4; // y = 3bit
temp_char_2[3] = (bin_data[1] & 0x07) << 7; // z = 3bit
temp_char_2[4] = (bin_data[2] & 0x7F) | temp_char_2[1];
acc.x_3 = (int)temp_char_2[4];
temp_char_2[5] = (bin_data[3] & 0x7F) | temp_char_2[2];
acc.y_3 = (int)temp_char_2[5];
temp_char_2[6] = (bin_data[4] & 0x7F) | temp_char_2[3];
acc.z_3 = (int)temp_char_2[6];
for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];
n_cou=n_cou-5;
}
ERS_CloseAll();
}
//--------------------------------------------------------------------------
// Name: Thread_3(void)
//--------------------------------------------------------------------------
void Thread_3(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 3;
int flag=0;
int temp_char_3[7];
ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);
//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
// 1となる位置を調べる
flag=0;
while(MainLoopFlag){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++){
if((buf[i] & 0x80) == 0x80){
flag=1;
break;
}
}
if(flag==1)break;
Sleep(1);
}
// 1 となる位置から後ろをbuf_dataに入れる
n_cou = n - i;
for(j=0;j<n_cou;j++)
buf_data[j]=buf[i+j];
//for(i=0;i<n_cou;i++)printf("%x ",buf_data[i]);
//printf("\n");
while(MainLoopFlag){
while(MainLoopFlag && n_cou<5){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
n_cou=n_cou+n;
}
for(i=0;i<5;i++)bin_data[i]=buf_data[i];
//for(i=0;i<5;i++)printf("%x ",bin_data[i]);
//printf("\n");
temp_char_3[0] = (bin_data[0] & 0x38) >> 3; //ID
acc.ID_4 = (int)temp_char_3[0];
temp_char_3[1] = (bin_data[0] & 0x07) << 7; // x = 3bit
temp_char_3[2] = (bin_data[1] & 0x38) << 4; // y = 3bit
temp_char_3[3] = (bin_data[1] & 0x07) << 7; // z = 3bit
temp_char_3[4] = (bin_data[2] & 0x7F) | temp_char_3[1];
acc.x_4 = (int)temp_char_3[4];
temp_char_3[5] = (bin_data[3] & 0x7F) | temp_char_3[2];
acc.y_4 = (int)temp_char_3[5];
temp_char_3[6] = (bin_data[4] & 0x7F) | temp_char_3[3];
acc.z_4 = (int)temp_char_3[6];
for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];
n_cou=n_cou-5;
}
ERS_CloseAll();
}
Correct data :
(Example)
ID->1, x->47, y->147, z->298
ID->2, x->298, y->25, z->147
ID->3, x->47, y->147, z->298
ID->4, x->213, y->123, z->43
ID->1, x->49, y->152, z->222
ID->2, x->256, y->30, z->155
ID->3, x->47, y->147, z->298
ID->4, x->221, y->132, z->54
incorrect data:
ID->1, x->905, y->179, z->20
ID->6, x->47, y->147, z->298
ID->0, x->0, y->0, z->0
ID->4, x->1010, y->56, z->23
ID->1, x->905, y->179, z->20
ID->6, x->47, y->147, z->298
ID->0, x->0, y->0, z->0
ID->4, x->1010, y->56, z->23
Basically the ID shouldn't change while the x, y and z would as you move the accelerometer.
I have question regarding the Windows APIs RedrawWindow , ExtTextOut and BitBlt .
I am hooking the ExtTextOut to extract the text location on screen, if I call RedrawWindow with parameter lprcUpdate = NULL and hrgnUpdate = NULL:
WindowWrapper.RedrawWindow(topWnd, IntPtr.Zero, IntPtr.Zero, RedrawWindowFlags.UpdateNow | RedrawWindowFlags.Frame | RedrawWindowFlags.Invalidate | RedrawWindowFlags.AllChildren | RedrawWindowFlags.Erase);
In hooked function ExtTextOut, I call LPToDP API to translate the location to device context location. Then it works fine. The text location is correct.
WindowWrapper.LPtoDP(hdc, arrPoint, 2);
But, if I call
WindowWrapper.RedrawWindow(topWnd, ref location, IntPtr.Zero, RedrawWindowFlags.UpdateNow | RedrawWindowFlags.Frame | RedrawWindowFlags.Invalidate | RedrawWindowFlags.AllChildren | RedrawWindowFlags.Erase);
Then it fails. The location is incorrect. I see that it translated by using LPToDP into incorrect location.
Any one know about this problem? How can we fix it?
public static bool ExtTextOutW_Hook(IntPtr hdc, int X, int Y, uint fuOptions,
IntPtr lprc, [MarshalAs(UnmanagedType.LPWStr)] string lpString,
uint cbCount, IntPtr lpDx)
{
try
{
lock (IsRetrievingSynch)
{
if (IsRetrieving && !string.IsNullOrEmpty(lpString) && cbCount > 0)
{
TextData textData = new TextData();
textData.FuncName = "ExtTextOutW";
char[] text = new char[cbCount];
bool isGlyphIndex = ((fuOptions & 0x0010) == 0x0010);
//ETO_GLYPH_INDEX
if (isGlyphIndex)
{
FontMap fontMap = GlyphCharMap.LoadGlyphCharMap(hdc);
if (fontMap != null && fontMap.GlyphCharMap != null)
{
for (int i = 0; i < cbCount; i++)
{
ushort glyph = lpString[i];
if (glyph < fontMap.GlyphCharMap.Length)
{
if (fontMap.GlyphCharMap[glyph] != 0)
{
text[i] = (char)(fontMap.GlyphCharMap[glyph]);
}
else
{
text[i] = (char)32;
}
}
else
{
text[i] = (char)glyph;
}
}
}
}
else
{
for (int i = 0; i < cbCount; i++)
{
text[i] = lpString[i];
}
}
textData.Text = new string(text);
textData.Text = textData.Text.Normalize(NormalizationForm.FormKC);
//textData.Text = textData.Text.Trim();
if (!string.IsNullOrEmpty(textData.Text))
{
int realCount = textData.Text.Length;
TEXTMETRICW tm = new TEXTMETRICW();
WindowWrapper.GetTextMetricsW(hdc, out tm);
int[] nWidth = new int[1];
WindowWrapper.GetCharWidth32W(hdc, ' ', ' ', nWidth);
textData.CharHeight = tm.tmHeight;
textData.CharWidth = nWidth[0];
textData.CharWidths = new int[realCount];
for (int i = 0; i < realCount; i++)
{
nWidth = new int[1];
WindowWrapper.GetCharWidth32W(hdc, textData.Text[i], textData.Text[i], nWidth);
textData.CharWidths[i] = nWidth[0];
}
uint textAlign = WindowWrapper.GetTextAlign(hdc);
POINT ptStartPos = new POINT();
if ((textAlign & WindowWrapper.TA_UPDATECP) > 0)
{
WindowWrapper.GetCurrentPositionEx(hdc, out ptStartPos);
}
else
{
ptStartPos.X = X;
ptStartPos.Y = Y;
}
switch (textAlign & (WindowWrapper.TA_BASELINE | WindowWrapper.TA_BOTTOM | WindowWrapper.TA_TOP))
{
case WindowWrapper.TA_BOTTOM:
textData.Location.Top = Y - tm.tmHeight;
textData.Location.Bottom = Y;
break;
case WindowWrapper.TA_BASELINE:
textData.Location.Top = Y - tm.tmAscent;
textData.Location.Bottom = Y + tm.tmDescent;
break;
case WindowWrapper.TA_TOP:
default:
textData.Location.Top = Y;
textData.Location.Bottom = Y + tm.tmHeight;
break;
}
int nLAlign = 0, nRAlign = 0;
switch (textAlign & (WindowWrapper.TA_LEFT | WindowWrapper.TA_RIGHT | WindowWrapper.TA_CENTER))
{
case WindowWrapper.TA_LEFT:
nLAlign = 0;
nRAlign = 2;
break;
case WindowWrapper.TA_CENTER:
nLAlign = -1;
nRAlign = 1;
break;
case WindowWrapper.TA_RIGHT:
nLAlign = -2;
nRAlign = 0;
break;
}
SIZE size = new SIZE();
if (WindowWrapper.GetTextExtentPoint32W(hdc, lpString, (int)cbCount, ref size))
{
textData.Location.Left = X + nLAlign * (size.CX / 2);
textData.Location.Right = X + nRAlign * (size.CX / 2);
}
else
{
textData.Location.Left = X - 20;
textData.Location.Right = X + 20;
}
LogFactory.RemoteLog.TraceDebug(string.Format("1.ExtTextOutW {0} {1} {2} {3} {4}",
textData.Text, textData.Location.Left, textData.Location.Top,
textData.Location.Right - textData.Location.Left,
textData.Location.Bottom - textData.Location.Top));
//Utility.ClipViewPort(hdc, ref textData.Location);
POINT[] arrPoint = new POINT[2];
arrPoint[0] = new POINT()
{
X = textData.Location.Left,
Y = textData.Location.Top
};
arrPoint[1] = new POINT()
{
X = textData.Location.Right,
Y = textData.Location.Bottom
};
POINT ptDCOrg = new POINT();
WindowWrapper.LPtoDP(hdc, arrPoint, 2);
textData.Location.Left = arrPoint[0].X;
textData.Location.Top = arrPoint[0].Y;
textData.Location.Right = arrPoint[1].X;
textData.Location.Bottom = arrPoint[1].Y;
WindowWrapper.GetDCOrgEx(hdc, out ptDCOrg);
textData.Location.Left += ptDCOrg.X;
textData.Location.Top += ptDCOrg.Y;
textData.Location.Right += ptDCOrg.X;
textData.Location.Bottom += ptDCOrg.Y;
LogFactory.RemoteLog.TraceDebug(string.Format("2.ExtTextOutW {0} {1} {2} {3} {4}",
textData.Text, textData.Location.Left, textData.Location.Top,
textData.Location.Right - textData.Location.Left,
textData.Location.Bottom - textData.Location.Top));
StringBuilder fontName = new StringBuilder();
fontName.Capacity = 260;
WindowWrapper.GetTextFace(hdc, 260, fontName);
textData.Font = fontName.ToString();
IntPtr hWnd = WindowWrapper.WindowFromDC(hdc);
if (WindowWrapper.IsWindowVisible(hWnd))
{
textData.Hdc = hdc;
textData.Hwnd = hWnd;
textData.ZOrder = WindowWrapper.GetZOrder(hWnd);
if (Utility.IsIntersect(_location, textData.Location) && WindowWrapper.IsChildWindow(_topWindow, hWnd))
{
LogFactory.RemoteLog.TraceVerbose(string.Format("3.ExtTextOutW {0} {1} {2} {3} {4} Z-Order: {5}",
textData.Text,
textData.Location.Left,
textData.Location.Top,
textData.Location.Right - textData.Location.Left,
textData.Location.Bottom - textData.Location.Top,
textData.ZOrder));
if (TextDataChannel != null)
TextDataChannel.SendTextData(textData);
}
}
else
{
textData.Hdc = hdc;
//if (lpDx != IntPtr.Zero)
// Utility.ClipViewPort(hdc, ref textData.Location);
_tempDataList.Add(textData);
}
}
}
}
}
catch (Exception ex)
{
LogFactory.RemoteLog.TraceError("ExtTextOutW " + ex.Message);
}
return ExtTextOutW(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx);
}
Thanks.