DXBind.cpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  1. /*
  2. 本源码由TC简单软件科技有限公司开源,功能可以自由修改、发布、
  3. 长沙简单软件科技有限公司对于源码不做后期维护,,请大家在使用过程中遵循开源协议
  4. */
  5. #include "StdAfx.h"
  6. #include "DXBind.h"
  7. #include "windows.h"
  8. #include "TSRuntime.h"
  9. #include "TSHookFuntion.h"
  10. #include "TsMessage.h"
  11. #include "D3dx9tex.h"
  12. #include "MapFile.h"
  13. #include "CreateGif/Quantizer.h"
  14. #include "CreateGif/GSKCreateGIF.h"
  15. #include "TSMyUser32DllFuntion.h"
  16. #include <imm.h>
  17. #include <cmath>
  18. #include "TSVIPHookFuntion.h"
  19. #pragma comment(lib,"imm32.lib")
  20. //DLL所在的位置
  21. extern TCHAR gDLLFolder[MAX_PATH + 1];
  22. extern myIsWindow my_IsWindow;
  23. extern myGetWindowThreadProcessId my_GetWindowThreadProcessId;
  24. //ZwWriteVirtualMemory
  25. DWORD ZwWriteVirtualMemoryRet = 0;
  26. DWORD ZwWriteVirtualMemoryEax = 0;
  27. __declspec(naked) NTSTATUS WINAPI My_ZwWriteVirtualMemory(IN HANDLE ProcessHandle,
  28. IN PVOID BaseAddress,
  29. IN PVOID Buffer,
  30. IN ULONG NumberOfBytesToWrite,
  31. OUT PULONG NumberOfBytesWritten OPTIONAL)
  32. {
  33. _asm
  34. {
  35. mov eax, ZwWriteVirtualMemoryEax;
  36. push ZwWriteVirtualMemoryRet;
  37. ret;
  38. }
  39. }
  40. DWORD MyGetWindowThreadProcessIdRet = 0;
  41. __declspec(naked) DWORD WINAPI MyGetWindowThreadProcessId(HWND hWnd, LPDWORD lpdwProcessId)
  42. {
  43. _asm
  44. {
  45. mov edi, edi
  46. push ebp
  47. mov ebp, esp
  48. jmp MyGetWindowThreadProcessIdRet;
  49. }
  50. }
  51. extern DWORD ZwWriteVirtualMemoryRet;
  52. extern DWORD ZwWriteVirtualMemoryEax;
  53. __declspec(naked) NTSTATUS WINAPI My_ZwProtectVirtualMemory(IN HANDLE ProcessHandle,
  54. IN PVOID* BaseAddress,
  55. IN SIZE_T* NumberOfBytesToProtect,
  56. IN ULONG NewAccessProtection,
  57. OUT PULONG OldAccessProtection)
  58. {
  59. _asm
  60. {
  61. //mov eax,0x4d ;//WIN7X64 偏移
  62. mov eax, Ntdll_ProtectVirtualEax;
  63. push Ntdll_ProtectVirtual;
  64. ret;
  65. }
  66. }
  67. extern DWORD ZwOpenProcessRet;
  68. extern DWORD ZwOpenProcessEax;
  69. __declspec(naked) NTSTATUS WINAPI MyZwOpenProcess(
  70. __out PHANDLE ProcessHandle,
  71. __in ACCESS_MASK DesiredAccess,
  72. __in POBJECT_ATTRIBUTES ObjectAttributes,
  73. __in_opt PCLIENT_ID ClientId
  74. )
  75. {
  76. _asm
  77. {
  78. mov eax, ZwOpenProcessEax;
  79. jmp ZwOpenProcessRet;
  80. }
  81. }
  82. HANDLE MyOpenProcess(int proid)
  83. {
  84. HANDLE ProcessHandle = (HANDLE)0;
  85. OBJECT_ATTRIBUTES ObjectAttribute = { sizeof(OBJECT_ATTRIBUTES), 0,NULL,NULL };
  86. ObjectAttribute.Attributes = 0;
  87. CLIENT_ID ClientIds;
  88. ClientIds.UniqueProcess = (HANDLE)proid;
  89. ClientIds.UniqueThread = (HANDLE)0;
  90. MyZwOpenProcess(&ProcessHandle, PROCESS_ALL_ACCESS, &ObjectAttribute, &ClientIds);
  91. ProcessHandle;
  92. return ProcessHandle;
  93. }
  94. typedef NTSTATUS(__stdcall* ZwUnmapViewOfSection)(IN HANDLE ProcessHandle, IN PVOID BaseAddress);
  95. ZwUnmapViewOfSection Zw_UnmapViewOfSection = NULL;
  96. bool DXBind::Inject201()//201,203绑定
  97. {
  98. //原理是在USER32InternalCallWinProc这个位置下钩子,执行定义好的汇编指令,加载我们的DLL
  99. bool success = false;
  100. DWORD InternalCallWinProc_Addr = 0;
  101. DWORD InternalCallWinProc_AddrRet = 0;
  102. InternalCallWinProc_Addr = (DWORD)::GetModuleHandle(L"user32.dll");
  103. if (winver == 1)//WinXP
  104. InternalCallWinProc_Addr += USER32InternalCallWinProcXPoffse;
  105. else if (winver == 2)//Win2003
  106. InternalCallWinProc_Addr += USER32InternalCallWinProcWin2003offse;
  107. else if (winver == 4 && TSRuntime::IsWin7X64)//WIN7X64
  108. InternalCallWinProc_Addr += USER32InternalCallProcWin7x64offse;
  109. else if (winver == 4)//WIN7X86
  110. InternalCallWinProc_Addr += USER32InternalCallProcWin7offse;
  111. else if (winver == 5 && TSRuntime::IsWin8X64)//WIN8X64
  112. InternalCallWinProc_Addr += USER32InternalCallProcWin8x64offse;
  113. else if (winver == 5)//WIN8X86
  114. InternalCallWinProc_Addr += USER32InternalCallProcWin8offse;
  115. PVOID pInternalCallProcAddr = (PVOID)InternalCallWinProc_Addr;
  116. InternalCallWinProc_AddrRet = InternalCallWinProc_Addr + 5;
  117. TSRuntime::EnablePrivilege(L"SeDebugPrivilege", true);
  118. HANDLE hprocess = NULL;
  119. DWORD dwwrite = 0;
  120. hprocess = MyOpenProcess(nPid);
  121. if (hprocess == NULL)
  122. return success;
  123. if (CallCodelen > 0)//先验证申请地址在当前进程是否存在
  124. {
  125. MEMORY_BASIC_INFORMATION mbi;
  126. VirtualQueryEx(hprocess, Alloaddr, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
  127. if (mbi.Protect != PAGE_EXECUTE_READWRITE)//说明已经不是原来的地址,需要重新申请地址
  128. {
  129. CallCodelen = 0;
  130. memset(CallCode, 0, 0x50);
  131. }
  132. }
  133. if (CallCodelen == 0)
  134. {
  135. //MessageBoxA(NULL,"MyOpenProcess-success","TS",NULL);
  136. LPVOID dlladdr = NULL;
  137. dlladdr = ::VirtualAllocEx(hprocess, NULL, wcslen(gDLLFolder) * sizeof(wchar_t) + 1, MEM_COMMIT, PAGE_EXECUTE_READWRITE);//申请地址存储DLL路径
  138. if (dlladdr)
  139. {
  140. //TSRuntime::add_log("VirtualAllocEx:dlladdr:%x,CallCode;%x",dlladdr,&CallCode[0]);
  141. //MessageBoxA(NULL,"VirtualAllocEx-dlladdr-success","TS",NULL);
  142. My_ZwWriteVirtualMemory(hprocess, dlladdr, gDLLFolder, wcslen(gDLLFolder) * sizeof(wchar_t), &dwwrite);
  143. Alloaddr = ::VirtualAllocEx(hprocess, NULL, 0x50, MEM_COMMIT, PAGE_EXECUTE_READWRITE);//申请地址存储,远程CALL的机器码
  144. //TSRuntime::add_log("VirtualAllocEx:Alloaddr:%x,dwwrite;%x",Alloaddr,dwwrite);
  145. if (dwwrite != 0 && Alloaddr)
  146. {
  147. //MessageBoxA(NULL,"VirtualAllocEx-Alloaddr-success","TS",NULL);
  148. dwwrite = 0;
  149. char Asmcalladdr[MAX_PATH] = { 0 };
  150. DWORD Loadlibaryaddr = 0;
  151. Loadlibaryaddr = (DWORD)TSRuntime::GetRemoteProcAddress(L"Kernel32.dll", "LoadLibraryW", nPid);
  152. if (Loadlibaryaddr != 0)
  153. {
  154. CMgAsmBase tsasm;//需要要汇编指令改为机器码
  155. CMgAsmBase::t_asmmodel am;
  156. char erro[MAX_PATH] = { 0 };
  157. char asmstr[MAX_PATH] = { 0 };
  158. //MessageBoxA(NULL,"Loadlibaryaddr-success","TS",NULL);
  159. sprintf(Asmcalladdr, "call %x", Loadlibaryaddr);
  160. int l = 0, len = 0;
  161. l = tsasm.Assemble("pushad", NULL, &am, 0, 0, erro);
  162. memcpy(&CallCode[len], am.code, l);
  163. len += l;
  164. sprintf(asmstr, "push 0x%x", (DWORD)dlladdr);
  165. l = tsasm.Assemble(asmstr, ((DWORD)Alloaddr + len), &am, 0, 0, erro);
  166. //TSRuntime::add_log("VirtualAllocEx:l:%d,CallCode;%x,am.length:%d,len:%d",l,&CallCode[0],am.length,len);
  167. memcpy(&CallCode[len], am.code, l);
  168. len += l;
  169. l = tsasm.Assemble(Asmcalladdr, ((DWORD)Alloaddr + len), &am, 0, 0, erro);
  170. memcpy(&CallCode[len], am.code, l);
  171. len += l;
  172. l = tsasm.Assemble("popad", NULL, &am, 0, 0, erro);
  173. memcpy(&CallCode[len], am.code, l);
  174. len += l;
  175. l = tsasm.Assemble("push EBP", NULL, &am, 0, 0, erro);
  176. memcpy(&CallCode[len], am.code, l);
  177. len += l;
  178. l = tsasm.Assemble("Mov EBP,ESP", NULL, &am, 0, 0, erro);
  179. memcpy(&CallCode[len], am.code, l);
  180. len += l;
  181. l = tsasm.Assemble("push ESI", NULL, &am, 0, 0, erro);
  182. memcpy(&CallCode[len], am.code, l);
  183. len += l;
  184. l = tsasm.Assemble("push EDI", NULL, &am, 0, 0, erro);
  185. memcpy(&CallCode[len], am.code, l);
  186. len += l;
  187. memset(Asmcalladdr, 0, MAX_PATH);
  188. sprintf(Asmcalladdr, "jmp %x", InternalCallWinProc_AddrRet);
  189. l = tsasm.Assemble(Asmcalladdr, ((DWORD)Alloaddr + len), &am, 0, 0, erro);
  190. memcpy(&CallCode[len], am.code, l);
  191. len += l;
  192. l = tsasm.Assemble("ret", NULL, &am, 0, 0, erro);
  193. memcpy(&CallCode[len], am.code, l);
  194. len += l;
  195. My_ZwWriteVirtualMemory(hprocess, Alloaddr, CallCode, len, &dwwrite);
  196. CallCodelen = dwwrite;
  197. }
  198. }
  199. //::VirtualFreeEx(hprocess,dlladdr,0,MEM_RELEASE);
  200. }
  201. }
  202. if (CallCodelen > 0)
  203. {
  204. //MessageBoxA(NULL,"CallCodelen>0","TS",NULL);
  205. //准备下钩子注入
  206. DWORD dwback;
  207. DWORD length = 5;
  208. BYTE jmp[5] = { 0 };
  209. jmp[0] = 0xe9;
  210. dwwrite = 0;
  211. *(DWORD*)(jmp + 1) = (DWORD)Alloaddr - InternalCallWinProc_Addr - 5;
  212. wchar_t temp[MAX_PATH] = { 0 };
  213. MEMORY_BASIC_INFORMATION mbi;
  214. VirtualQueryEx(hprocess, pInternalCallProcAddr, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
  215. if (mbi.Protect != PAGE_EXECUTE_READWRITE)
  216. My_ZwProtectVirtualMemory(hprocess, &pInternalCallProcAddr, &length, PAGE_EXECUTE_READWRITE, &dwback);
  217. //bool iswrite=::WriteProcessMemory(hprocess,Addr,jmp,5,&dwwrite);
  218. //if(!iswrite||dwwrite==0)
  219. //TSRuntime::add_log("Addr:%x,InternalCallWinProc_Addr:%x",pInternalCallProcAddr,InternalCallWinProc_Addr);
  220. My_ZwWriteVirtualMemory(hprocess, (void*)InternalCallWinProc_Addr, jmp, 5, &dwwrite);
  221. if (dwwrite > 0)
  222. {
  223. wchar_t pszEventName[MAX_PATH] = { 0 };
  224. ::wsprintf(pszEventName, L"%s%d", TS_BIND201_NAME, nPid);
  225. //char eer[50]={0};
  226. //sprintf(eer,"InternalCallWinProc_Addr:%x,dwwrite:%d",InternalCallWinProc_Addr,dwwrite);
  227. //MessageBoxA(NULL,"My_ZwWriteVirtualMemory-unsuccess",eer,NULL);
  228. HANDLE hEvent = NULL;
  229. for (int i = 0; i < 3000; i++)
  230. {
  231. hEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  232. if (hEvent)
  233. {
  234. //MessageBoxA(NULL,"My_ZwWriteVirtualMemory-success","TS",NULL);
  235. success = true;
  236. break;
  237. }
  238. Sleep(1);
  239. }
  240. //My_ZwWriteVirtualMemory(hprocess,(void*)InternalCallWinProc_Addr,ori,5,&dwwrite);
  241. SetEvent(hEvent);
  242. //My_ZwProtectVirtualMemory(hprocess,&pInternalCallProcAddr, (DWORD*)5, dwback, &dwback);
  243. }
  244. }
  245. CloseHandle(hprocess);
  246. return success;
  247. }
  248. DXBind::DXBind(void)
  249. {
  250. nPid = 0;
  251. KeyDownsleep = 50;//键盘单击默认延时
  252. MouseClicksleep = 40;//鼠标单击默认延时
  253. WindowsKeyMouseClicksleep = 10;// Windows模式键盘\鼠标单击延时
  254. NormalClicksleep = 30;//前台点击默认延时
  255. NormalKeysleep = 30;
  256. m_parenthwnd = 0;
  257. m_isBind = false;
  258. m_eventMsg = NULL;
  259. BindMode = -1;
  260. mSetHook = NULL;
  261. pData = NULL;
  262. m_hwnd = 0;
  263. IsNpProtect = false;
  264. CallCodelen = 0;
  265. Alloaddr = NULL;
  266. memset(ColorDataInfo, 0, sizeof(ColorDataInfo));
  267. memset(CallCode, 0, 0x50);
  268. memset(m_Retstr, 0, MAX_PATH * 100);
  269. memset(m_SetPath, 0, MAX_PATH);
  270. memset(Colors, 0, MAX_PATH);
  271. memset(m_colorOffR, 0, MAX_PATH);
  272. memset(m_colorOffG, 0, MAX_PATH);
  273. memset(m_colorOffB, 0, MAX_PATH);
  274. memset(MyDictCount, 0, 20 * sizeof(pMyDictInfo)); //存储当前所有字库,默认最大字库个数为20个
  275. memset(nMaxStrHight, 0, 20); //记录每个字库中,最高的字高.
  276. memset(nMyDictCountsize, 0, 20 * sizeof(DWORD)); //记录每个字库的数量
  277. NowUsingDictIndex = -1;//记录当前正在使用字库的下标
  278. isdxpic = false;
  279. char code[MAX_PATH] = { 0 };
  280. std::string pszMachineCode;
  281. USES_CONVERSION;
  282. wcscpy(TSRuntime::MachineCode, A2W(pszMachineCode.c_str()));
  283. Isgraphicprotect = false;
  284. if (Zw_UnmapViewOfSection == NULL)
  285. Zw_UnmapViewOfSection = (ZwUnmapViewOfSection)::GetProcAddress(::GetModuleHandle(L"ntdll.dll"), "ZwUnmapViewOfSection");
  286. pWndBmpBuffer = NULL;
  287. pWndBmpStrxy = NULL;
  288. pBuffer = NULL;
  289. IsTSEnableRealMouse = false;
  290. IsTSEnableRealKeypad = false;
  291. mousedelay = 0;
  292. mousestep = 0;
  293. LastMouseMoveX = 0;
  294. LastMouseMoveY = 0;
  295. GDI_dwExStyle = 0;
  296. m_keyMode |= KEY_NORMAL;//默认赋值为前台模式,可以在绑定前操作键鼠
  297. m_mouseMode |= MOUSE_NORMAL;//默认赋值为前台模式,可以在绑定前操作键鼠
  298. }
  299. DXBind::~DXBind(void)
  300. {
  301. }
  302. bool DXBind::UnBind()
  303. {
  304. //::EnableWindow(m_mousekeyhwnd,true);
  305. //DWORD nPid=0;
  306. ////GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  307. if (pWndBmpStrxy || pWndBmpBuffer)
  308. {
  309. for (int i = 0; i < 2000; i++)
  310. {
  311. delete[] pWndBmpStrxy[i];
  312. delete[] pWndBmpBuffer[i];
  313. pWndBmpBuffer[i] = NULL;
  314. pWndBmpStrxy[i] = NULL;
  315. }
  316. delete[] pWndBmpStrxy;
  317. delete[] pWndBmpBuffer;
  318. pWndBmpStrxy = NULL;
  319. pWndBmpBuffer = NULL;
  320. }
  321. if (pBuffer)
  322. {
  323. delete[] pBuffer;
  324. pBuffer = NULL;
  325. }
  326. m_hwnd = 0;
  327. m_parenthwnd = 0;
  328. m_isBind = false;
  329. IsTSEnableRealMouse = false;
  330. IsTSEnableRealKeypad = false;
  331. if (nPid)
  332. {
  333. char pszBinWindow_SetWindowHook[MAX_PATH] = { 0 };
  334. sprintf(pszBinWindow_SetWindowHook, "%s%d", TS_BINDWINDOW_SETWINDOWHOOK, nPid);
  335. HANDLE hbindprop = GetPropA(m_hwnd, pszBinWindow_SetWindowHook);
  336. char pszBinWindow_IME[MAX_PATH] = { 0 };
  337. sprintf(pszBinWindow_IME, "%s%d", TS_BINDWINDOW_IME, nPid);
  338. HANDLE hbindIMEprop = GetPropA(m_hwnd, pszBinWindow_IME);
  339. char pszBinWindow_IME101[MAX_PATH] = { 0 };
  340. sprintf(pszBinWindow_IME101, "%s%d", TS_BINDWINDOW_IME101, nPid);
  341. HANDLE hbindIMEprop101 = GetPropA(m_hwnd, pszBinWindow_IME101);
  342. char pszBinWindow_GDIWINDOW[MAX_PATH] = { 0 };
  343. sprintf(pszBinWindow_GDIWINDOW, "%s%d", TS_BINDWINDOW_GDIWINDOW, nPid);
  344. HANDLE hbindGdiWindow = GetPropA(m_hwnd, pszBinWindow_GDIWINDOW);
  345. if (hbindGdiWindow == NULL)
  346. hbindGdiWindow = GetPropA(m_parenthwnd, pszBinWindow_GDIWINDOW);
  347. if (hbindprop)
  348. {
  349. RemovePropA(m_hwnd, pszBinWindow_SetWindowHook); //删除绑定窗口标识
  350. }
  351. else if (hbindIMEprop)
  352. RemovePropA(m_hwnd, pszBinWindow_IME); //删除绑定窗口标识
  353. else if (hbindIMEprop101)
  354. RemovePropA(m_hwnd, pszBinWindow_IME101); //删除绑定窗口标识
  355. else if (hbindGdiWindow)
  356. RemovePropA(m_hwnd, pszBinWindow_GDIWINDOW); //删除绑定窗口标识
  357. else if (hbindGdiWindow == NULL)
  358. {
  359. //::GetWindowThreadProcessId(m_parenthwnd,&nPid); //获取注入窗口的进程ID
  360. char pszBinWindow_GDIWINDOW[MAX_PATH] = { 0 };
  361. sprintf(pszBinWindow_GDIWINDOW, "%s%d", TS_BINDWINDOW_GDIWINDOW, nPid);
  362. RemovePropA(m_parenthwnd, pszBinWindow_GDIWINDOW); //删除绑定窗口标识
  363. }
  364. }
  365. else
  366. return false;
  367. //GDI释放
  368. //if(TSRuntime::m_displayMode&DISPLAY_GDI)
  369. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2)
  370. {
  371. ////卸载GDI
  372. //::SetWindowLong(m_hwnd, GWL_EXSTYLE, TSRuntime::m_dwExStyle);
  373. //DWORD dwExStyle=::GetWindowLong((HWND)m_hwnd,GWL_EXSTYLE);
  374. //if(dwExStyle>=0x80100)
  375. // dwExStyle=dwExStyle-0x80100;
  376. //::SetWindowLong(m_hwnd, GWL_EXSTYLE, dwExStyle);
  377. if (GDI_dwExStyle == 0)
  378. GDI_dwExStyle = 0x100;
  379. ::SetWindowLong(m_hwnd, GWL_EXSTYLE, GDI_dwExStyle);
  380. if (m_parenthwnd != NULL)
  381. {
  382. ::SetWindowLong(m_parenthwnd, GWL_EXSTYLE, GDI_dwExStyle);
  383. //dwExStyle=::GetWindowLong((HWND)m_parenthwnd,GWL_EXSTYLE);
  384. //if(dwExStyle>=0x80100)
  385. // dwExStyle=dwExStyle-0x80100;
  386. //::SetWindowLong(m_parenthwnd, GWL_EXSTYLE, dwExStyle);
  387. }
  388. //::UpdateWindow(m_hwnd);
  389. ::RedrawWindow(m_hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN | RDW_FRAME);
  390. ////卸载GDI
  391. }
  392. if (BindMode == BIND_WINHOOK) //
  393. {
  394. //::MessageBox(0,L"SetHook解绑",L"TS",0);
  395. // 调用SetHook函数 ,卸载钩子
  396. HINSTANCE hlibrary = NULL;
  397. hlibrary = LoadLibrary(gDLLFolder);
  398. if (hlibrary)
  399. {
  400. // mSetHook=(MySetWindowHook)GetProcAddress(hlibrary,"CBFunB");
  401. if (mSetHook)
  402. {
  403. //::MessageBox(0,L"SetHook解绑",L"TS",0);
  404. // 调用SetHook函数
  405. DWORD dwProcessId = 0;
  406. DWORD dwThreadId = GetWindowThreadProcessId(m_hwnd, &dwProcessId);
  407. bool isunhook = false;
  408. if (dwThreadId != 0)
  409. {
  410. isunhook = mSetHook(FALSE, dwThreadId);
  411. }
  412. }
  413. FreeLibrary(hlibrary);
  414. }
  415. }
  416. if (BindMode == BIND_201)
  417. {
  418. wchar_t* name = wcsrchr(gDLLFolder, '\\');
  419. if (name)
  420. {
  421. name++;
  422. wchar_t dllname[MAX_PATH] = { 0 };
  423. wcscpy(dllname, name);
  424. HMODULE RemodeDLLMode = TSRuntime::GetRemoteModuleBase(dllname, nPid);
  425. if (RemodeDLLMode != NULL)
  426. {
  427. TSRuntime::EnablePrivilege(L"SeDebugPrivilege", true);
  428. HANDLE hprocess = NULL;
  429. hprocess = MyOpenProcess(nPid);
  430. Zw_UnmapViewOfSection(hprocess, RemodeDLLMode);
  431. CloseHandle(hprocess);
  432. }
  433. }
  434. }
  435. m_hwnd = 0;
  436. if (pData)
  437. {
  438. pData->ispichook = false;
  439. pData->reset();
  440. }
  441. return true;
  442. }
  443. bool DXBind::bind(HWND wnd, DWORD keyMod, DWORD mouseMod, DWORD disMod, BIND_MODE bindMode)
  444. {
  445. bool bret = false;
  446. //判断窗口是否有效
  447. if (my_IsWindow)
  448. {
  449. if (wnd == 0 || ::my_IsWindow(wnd) == false)
  450. {
  451. MessageBox(NULL, L"无效窗口句柄", L"TS", NULL);
  452. return false;
  453. }
  454. }
  455. else
  456. {
  457. if (wnd == 0 || ::IsWindow(wnd) == false)
  458. {
  459. MessageBox(NULL, L"无效窗口句柄", L"TS", NULL);
  460. return false;
  461. }
  462. }
  463. //记录被绑定的窗口的句柄
  464. m_hwnd = wnd;
  465. //得到共享内存的名称
  466. char pszMapName[MAX_PATH] = { 0 };
  467. if (my_GetWindowThreadProcessId)
  468. my_GetWindowThreadProcessId(wnd, &nPid); //获取注入窗口的进程ID
  469. else
  470. ::GetWindowThreadProcessId(wnd, &nPid); //获取注入窗口的进程ID
  471. if (nPid == 0)
  472. {
  473. if (MyGetWindowThreadProcessIdRet == 0)
  474. {
  475. DWORD addr = (DWORD)GetProcAddress(GetModuleHandle(L"user32.dll"), "GetWindowThreadProcessId");
  476. MyGetWindowThreadProcessIdRet = addr + 5;
  477. }
  478. MyGetWindowThreadProcessId(wnd, &nPid);
  479. if (nPid == 0)
  480. {
  481. MessageBox(NULL, L"无效窗口句柄", L"TS", NULL);
  482. return false;
  483. }
  484. }
  485. //MessageBox(NULL,L"MyGetWindowThreadProcessId!",L"TS",NULL);
  486. SendMsg(TS_UNBIND);
  487. sprintf(pszMapName, "%s%d", TS_MAPVIEW_NAME, nPid);
  488. char pszBinWindow_SetWindowHook[MAX_PATH] = { 0 };
  489. sprintf(pszBinWindow_SetWindowHook, "%s%d", TS_BINDWINDOW_SETWINDOWHOOK, nPid);
  490. HANDLE hbindprop = GetPropA(m_hwnd, pszBinWindow_SetWindowHook);
  491. char pszBinWindow_IME[MAX_PATH] = { 0 };
  492. sprintf(pszBinWindow_IME, "%s%d", TS_BINDWINDOW_IME, nPid);
  493. HANDLE hbindIMEprop = GetPropA(m_hwnd, pszBinWindow_IME);
  494. char pszBinWindow_IME101[MAX_PATH] = { 0 };
  495. sprintf(pszBinWindow_IME101, "%s%d", TS_BINDWINDOW_IME101, nPid);
  496. HANDLE hbindIMEprop101 = GetPropA(m_hwnd, pszBinWindow_IME101);
  497. if (hbindprop && m_isBind)
  498. return false;
  499. if (hbindIMEprop101 || hbindIMEprop) //如果绑定标识符还在说明已经窗口已经绑定
  500. {
  501. //MessageBox(NULL,L"卸载DLL!",L"TS",NULL);
  502. SendMessage(wnd, TS_UNBIND, 0, 0);
  503. Sleep(100);
  504. if (hbindIMEprop)
  505. RemovePropA(wnd, pszBinWindow_IME);
  506. else if (hbindIMEprop101)
  507. RemovePropA(wnd, pszBinWindow_IME101);
  508. }
  509. //MessageBox(NULL,L"申请指定长度的共享内存空间!-start",L"TS",NULL);
  510. //申请指定长度的共享内存空间
  511. CShareMemory* sm = new CShareMemory(pszMapName, sizeof(CMessageData), TRUE);
  512. //取得指向共享内存的指针
  513. pData = (CMessageData*)sm->GetBuffer();
  514. if (pData != NULL)
  515. {
  516. //MessageBox(NULL,L"reset!-start",L"TS",NULL);
  517. pData->reset();
  518. //MessageBox(NULL,L"reset!-end",L"TS",NULL);
  519. //TSRuntime::add_log( "TS_GETSCREENDATA:%x",pData->ColorDataInfo);
  520. }
  521. else
  522. return false;
  523. pData->InjectHwnd = TSRuntime::GetProcessMainWnd(GetCurrentProcessId()); //自身窗口句柄
  524. pData->isBind = true;
  525. pData->m_hwnd = wnd;
  526. if (m_mouseMode & MOUSE_API)
  527. pData->ismousehook = true;
  528. if (m_keyMode & KEY_API)
  529. pData->iskeyboardhook = true;
  530. if (m_displayMode & DISPLAY_DX_3D)
  531. pData->ispichook = true;
  532. if (TSRuntime::IsNpProtect)
  533. {
  534. pData->IsNpProtect = true;
  535. IsNpProtect = true;
  536. }
  537. if (m_displayMode & DISPLAY_DX_3D)//如果是DX图色绑定
  538. pData->isgraphicprotect = Isgraphicprotect;
  539. if (wcslen(TSRuntime::SetPicPwdString) > 0)
  540. wcscpy(pData->SetPicPwdString, TSRuntime::SetPicPwdString);
  541. pData->Isdxkmprotect = Isdxkmprotect;
  542. pData->Isdxantiapi = Isdxantiapi;
  543. TSRuntime::hookHwnd = wnd;
  544. winver = TSRuntime::InitialWindowsVersion();
  545. if (TSRuntime::IsStartProtect)
  546. {
  547. //BYTE winver=TSRuntime::InitialWindowsVersion();
  548. //if(winver!=4)//不是Win7
  549. {
  550. //MessageBox(NULL,L"HookandUnhookAPI-start",L"HookandUnhookAPI",NULL);
  551. TSRuntime::IsVirtualProtectEx = true; //如果开了盾!使用驱动模式远程修改游戏页保护属性
  552. HookandUnhookAPI(0);//如果开了盾!使用驱动模式远程修改游戏页保护属性
  553. }
  554. }
  555. //SetWindowHook注入
  556. if (bindMode == BIND_WINHOOK)
  557. {
  558. if (!SetWindowHookExInject(wnd))
  559. {
  560. //MessageBoxA(NULL,"SetWindowHook注入失败","TS",NULL);
  561. return false;
  562. }
  563. SetPropA(wnd, pszBinWindow_SetWindowHook, (HANDLE)1); //设置绑定标识符
  564. }
  565. //输入法注入
  566. else if (bindMode == BIND_IME || bindMode == BIND_IME101)
  567. {
  568. if (bindMode == BIND_IME)
  569. {
  570. pData->InjectType = BIND_IME;
  571. SetPropA(wnd, pszBinWindow_IME, (HANDLE)1); //设置绑定标识符
  572. }
  573. else if (bindMode == BIND_IME101)
  574. {
  575. pData->InjectType = BIND_IME101;
  576. SetPropA(wnd, pszBinWindow_IME101, (HANDLE)1); //设置绑定标识符
  577. }
  578. //MessageBox(NULL,L"输入法注入!-start",L"TS",NULL);
  579. char pszEventName[MAX_PATH] = { 0 };
  580. ::sprintf(pszEventName, "%s%d", TS_EVENT_IMEHOOK, nPid);
  581. HANDLE hEvent = OpenEventA(EVENT_ALL_ACCESS, FALSE, pszEventName);
  582. if (hEvent == NULL)
  583. {
  584. if (!IMEInject(wnd))
  585. {
  586. if (hbindIMEprop)
  587. RemovePropA(m_hwnd, pszBinWindow_IME); //删除绑定窗口标识
  588. else if (hbindIMEprop101)
  589. RemovePropA(m_hwnd, pszBinWindow_IME101); //删除绑定窗口标识
  590. return false;
  591. }
  592. }
  593. //else
  594. // TSRuntime::add_log( "SetEvent");
  595. }
  596. else if (bindMode == BIND_201 || bindMode == BIND_203)
  597. {
  598. pData->InjectType = bindMode;
  599. bool bret = Inject201();
  600. if (!bret)
  601. return false;
  602. }
  603. else
  604. {
  605. return false;//未知注入模式
  606. }
  607. //MessageBox(NULL,L"输入法注入!-end",L"TS",NULL);
  608. for (int i = 0; i < 300; i++)
  609. {
  610. if (pData->isInject) ////等待已经绑定窗口标识 TS_BINDWINDOW_NAME
  611. break;
  612. ::Sleep(20);
  613. }
  614. //这里判断DLL是否已经注入,如果成功再做下面的绑定操作
  615. if (!pData->isInject)
  616. {
  617. //MessageBox(NULL,L"绑定失败!",L"TS",NULL);
  618. return false;
  619. }
  620. if (wcslen(m_SetPath) > 1)
  621. {
  622. memset(pData->setpath, 0, MAX_PATH);
  623. wcscpy(pData->setpath, m_SetPath);
  624. SendMsg(TS_SETPATH);
  625. }
  626. if (NowUsingDictIndex != -1)
  627. {
  628. //如果已经注入成功,者需拷贝一份字库信息到共享内存
  629. pData->NowUsingDictIndex = NowUsingDictIndex;
  630. pData->MyDictCount[NowUsingDictIndex] = MyDictCount[NowUsingDictIndex];
  631. pData->nMyDictCountsize[NowUsingDictIndex] = nMyDictCountsize[NowUsingDictIndex];
  632. pData->nMaxStrHight[NowUsingDictIndex] = nMaxStrHight[NowUsingDictIndex];
  633. //return SendMsg(TS_SETDICT,(WPARAM)GetCurrentProcessId()); //发送消息通知切换字库
  634. }
  635. //TSRuntime::add_log( "pData->isgraphicprotect:%d,Isgraphicprotect;%d",pData->isgraphicprotect,Isgraphicprotect);
  636. //Sleep(50);
  637. SendMsg(TS_BIND, (WPARAM)wnd, (LPARAM)IsNpProtect);
  638. m_isBind = true;
  639. return true;
  640. }
  641. bool DXBind::SetWindowHookExInject(HWND wnd)
  642. {
  643. bool bInstall = true;
  644. BOOL bRet = false;
  645. HINSTANCE hlibrary = NULL;
  646. hlibrary = LoadLibrary(gDLLFolder);
  647. if (hlibrary)
  648. {
  649. mSetHook = (MySetWindowHook)GetProcAddress(hlibrary, "CBFunB");
  650. if (mSetHook)
  651. {
  652. // 调用SetHook函数
  653. DWORD dwProcessId = 0;
  654. DWORD dwThreadId = GetWindowThreadProcessId(wnd, &dwProcessId);
  655. if (dwThreadId != 0)
  656. bRet = mSetHook(bInstall, dwThreadId);
  657. SendMessage(wnd, WM_PAINT, 0, 0); //WH_CALLWNDPROC消息需要,有SendMessage消息后才可以绑定成功,所以模拟SendMessage发送一个消息
  658. }
  659. FreeLibrary(hlibrary);
  660. }
  661. return bRet;
  662. }
  663. VOID GetInstallIMEValue(wchar_t* keyvaule)
  664. {
  665. TCHAR ValueInfo[MAX_PATH] = { 0 };
  666. TCHAR achValue[MAX_PATH] = { 0 };
  667. DWORD ValueSize = MAX_PATH;
  668. DWORD cType;
  669. HKEY hKey;
  670. //打开注册表项
  671. if (RegOpenKey(HKEY_CURRENT_USER, L"Keyboard Layout\\Preload", &hKey) == ERROR_SUCCESS)
  672. {
  673. for (int i = 1; i < 100; i++) //用户安装的输入法最大数假设为100
  674. {
  675. swprintf(achValue, L"%d", i);
  676. if (RegQueryValueEx(hKey, achValue, NULL, &cType, (LPBYTE)ValueInfo, &ValueSize) == ERROR_SUCCESS)
  677. {
  678. wcscpy(keyvaule, ValueInfo);
  679. }
  680. else
  681. break;
  682. }
  683. }
  684. }
  685. bool DXBind::IMEInject(HWND wnd)
  686. {
  687. //注入DLL,方便DLL注入启动线程
  688. {
  689. wchar_t tempPath[MAX_PATH] = { 0 };
  690. wchar_t tempIME[MAX_PATH] = { 0 };
  691. wchar_t IMEpath[MAX_PATH] = { 0 };
  692. GetTempPath(MAX_PATH, tempPath);
  693. ::GetSystemDirectory(tempPath, MAX_PATH);
  694. wchar_t IMEWOW64Path[MAX_PATH] = { 0 };
  695. wchar_t sys32Path[MAX_PATH] = { 0 };
  696. if (TSRuntime::IsWin8X64)//WIN8X64下安装路径有变,必须安装在SysWOW64目录下
  697. {
  698. wcscpy(IMEWOW64Path, tempPath);
  699. wcscpy(sys32Path, tempPath);
  700. wchar_t* syspath = wcsrchr(IMEWOW64Path, '\\');
  701. int syslen = wcslen(syspath);
  702. memset(syspath, 0, syslen);
  703. swprintf(IMEWOW64Path, L"%s\\SysWOW64\\TSimedll.ime", IMEWOW64Path);
  704. //MessageBox(NULL,tempPath,syspath,NULL);
  705. }
  706. ::wsprintf(tempIME, L"%s\\TSimedllcode.ime", tempPath);//MessageBoxA(NULL,"TS_BINDWINDOW_NAME","IME",NULL);
  707. ::wsprintf(IMEpath, L"%s\\TSimedll.ime", tempPath);//MessageBoxA(NULL,"TS_BINDWINDOW_NAME","IME",NULL);
  708. HINSTANCE globalModuleHandle = ::GetModuleHandle(gDLLFolder);
  709. HRSRC hRsc = FindResource(globalModuleHandle, MAKEINTRESOURCE(IDR_IME1), L"IME");
  710. HGLOBAL hglobal = LoadResource(globalModuleHandle, hRsc);
  711. VOID* lock = LockResource(hglobal);
  712. DWORD size = SizeofResource(globalModuleHandle, hRsc);
  713. char* pmen = (char*)malloc(size);
  714. WriteProcessMemory(GetCurrentProcess(), (void*)pmen, lock, size, NULL);
  715. HANDLE Filepath;
  716. Filepath = CreateFile(tempIME, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  717. DWORD nwrite;
  718. //TSRuntime::add_log("Filepath:%d,tempIME:%s,GetLastError:%d",Filepath,tempIME,::GetLastError());
  719. WriteFile(Filepath, (void*)pmen, size, &nwrite, NULL);
  720. CloseHandle(Filepath);
  721. int rbl = 1;
  722. if (rbl)//解密成功
  723. {
  724. if (TSRuntime::IsWin8X64)
  725. ::CopyFile(IMEpath, IMEWOW64Path, false);
  726. //MessageBox(NULL,IMEpath,L"TS",NULL);
  727. DeleteFile(tempIME);
  728. ////////注入前动态修改输入法DLL的导入表
  729. //USES_CONVERSION;
  730. //wchar_t sysdll[MAX_PATH]={0};
  731. //::wsprintf(sysdll,L"%s\\TSPlug.dll",tempPath);
  732. //CopyFile(gDLLFolder,sysdll,FALSE);
  733. //HookIMEdllIAT(W2A(IMEpath),W2A(sysdll));
  734. ////////注入前动态修改输入法DLL的导入表
  735. //输入法注入
  736. SystemParametersInfo(SPI_GETDEFAULTINPUTLANG, 0, &SysIME, 0); //得到系统默认输入法设置
  737. MSetUpIME = ImmInstallIME(IMEpath, L"天使输入法"); //TSRuntime::add_log("ImmInstallIME错误信息:%d",::GetLastError());
  738. //if(TSRuntime::IsWin8X64==false)
  739. bool setdef = SystemParametersInfo(SPI_SETDEFAULTINPUTLANG, 0, &MSetUpIME, SPIF_SENDWININICHANGE); //将指定输入法设置为系统默认输入法,应用程序一打开就加载
  740. if (MSetUpIME > 0)
  741. {
  742. //DLL注入
  743. int ret = ::SendMessage(wnd, WM_INPUTLANGCHANGEREQUEST, (WPARAM)TRUE, (LPARAM)MSetUpIME); //后台激活输入法,注入Dll
  744. //if(!ret)
  745. // MessageBox(NULL,L"IME失败",L"TS",NULL);
  746. //::PostMessage(wnd,WM_INPUTLANGCHANGEREQUEST,(WPARAM )TRUE,(LPARAM )MSetUpIME);
  747. ////做清理工作
  748. DWORD IMEkey = (DWORD)MSetUpIME;//记录当前输入法的键值,方便后面在注册表中删除
  749. TCHAR keyvaule[10] = { 0 };
  750. swprintf(keyvaule, L"%x", IMEkey);//格式化为字符串,因为值大于了整形值的最大范围
  751. //卸载输入法
  752. SystemParametersInfo(SPI_SETDEFAULTINPUTLANG, 0, &SysIME, 0); //还原系统默认输入法设置
  753. UnloadKeyboardLayout(MSetUpIME);
  754. //注册表中删除输入法
  755. TCHAR achValue[MAX_PATH] = { 0 };
  756. TCHAR ValueInfo[MAX_PATH] = { 0 };
  757. DWORD ValueSize = MAX_PATH;
  758. DWORD cType;
  759. HKEY hKey;
  760. //打开注册表项
  761. if (RegOpenKey(HKEY_CURRENT_USER, L"Keyboard Layout\\Preload", &hKey) == ERROR_SUCCESS)
  762. {
  763. for (int i = 1; i < 100; i++) //用户安装的输入法最大数假设为100
  764. {
  765. swprintf(achValue, L"%d", i);
  766. if (RegQueryValueEx(hKey, achValue, NULL, &cType, (LPBYTE)ValueInfo, &ValueSize) == ERROR_SUCCESS)
  767. {
  768. if (wcsicmp(keyvaule, ValueInfo) == 0)//如果匹配到是我们安装的输入法,删除掉
  769. {
  770. RegDeleteValue(hKey, achValue);
  771. break;
  772. }
  773. }
  774. else
  775. break;
  776. }
  777. }
  778. }
  779. else
  780. {
  781. //wchar_t err[MAX_PATH]={0};
  782. //swprintf(err,L"输入法安装失败:GetLastError:%d",GetLastError());
  783. //::MessageBox(0,err,L"TS",0);
  784. return false;
  785. }
  786. DeleteFile(IMEpath);
  787. }
  788. }
  789. return true;
  790. }
  791. bool DXBind::DownCpu(DWORD sleeptime)
  792. {
  793. bool bret = false;
  794. if (!pData->ispichook)
  795. return 0;//如果PIC钩子HOOK失败,直接返回失败
  796. if (sleeptime > 100)
  797. sleeptime = 100;
  798. else if (sleeptime < 0)
  799. sleeptime = 0;
  800. bret = SendMsg(TS_DOWNCPU, sleeptime);
  801. return bret;
  802. }
  803. bool DXBind::CmpColor(LONG x, LONG y, BSTR sColor, DOUBLE sim)
  804. {
  805. bool bret = 1; //颜色匹配返回0,颜色不匹配1
  806. if (wcslen(sColor) > MAX_PATH)//传进要比较的颜色值字符串太长返回失败
  807. return 1;
  808. DWORD nret = GetColor(x, y, TSRuntime::RetStr);
  809. if (nret == 0) //如果取色失败,返回失败
  810. return 1;
  811. //处理多个色偏
  812. int col = 0, ncolors;
  813. wchar_t colorBuffer[MAX_PATH][16];//存储多个字符串
  814. for (int i = 0; i < MAX_PATH; i++)
  815. {
  816. memset(colorBuffer[i], 0, 16);
  817. }
  818. short nstr = 0;//要查找色偏的个数 ,最多支持(MAX_PATH)个色偏
  819. while (*sColor)
  820. {
  821. if (*sColor != L'|')
  822. {
  823. if (col > 13)
  824. return 1;
  825. colorBuffer[nstr][col++] = *sColor;
  826. sColor++;
  827. continue;
  828. }
  829. else
  830. {
  831. if (col < 1)
  832. {
  833. sColor++;
  834. continue;
  835. }
  836. nstr++;
  837. col = 0;
  838. sColor++;
  839. }
  840. }
  841. if (col >= 1)
  842. nstr++;
  843. ncolors = nstr;//记录下色偏个数
  844. //int t=::GetTickCount();
  845. for (int i = 0; i < nstr; i++)
  846. {
  847. WCHAR colorR[4] = { 0 };//字的颜色
  848. WCHAR colorG[4] = { 0 };//字的颜色
  849. WCHAR colorB[4] = { 0 };//字的颜色
  850. WCHAR coloroffR[4] = { 0 };//偏色
  851. WCHAR coloroffG[4] = { 0 };//偏色
  852. WCHAR coloroffB[4] = { 0 };//偏色
  853. int count = 0;
  854. int colorL = 0;
  855. while (colorBuffer[i][colorL] != L'-')
  856. {
  857. if (count > 6) //判断是否是非法颜色值
  858. break;
  859. if (count < 2)
  860. colorR[count] = colorBuffer[i][colorL++];
  861. else if (count > 1 && count < 4)
  862. colorG[count - 2] = colorBuffer[i][colorL++];
  863. else if (count > 3)
  864. colorB[count - 4] = colorBuffer[i][colorL++];
  865. count++;
  866. }
  867. //将传入的RGB转换为BGR
  868. WCHAR colorBGR[16] = { 0 };//字的颜色
  869. swprintf(colorBGR, L"%s%s%s", colorR, colorG, colorB);
  870. count = 0;
  871. colorL++;
  872. while (colorBuffer[colorL] != L'\0')
  873. {
  874. if (count >= 6) //判断是否是非法色偏值
  875. break;
  876. if (count < 2)
  877. coloroffR[count] = colorBuffer[i][colorL++];
  878. else if (count > 1 && count < 4)
  879. coloroffG[count - 2] = colorBuffer[i][colorL++];
  880. else if (count > 3)
  881. coloroffB[count - 4] = colorBuffer[i][colorL++];
  882. count++;
  883. }
  884. //将传入的RGB转换为BGR
  885. WCHAR coloroffBGR[16] = { 0 };//偏色
  886. swprintf(coloroffBGR, L"%s%s%s", coloroffB, coloroffG, coloroffR);
  887. Colors[i] = wcstol(colorBGR, NULL, 16);//整形颜色值
  888. DWORD strColoroff = wcstol(coloroffBGR, NULL, 16);//整形偏色值
  889. m_colorOffR[i] = GetRValue(strColoroff);
  890. m_colorOffG[i] = GetGValue(strColoroff);
  891. m_colorOffB[i] = GetBValue(strColoroff);
  892. }
  893. //处理多个色偏
  894. BYTE simColor = 0;
  895. if (sim == 0.5)
  896. simColor = 49;
  897. else if (sim == 0.6)
  898. simColor = 38;
  899. else if (sim == 0.7)
  900. simColor = 29;
  901. else if (sim == 0.8)
  902. simColor = 18;
  903. else if (sim == 0.9)
  904. simColor = 9;
  905. else if (sim == 0.4)
  906. simColor = 58;
  907. else if (sim == 0.3)
  908. simColor = 67;
  909. else if (sim == 0.2)
  910. simColor = 76;
  911. else if (sim == 0.1)
  912. simColor = 85;
  913. /////////////////////////////////////
  914. DWORD loadColor = wcstol(TSRuntime::RetStr, NULL, 16);//整形颜色值
  915. BYTE loadR = GetRValue(loadColor);
  916. BYTE loadG = GetGValue(loadColor);
  917. BYTE loadB = GetBValue(loadColor);
  918. for (int i = 0; i < ncolors; i++)
  919. {
  920. BYTE cmpR = GetRValue(Colors[i]);
  921. BYTE cmpG = GetGValue(Colors[i]);
  922. BYTE cmpB = GetBValue(Colors[i]);
  923. if (abs(loadR - cmpR) <= (m_colorOffR[i] + simColor) && abs(loadG - cmpG) <= (m_colorOffG[i] + simColor) && abs(loadB - cmpB) <= (m_colorOffB[i] + simColor))
  924. {
  925. return 0;//匹配成功返回0
  926. }
  927. }
  928. return bret;
  929. }
  930. int DXBind::GetColor(LONG x, LONG y, BSTR sColor)
  931. {
  932. int ret = 0;
  933. RECT clientrc;
  934. ::GetClientRect(m_hwnd, &clientrc);
  935. if (x >= clientrc.right || y >= clientrc.bottom)
  936. {
  937. return 0;
  938. }
  939. MyFindPictureClass fpic;
  940. fpic.m_displayMode = m_displayMode;
  941. fpic.m_parenthwnd = m_parenthwnd;
  942. fpic.pWndBmpBuffer = pWndBmpBuffer;
  943. fpic.pBuffer = pBuffer;
  944. fpic.m_hwnd = m_hwnd;
  945. int color = 0;
  946. // if(TSRuntime::m_displayMode&DISPLAY_GDI)
  947. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  948. {
  949. long x1, y1;
  950. ret = fpic.processColor(0, m_hwnd,
  951. color,
  952. x,
  953. y,
  954. 0,
  955. 0,
  956. 0,
  957. 0,
  958. x1, y1, NULL);
  959. int r = 0, g = 0, b = 0;
  960. r = GetRValue(color);
  961. g = GetGValue(color);
  962. b = GetBValue(color);
  963. memset(TSRuntime::RetStr, 0, MAX_PATH);
  964. if (r >= 0x10 && g >= 0x10 && b >= 0x10)
  965. swprintf(TSRuntime::RetStr, L"%x%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  966. else if (r < 0x10 && g < 0x10 && b < 0x10)
  967. swprintf(TSRuntime::RetStr, L"0%x0%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  968. else if (r < 0x10 && g < 0x10)
  969. swprintf(TSRuntime::RetStr, L"0%x0%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  970. else if (r < 0x10 && b < 0x10)
  971. swprintf(TSRuntime::RetStr, L"0%x%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  972. else if (g < 0x10 && b < 0x10)
  973. swprintf(TSRuntime::RetStr, L"%x0%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  974. else if (r < 0x10)
  975. swprintf(TSRuntime::RetStr, L"0%x%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  976. else if (g < 0x10)
  977. swprintf(TSRuntime::RetStr, L"%x0%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  978. else if (b < 0x10)
  979. swprintf(TSRuntime::RetStr, L"%x%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  980. }
  981. else if (m_displayMode & DISPLAY_DX_3D)
  982. {
  983. if (!pData->ispichook)
  984. return 0;//如果PIC钩子HOOK失败,直接返回失败
  985. fpic.m_parenthwnd = 0;
  986. ST_TSFINDPIC getdata;
  987. getdata.left = x;
  988. getdata.top = y;
  989. getdata.right = 0;
  990. getdata.bottom = 0;
  991. getdata.ret = -1;
  992. pData->type = TS_GETSCREENDATA;
  993. ::memcpy(pData->pmsg, &getdata, sizeof(ST_TSFINDPIC));
  994. wchar_t pszEventName[MAX_PATH] = { 0 };
  995. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  996. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  997. if (picEvent)
  998. {
  999. ::WaitForSingleObject(picEvent, 10000);
  1000. ::CloseHandle(picEvent);
  1001. }
  1002. else
  1003. {
  1004. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  1005. ::WaitForSingleObject(picEvent, 3000);
  1006. ::CloseHandle(picEvent);
  1007. }
  1008. DWORD(*Data)[2000] = (DWORD(*)[2000])pData->ColorDataInfo;
  1009. long x1, y1;
  1010. ret = fpic.processColor(0, m_hwnd,
  1011. color,
  1012. x,
  1013. y,
  1014. 0,
  1015. 0,
  1016. 0,
  1017. 0,
  1018. x1, y1, Data, NULL, NULL);
  1019. memset(TSRuntime::RetStr, 0, MAX_PATH);
  1020. int r = 0, g = 0, b = 0;
  1021. r = GetRValue(color);
  1022. g = GetGValue(color);
  1023. b = GetBValue(color);
  1024. if (r >= 0x10 && g >= 0x10 && b >= 0x10)
  1025. swprintf(TSRuntime::RetStr, L"%x%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1026. else if (r < 0x10 && g < 0x10 && b < 0x10)
  1027. swprintf(TSRuntime::RetStr, L"0%x0%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1028. else if (r < 0x10 && g < 0x10)
  1029. swprintf(TSRuntime::RetStr, L"0%x0%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1030. else if (r < 0x10 && b < 0x10)
  1031. swprintf(TSRuntime::RetStr, L"0%x%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1032. else if (g < 0x10 && b < 0x10)
  1033. swprintf(TSRuntime::RetStr, L"%x0%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1034. else if (r < 0x10)
  1035. swprintf(TSRuntime::RetStr, L"0%x%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1036. else if (g < 0x10)
  1037. swprintf(TSRuntime::RetStr, L"%x0%x%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1038. else if (b < 0x10)
  1039. swprintf(TSRuntime::RetStr, L"%x%x0%x", r, g, b);//将传入的BGR颜色值转换为RGB进行输出
  1040. }
  1041. return ret;
  1042. }
  1043. int DXBind::Ocr(LONG x1, LONG y1, LONG x2, LONG y2, wchar_t* color, DOUBLE sim, int type)
  1044. {
  1045. int ret = 0;
  1046. if (wcslen(color) > MAX_PATH)
  1047. return ret;
  1048. wchar_t coloroffer[MAX_PATH] = { 0 };
  1049. swprintf(coloroffer, L"%s", color);
  1050. RECT clientrc;
  1051. ::GetClientRect(m_hwnd, &clientrc);
  1052. if (x1 >= clientrc.right || y1 >= clientrc.bottom)
  1053. {
  1054. return 0;
  1055. }
  1056. MyFindPictureClass fpic;
  1057. fpic.m_displayMode = m_displayMode;
  1058. fpic.m_parenthwnd = m_parenthwnd;
  1059. fpic.pWndBmpBuffer = pWndBmpBuffer;
  1060. fpic.pWndBmpStrxy = pWndBmpStrxy;
  1061. fpic.pBuffer = pBuffer;
  1062. //if(TSRuntime::m_displayMode&DISPLAY_GDI)
  1063. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  1064. {
  1065. TSRuntime::NowUsingDictIndex = NowUsingDictIndex;
  1066. TSRuntime::nMyDictCountsize[TSRuntime::NowUsingDictIndex] = nMyDictCountsize[NowUsingDictIndex];
  1067. TSRuntime::nMaxStrHight[TSRuntime::NowUsingDictIndex] = nMaxStrHight[NowUsingDictIndex];
  1068. TSRuntime::MyDictCount[TSRuntime::NowUsingDictIndex] = MyDictCount[NowUsingDictIndex];
  1069. if (pData)
  1070. {
  1071. memset(pData->RetString, 0, MAX_PATH * 100);
  1072. ret = fpic.ocr(0, m_hwnd,
  1073. x1,
  1074. y1,
  1075. x2,
  1076. y2,
  1077. coloroffer,
  1078. sim,
  1079. pData->RetString, MyDictCount, nMyDictCountsize, NowUsingDictIndex, NULL, type);
  1080. }
  1081. else
  1082. {
  1083. memset(m_Retstr, 0, MAX_PATH * 100);
  1084. ret = fpic.ocr(0, m_hwnd,
  1085. x1,
  1086. y1,
  1087. x2,
  1088. y2,
  1089. coloroffer,
  1090. sim,
  1091. m_Retstr, MyDictCount, nMyDictCountsize, NowUsingDictIndex, NULL, type);
  1092. }
  1093. }
  1094. else if (m_displayMode & DISPLAY_DX_3D)
  1095. {
  1096. if (!pData->ispichook)
  1097. return 0;//如果PIC钩子HOOK失败,直接返回失败
  1098. fpic.m_hwnd = m_hwnd;
  1099. fpic.m_parenthwnd = 0;
  1100. ST_TSFINDPIC getdata;
  1101. getdata.left = x1;
  1102. getdata.top = y1;
  1103. getdata.right = x2;
  1104. getdata.bottom = y2;
  1105. getdata.ret = -1;
  1106. pData->type = TS_GETSCREENDATA;
  1107. ::memcpy(pData->pmsg, &getdata, sizeof(ST_TSFINDPIC));
  1108. wchar_t pszEventName[MAX_PATH] = { 0 };
  1109. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  1110. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  1111. if (picEvent)
  1112. {
  1113. ::WaitForSingleObject(picEvent, 10000);
  1114. ::CloseHandle(picEvent);
  1115. }
  1116. else
  1117. {
  1118. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  1119. ::WaitForSingleObject(picEvent, 3000);
  1120. ::CloseHandle(picEvent);
  1121. }
  1122. DWORD(*Data)[2000] = (DWORD(*)[2000])pData->ColorDataInfo;
  1123. memset(pData->RetString, 0, MAX_PATH * 100);
  1124. ret = fpic.ocr(0, m_hwnd,
  1125. x1,
  1126. y1,
  1127. x2,
  1128. y2,
  1129. coloroffer,
  1130. sim,
  1131. pData->RetString, MyDictCount, nMyDictCountsize, NowUsingDictIndex, Data, type);
  1132. //ST_TSFINDPIC pic;
  1133. //pic.left=x1;
  1134. //pic.top=y1;
  1135. //pic.right=x2;
  1136. //pic.bottom=y2;
  1137. //wcscpy(pic.color_format,coloroffer);
  1138. //pic.simi=sim;
  1139. //if(type==0)
  1140. // pData->type=TS_OCR;
  1141. //else if(type==1)
  1142. // pData->type=TS_OCREX;
  1143. //::memcpy(pData->pmsg,&pic,sizeof(ST_TSFINDPIC));
  1144. //wchar_t pszEventName[MAX_PATH]={0};
  1145. ////DWORD nPid=0;
  1146. ////GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  1147. //::wsprintf(pszEventName,L"%s%d",TS_EVENT_NAME,nPid);
  1148. //HANDLE picEvent=::CreateEvent(NULL,TRUE,FALSE,pszEventName);
  1149. //::WaitForSingleObject(picEvent,INFINITE);
  1150. //::CloseHandle(picEvent);
  1151. }
  1152. return ret;
  1153. }
  1154. int DXBind::findColor(LONG x1, LONG y1, LONG x2, LONG y2, int color, DOUBLE sim, LONG dir, LONG& intX, LONG& intY, wchar_t* colorstr, wchar_t* retstring, wchar_t* MultiColor)
  1155. {
  1156. int ret = 0;
  1157. if (wcslen(colorstr) >= MAX_PATH - 1)
  1158. return ret;
  1159. RECT clientrc;
  1160. ::GetClientRect(m_hwnd, &clientrc);
  1161. if (x1 >= clientrc.right || y1 >= clientrc.bottom)
  1162. {
  1163. intX = -1;
  1164. intY = -1;
  1165. return -1;
  1166. }
  1167. MyFindPictureClass fpic;
  1168. fpic.m_displayMode = m_displayMode;
  1169. fpic.m_parenthwnd = m_parenthwnd;
  1170. fpic.pWndBmpBuffer = pWndBmpBuffer;
  1171. fpic.pBuffer = pBuffer;
  1172. //if(TSRuntime::m_displayMode&DISPLAY_GDI)
  1173. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  1174. {
  1175. ret = fpic.processColor(0, m_hwnd,
  1176. color,
  1177. x1,
  1178. y1,
  1179. x2,
  1180. y2,
  1181. sim,
  1182. dir,
  1183. intX, intY, NULL, colorstr, retstring, MultiColor);
  1184. }
  1185. else if (m_displayMode & DISPLAY_DX_3D)
  1186. {
  1187. if (!pData->ispichook)
  1188. return 0;//如果PIC钩子HOOK失败,直接返回失败
  1189. fpic.m_hwnd = m_hwnd;
  1190. fpic.m_parenthwnd = 0;
  1191. ST_TSFINDPIC getdata;
  1192. getdata.left = x1;
  1193. getdata.top = y1;
  1194. getdata.right = x2;
  1195. getdata.bottom = y2;
  1196. getdata.ret = -1;
  1197. pData->type = TS_GETSCREENDATA;
  1198. ::memcpy(pData->pmsg, &getdata, sizeof(ST_TSFINDPIC));
  1199. wchar_t pszEventName[MAX_PATH] = { 0 };
  1200. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  1201. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  1202. if (picEvent)
  1203. {
  1204. ::WaitForSingleObject(picEvent, 10000);
  1205. ::CloseHandle(picEvent);
  1206. }
  1207. else
  1208. {
  1209. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  1210. ::WaitForSingleObject(picEvent, 3000);
  1211. ::CloseHandle(picEvent);
  1212. }
  1213. DWORD(*Data)[2000] = (DWORD(*)[2000])pData->ColorDataInfo;
  1214. ret = fpic.processColor(0, m_hwnd,
  1215. color,
  1216. x1,
  1217. y1,
  1218. x2,
  1219. y2,
  1220. sim,
  1221. dir,
  1222. intX, intY, Data, colorstr, retstring, MultiColor);
  1223. //ST_TSFINDPIC pic;
  1224. //pic.left=x1;
  1225. //pic.top=y1;
  1226. //pic.right=x2;
  1227. //pic.bottom=y2;
  1228. //pic.colorOffset=color;
  1229. //wcscpy(pic.color_format,colorstr);
  1230. //pic.simi=sim;
  1231. //pic.dir=dir;
  1232. //pic.x=-1;
  1233. //pic.y=-1;
  1234. //pic.ret=-1;
  1235. //if(retstring==NULL)
  1236. // pData->type=TS_FINDCOLOR;
  1237. //else
  1238. // pData->type=TS_FINDCOLOREX;
  1239. //::memcpy(pData->pmsg,&pic,sizeof(ST_TSFINDPIC));
  1240. //wchar_t pszEventName[MAX_PATH]={0};
  1241. ////DWORD nPid=0;
  1242. ////GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  1243. //::wsprintf(pszEventName,L"%s%d",TS_EVENT_NAME,nPid);
  1244. //HANDLE picEvent=::CreateEvent(NULL,TRUE,FALSE,pszEventName);
  1245. //::WaitForSingleObject(picEvent,3000);
  1246. //::CloseHandle(picEvent);
  1247. //ST_TSFINDPIC *pPic=(ST_TSFINDPIC *)pData->pmsg;
  1248. //intX=pPic->x;
  1249. //intY=pPic->y;
  1250. //ret=pPic->ret;
  1251. //if(retstring!=NULL)
  1252. // wcscpy(retstring,pData->RetString);
  1253. }
  1254. return ret;
  1255. }
  1256. bool DXBind::TSUseDict(int index)
  1257. {
  1258. MyFindPictureClass fpic;
  1259. BOOL isuse = fpic.MyUseDict(index, MyDictCount, NowUsingDictIndex);
  1260. //判断是否已经注入,者需拷贝一份字库信息到共享内存
  1261. if (pData && isuse)
  1262. {
  1263. pData->MyDictCount[TSRuntime::NowUsingDictIndex] = MyDictCount[NowUsingDictIndex];
  1264. pData->nMyDictCountsize[TSRuntime::NowUsingDictIndex] = nMyDictCountsize[NowUsingDictIndex];
  1265. pData->nMaxStrHight[TSRuntime::NowUsingDictIndex] = nMaxStrHight[NowUsingDictIndex];
  1266. pData->NowUsingDictIndex = NowUsingDictIndex;
  1267. return SendMessage(m_hwnd, TS_USEDICT, (WPARAM)GetCurrentProcessId(), 0); //发送消息通知切换字库
  1268. }
  1269. return isuse;
  1270. }
  1271. bool DXBind::TSSetDict(int index, wchar_t* filepath)
  1272. {
  1273. MyFindPictureClass fpic;
  1274. wchar_t path[MAX_PATH] = { 0 };
  1275. wchar_t* findpath = wcsstr(filepath, L":");
  1276. if (wcslen(m_SetPath) > 1 && findpath == NULL)
  1277. {
  1278. swprintf(path, L"%s\\%s", m_SetPath, filepath);
  1279. }
  1280. else
  1281. {
  1282. swprintf(path, L"%s", filepath);
  1283. }
  1284. BOOL isuse = fpic.MySetDict(index, path, MyDictCount, nMyDictCountsize, NowUsingDictIndex);
  1285. //判断是否已经注入,者需拷贝一份字库信息到共享内存
  1286. if (pData != NULL && isuse)
  1287. {
  1288. if (m_displayMode & DISPLAY_DX_3D)
  1289. {
  1290. pData->MyDictCount[NowUsingDictIndex] = MyDictCount[NowUsingDictIndex];
  1291. pData->nMyDictCountsize[NowUsingDictIndex] = nMyDictCountsize[NowUsingDictIndex];
  1292. pData->nMaxStrHight[NowUsingDictIndex] = TSRuntime::nMaxStrHight[NowUsingDictIndex];
  1293. pData->NowUsingDictIndex = NowUsingDictIndex;
  1294. return SendMessage(m_hwnd, TS_SETDICT, (WPARAM)GetCurrentProcessId(), 0); //发送消息通知切换字库
  1295. }
  1296. }
  1297. return isuse;
  1298. }
  1299. bool DXBind::TSSetpath(wchar_t* path)
  1300. {
  1301. memset(m_SetPath, 0, MAX_PATH);
  1302. wcscpy(m_SetPath, path);
  1303. if (pData)
  1304. {
  1305. memset(pData->setpath, 0, MAX_PATH);
  1306. wcscpy(pData->setpath, path);
  1307. return SendMessage(m_hwnd, TS_SETPATH, 0, 0);
  1308. }
  1309. }
  1310. bool DXBind::TSGetScreenData(LONG x1, LONG y1, LONG x2, LONG y2)
  1311. {
  1312. bool nret = FALSE;
  1313. RECT clientrc;
  1314. ::GetClientRect(m_hwnd, &clientrc);
  1315. if (x1 >= clientrc.right || y1 >= clientrc.bottom)
  1316. return 0;
  1317. memset(ColorDataInfo, 0, sizeof(ColorDataInfo));
  1318. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  1319. {
  1320. int m_Left = 0, m_Top = 0, m_Right = 0, m_bottom = 0;
  1321. int left = x1, right = x2, top = y1, bottom = y2;
  1322. int scrennwide;
  1323. int scrennheight;
  1324. HDC hDC;
  1325. if (m_displayMode & DISPLAY_NORMAL)
  1326. {
  1327. hDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  1328. //////确保选定区域是可见的
  1329. if (m_parenthwnd != ::GetDesktopWindow() && m_parenthwnd != 0)
  1330. {
  1331. //::MessageBoxA(NULL,"GetDesktopWindow","GetDesktopWindow",NULL);
  1332. RECT rec;
  1333. ::GetWindowRect(m_parenthwnd, &rec);
  1334. int xc = ::GetSystemMetrics(SM_CYCAPTION);
  1335. int cx = ::GetSystemMetrics(SM_CXFRAME);
  1336. int cy = ::GetSystemMetrics(SM_CYFRAME);
  1337. m_Left = rec.left + cx;
  1338. m_Top = rec.top + xc + cy;
  1339. m_Right = rec.right;
  1340. m_bottom = rec.bottom;
  1341. scrennwide = ::GetSystemMetrics(SM_CXSCREEN);
  1342. scrennheight = ::GetSystemMetrics(SM_CYSCREEN);
  1343. if (m_Left < 0)
  1344. {
  1345. if ((left + m_Left) <= 0)
  1346. left = 0;//越界
  1347. else
  1348. left = m_Left + left;
  1349. }
  1350. else
  1351. left = m_Left + left;
  1352. if (m_Top >= 0)
  1353. top = m_Top + top;
  1354. if (m_Right >= scrennwide)
  1355. {
  1356. if ((right + m_Left) > scrennwide)
  1357. right = scrennwide - 1;
  1358. else
  1359. right = right + m_Left;
  1360. }
  1361. else
  1362. right = right + m_Left;
  1363. if (m_bottom >= scrennheight)
  1364. {
  1365. if ((bottom + m_Top) >= scrennheight)
  1366. bottom = scrennheight - 1;
  1367. else
  1368. bottom = bottom + m_Top;
  1369. }
  1370. else
  1371. bottom = bottom + m_Top;
  1372. x1 = left;
  1373. y1 = top;
  1374. x2 = right;
  1375. y2 = bottom;
  1376. }
  1377. }
  1378. else//GDI边界值判断
  1379. {
  1380. hDC = GetDCEx(m_hwnd, NULL, DCX_PARENTCLIP);
  1381. RECT clientrc;
  1382. ::GetClientRect(m_hwnd, &clientrc);
  1383. scrennwide = clientrc.right - clientrc.left;
  1384. scrennheight = clientrc.bottom - clientrc.top;
  1385. m_Left = left;
  1386. m_Top = top;
  1387. m_Right = right;
  1388. m_bottom = bottom;
  1389. if (left < 0)
  1390. m_Left = left = 0;
  1391. if (left >= scrennwide)
  1392. m_Left = scrennwide - 1;
  1393. if (top < 0)
  1394. m_Top = top = 0;
  1395. if (top >= scrennheight)
  1396. m_Top = scrennheight - 1;
  1397. if (right >= scrennwide)
  1398. m_Right = scrennwide - 1;
  1399. if (bottom >= scrennheight)
  1400. m_bottom = scrennheight - 1;
  1401. x1 = left;
  1402. y1 = top;
  1403. x2 = right;
  1404. y2 = bottom;
  1405. }
  1406. HDC hMemDC; //内存缓冲设备环境
  1407. HBITMAP hbmMem, hbmOld; //内存缓冲设备环境中的位图
  1408. // 24位图的BITMAPINFO
  1409. BITMAPINFO* pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
  1410. memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
  1411. BITMAPINFOHEADER* pInfo_Header = (BITMAPINFOHEADER*)pBITMAPINFO;
  1412. pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
  1413. pInfo_Header->biWidth = x2 - x1;
  1414. pInfo_Header->biHeight = y2 - y1;
  1415. pInfo_Header->biPlanes = 1;
  1416. pInfo_Header->biBitCount = 24;
  1417. pInfo_Header->biCompression = BI_RGB;
  1418. int xc = 0;
  1419. int cx = 0;
  1420. int cy = 0;
  1421. hMemDC = CreateCompatibleDC(hDC); //创建内存兼容设备环境
  1422. //创建内存兼容位图
  1423. hbmMem = CreateCompatibleBitmap(hDC, pInfo_Header->biWidth, pInfo_Header->biHeight);
  1424. hbmOld = (HBITMAP)SelectObject(hMemDC, hbmMem);
  1425. if ((m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  1426. {
  1427. RECT winret;
  1428. ::GetWindowRect(m_hwnd, &winret);
  1429. POINT point;
  1430. point.x = 0;
  1431. point.y = 0;
  1432. ::ClientToScreen(m_hwnd, &point);
  1433. cx = point.x - winret.left;
  1434. cy = point.y - winret.top;
  1435. HDC T_hMemDC = CreateCompatibleDC(hDC); //创建内存兼容设备环境
  1436. //创建内存兼容位图
  1437. HBITMAP T_hbmMem = CreateCompatibleBitmap(hDC, pInfo_Header->biWidth + cx + x1, pInfo_Header->biHeight + cy + y1);
  1438. HBITMAP T_hbmOld = (HBITMAP)SelectObject(T_hMemDC, T_hbmMem);
  1439. ::UpdateWindow(m_hwnd);
  1440. ::RedrawWindow(m_hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN | RDW_FRAME);
  1441. ::PrintWindow(m_hwnd, T_hMemDC, 0);
  1442. BitBlt(hMemDC, 0, 0, pInfo_Header->biWidth, pInfo_Header->biHeight, T_hMemDC, xc + x1, cy + y1, CAPTUREBLT | SRCCOPY);
  1443. DeleteDC(T_hMemDC);
  1444. DeleteObject(T_hbmMem);
  1445. DeleteObject(T_hbmOld);
  1446. }
  1447. else
  1448. {
  1449. //将内存设备环境中的内容绘制到物理设备环境 hDC
  1450. BitBlt(hMemDC, 0, 0, pInfo_Header->biWidth, pInfo_Header->biHeight, hDC, xc + x1, cy + y1, CAPTUREBLT | SRCCOPY);
  1451. }
  1452. HBITMAP hBitmap = (HBITMAP)SelectObject(hMemDC, hbmOld);
  1453. // 获得数据buf
  1454. DWORD bufSize = (pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
  1455. BYTE* pBuffer = new BYTE[bufSize];
  1456. int aHeight = pInfo_Header->biHeight;
  1457. if (::GetDIBits(hMemDC, hBitmap, 0, aHeight, pBuffer, pBITMAPINFO, DIB_RGB_COLORS) == 0)
  1458. {
  1459. if (TSRuntime::IsShowErrorMsg)
  1460. ::MessageBox(NULL, L"加载位图失败", L"TS", 0);
  1461. return false;
  1462. }
  1463. int wndWidth = pBITMAPINFO->bmiHeader.biWidth;
  1464. int wndHeight = pBITMAPINFO->bmiHeader.biHeight;
  1465. //TSRuntime::add_log( "wndHeight:%d wndWidth%d",wndHeight,wndWidth);
  1466. //HDC dd = ::GetWindowDC(::GetDesktopWindow());
  1467. COLORREF helpcolor = 0;
  1468. int pitch = wndWidth % 4;
  1469. for (int i = 0; i < wndHeight; i++)
  1470. {
  1471. int realPitch = i * pitch;
  1472. for (int j = 0; j < wndWidth; j++)
  1473. {
  1474. UCHAR b = pBuffer[(i * wndWidth + j) * 3 + realPitch];
  1475. UCHAR g = pBuffer[(i * wndWidth + j) * 3 + 1 + realPitch];
  1476. UCHAR r = pBuffer[(i * wndWidth + j) * 3 + 2 + realPitch];
  1477. helpcolor = RGB(r, g, b);
  1478. ColorDataInfo[wndHeight - i - 1][j] = helpcolor;
  1479. //pColorBuffer[wndHeight-i-1][j]=helpcolor;
  1480. //::SetPixel(dd,wndHeight-i-1,j,helpcolor);
  1481. }
  1482. }
  1483. if (m_hwnd == GetDesktopWindow())
  1484. DeleteDC(hDC);
  1485. else
  1486. ReleaseDC(m_hwnd, hDC);
  1487. //释放资源
  1488. DeleteObject(hbmMem);
  1489. DeleteObject(hbmOld);
  1490. DeleteDC(hMemDC);
  1491. free(pBITMAPINFO);
  1492. ::DeleteObject(hBitmap);
  1493. delete[] pBuffer;
  1494. }
  1495. else if (m_displayMode & DISPLAY_DX_3D)
  1496. {
  1497. if (!pData->ispichook)
  1498. return 0;//如果PIC钩子HOOK失败,直接返回失败
  1499. ST_TSFINDPIC getdata;
  1500. getdata.left = x1;
  1501. getdata.top = y1;
  1502. getdata.right = x2;
  1503. getdata.bottom = y2;
  1504. getdata.ret = -1;
  1505. pData->type = TS_GETSCREENDATA;
  1506. ::memcpy(pData->pmsg, &getdata, sizeof(ST_TSFINDPIC));
  1507. wchar_t pszEventName[MAX_PATH] = { 0 };
  1508. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  1509. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  1510. if (picEvent)
  1511. {
  1512. ::WaitForSingleObject(picEvent, 10000);
  1513. ::CloseHandle(picEvent);
  1514. }
  1515. else
  1516. {
  1517. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  1518. ::WaitForSingleObject(picEvent, 3000);
  1519. ::CloseHandle(picEvent);
  1520. }
  1521. ST_TSFINDPIC* pcaptrue = (ST_TSFINDPIC*)pData->pmsg;
  1522. nret = pcaptrue->ret;
  1523. memcpy(ColorDataInfo, pData->ColorDataInfo, sizeof(ColorDataInfo));
  1524. return nret;
  1525. }
  1526. }
  1527. int DXBind::Capture(LONG x1, LONG y1, LONG x2, LONG y2, wchar_t* pic_path, int pictype)
  1528. {
  1529. //TSRuntime::add_log( "截图");
  1530. int nret = 0;
  1531. //if(TSRuntime::m_displayMode&DISPLAY_GDI)
  1532. RECT clientrc;
  1533. ::GetClientRect(m_hwnd, &clientrc);
  1534. if (x1 >= clientrc.right || y1 >= clientrc.bottom)
  1535. return 0;//已经越界
  1536. ::GetClientRect(m_hwnd, &clientrc);
  1537. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  1538. {
  1539. //TSRuntime::add_log( "gdi模式截图");
  1540. wchar_t savepath[MAX_PATH] = { 0 };
  1541. wchar_t* findpath = wcsstr(pic_path, L":");
  1542. if (wcslen(m_SetPath) > 1 && findpath == NULL)
  1543. {
  1544. swprintf(savepath, L"%s\\%s", m_SetPath, pic_path);
  1545. }
  1546. else
  1547. {
  1548. swprintf(savepath, L"%s", pic_path);
  1549. }
  1550. MyFindPictureClass pic;
  1551. pic.m_displayMode = m_displayMode;
  1552. pic.m_parenthwnd = m_parenthwnd;
  1553. //TSRuntime::add_log( "pic.m_parenthwnd:%d",pic.m_parenthwnd);
  1554. RECT rc;
  1555. rc.left = x1;
  1556. rc.top = y1;
  1557. rc.right = x2;
  1558. rc.bottom = y2;
  1559. int scrennwide;
  1560. int scrennheight;
  1561. scrennwide = ::GetSystemMetrics(SM_CXSCREEN);
  1562. scrennheight = ::GetSystemMetrics(SM_CYSCREEN);
  1563. bool bret = false;
  1564. if (pictype == D3DXIFF_JPG || pictype == D3DXIFF_PNG)
  1565. {
  1566. wchar_t temppath[MAX_PATH] = { 0 };
  1567. wchar_t path[MAX_PATH] = { 0 };
  1568. GetTempPath(MAX_PATH, temppath);
  1569. //DWORD nPid=0;
  1570. //GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  1571. swprintf(path, L"%s\\TEMPBMP%d.BMP", temppath, nPid);
  1572. if (m_displayMode & DISPLAY_NORMAL)
  1573. {
  1574. if (m_parenthwnd != ::GetDesktopWindow() && m_parenthwnd != 0)
  1575. {
  1576. int m_Left = 0, m_Top = 0, m_Right = 0, m_bottom = 0;
  1577. int left = x1, right = x2, top = y1, bottom = y2;
  1578. RECT rec;
  1579. ::GetWindowRect(m_parenthwnd, &rec);
  1580. int xc = 0;
  1581. int cx = 0;
  1582. int cy = 0;
  1583. if (m_hwnd != GetDesktopWindow())
  1584. {
  1585. xc = ::GetSystemMetrics(SM_CYCAPTION);
  1586. cx = ::GetSystemMetrics(SM_CXFRAME);
  1587. cy = ::GetSystemMetrics(SM_CYFRAME);
  1588. }
  1589. m_Left = rec.left + cx;
  1590. m_Top = rec.top + xc + cy;
  1591. m_Right = rec.right;
  1592. m_bottom = rec.bottom;
  1593. if (m_Left < 0)
  1594. {
  1595. if ((left + m_Left) <= 0)
  1596. left = 0;//越界
  1597. else
  1598. left = m_Left + left;
  1599. }
  1600. else
  1601. left = m_Left + left;
  1602. if (m_Top >= 0)
  1603. top = m_Top + top;
  1604. if (m_Right >= scrennwide)
  1605. {
  1606. if ((right + m_Left) > scrennwide)
  1607. right = scrennwide - 1;
  1608. else
  1609. right = right + m_Left;
  1610. }
  1611. else
  1612. right = right + m_Left;
  1613. if (m_bottom >= scrennheight)
  1614. {
  1615. if ((bottom + m_Top) >= scrennheight)
  1616. bottom = scrennheight - 1;
  1617. else
  1618. bottom = bottom + m_Top;
  1619. }
  1620. else
  1621. bottom = bottom + m_Top;
  1622. rc.left = left;
  1623. rc.top = top;
  1624. rc.right = right;
  1625. rc.bottom = bottom;
  1626. }
  1627. else
  1628. {
  1629. ////确保选定区域是可见的
  1630. if (rc.left < 0)
  1631. rc.left = 0;
  1632. if (rc.top < 0)
  1633. rc.top = 0;
  1634. if (rc.right > scrennwide)
  1635. rc.right = scrennwide;
  1636. if (rc.bottom > scrennheight)
  1637. rc.bottom = scrennheight;
  1638. }
  1639. bret = pic.CopyScreenToBitmap(&rc, path);
  1640. if (pictype == D3DXIFF_JPG && bret)
  1641. bret = pic.BMP2JPG(path, savepath);
  1642. else if (pictype == D3DXIFF_PNG && bret)
  1643. bret = pic.BMP2PNG(path, savepath);
  1644. DeleteFile(path);
  1645. }
  1646. else if (pic.SaveGDIBitmap(m_hwnd, rc, path))
  1647. {
  1648. ////确保选定区域是可见的
  1649. if (rc.left < 0)
  1650. rc.left = 0;
  1651. if (rc.top < 0)
  1652. rc.top = 0;
  1653. if (rc.right > clientrc.right)
  1654. rc.right = clientrc.right;
  1655. if (rc.bottom > clientrc.bottom)
  1656. rc.bottom = clientrc.bottom;
  1657. if (pictype == D3DXIFF_JPG)
  1658. bret = pic.BMP2JPG(path, savepath);
  1659. else if (pictype == D3DXIFF_PNG)
  1660. bret = pic.BMP2PNG(path, savepath);
  1661. DeleteFile(path);
  1662. }
  1663. }
  1664. else if (pictype == D3DXIFF_BMP)
  1665. {
  1666. if (m_displayMode & DISPLAY_NORMAL)
  1667. {
  1668. //::GetWindowRect(m_parenthwnd,&rc);
  1669. //int xc =::GetSystemMetrics(SM_CYCAPTION);
  1670. //int cx =::GetSystemMetrics(SM_CXFRAME);
  1671. //int cy =::GetSystemMetrics(SM_CYFRAME);
  1672. //rc.top=rc.top+xc+cy;
  1673. if (m_parenthwnd != ::GetDesktopWindow() && m_parenthwnd != 0)
  1674. {
  1675. int m_Left = 0, m_Top = 0, m_Right = 0, m_bottom = 0;
  1676. int left = x1, right = x2, top = y1, bottom = y2;
  1677. RECT rec;
  1678. ::GetWindowRect(m_parenthwnd, &rec);
  1679. int xc = 0;
  1680. int cx = 0;
  1681. int cy = 0;
  1682. if (m_hwnd != GetDesktopWindow())
  1683. {
  1684. xc = ::GetSystemMetrics(SM_CYCAPTION);
  1685. cx = ::GetSystemMetrics(SM_CXFRAME);
  1686. cy = ::GetSystemMetrics(SM_CYFRAME);
  1687. }
  1688. m_Left = rec.left + cx;
  1689. m_Top = rec.top + xc + cy;
  1690. m_Right = rec.right;
  1691. m_bottom = rec.bottom;
  1692. if (m_Left < 0)
  1693. {
  1694. if ((left + m_Left) <= 0)
  1695. left = 0;//越界
  1696. else
  1697. left = m_Left + left;
  1698. }
  1699. else
  1700. left = m_Left + left;
  1701. if (m_Top >= 0)
  1702. top = m_Top + top;
  1703. if (m_Right >= scrennwide)
  1704. {
  1705. if ((right + m_Left) > scrennwide)
  1706. right = scrennwide - 1;
  1707. else
  1708. right = right + m_Left;
  1709. }
  1710. else
  1711. right = right + m_Left;
  1712. if (m_bottom >= scrennheight)
  1713. {
  1714. if ((bottom + m_Top) >= scrennheight)
  1715. bottom = scrennheight - 1;
  1716. else
  1717. bottom = bottom + m_Top;
  1718. }
  1719. else
  1720. bottom = bottom + m_Top;
  1721. rc.left = left;
  1722. rc.top = top;
  1723. rc.right = right;
  1724. rc.bottom = bottom;
  1725. }
  1726. else
  1727. {
  1728. ////确保选定区域是可见的
  1729. if (rc.left < 0)
  1730. rc.left = 0;
  1731. if (rc.top < 0)
  1732. rc.top = 0;
  1733. if (rc.right > scrennwide)
  1734. rc.right = scrennwide;
  1735. if (rc.bottom > scrennheight)
  1736. rc.bottom = scrennheight;
  1737. }
  1738. bret = pic.CopyScreenToBitmap(&rc, savepath);
  1739. }
  1740. else
  1741. bret = pic.SaveGDIBitmap(m_hwnd, rc, savepath);
  1742. }
  1743. if (bret)
  1744. nret = 1;
  1745. else
  1746. nret = 0;
  1747. //USES_CONVERSION;
  1748. //TSRuntime::add_log( "findPicture,pic_path:%s,X:%d,Y:%d",W2A(pic_name),intX,intY);
  1749. return nret;
  1750. }
  1751. //else if(TSRuntime::m_displayMode&DISPLAY_DX_3D)
  1752. else if (m_displayMode & DISPLAY_DX_3D)
  1753. {
  1754. //TSRuntime::add_log( "dx模式截图");
  1755. if (!pData->ispichook)
  1756. return 0;//如果PIC钩子HOOK失败,直接返回失败
  1757. ST_TSFINDPIC captrue;
  1758. captrue.left = x1;
  1759. captrue.top = y1;
  1760. captrue.right = x2;
  1761. captrue.bottom = y2;
  1762. wcscpy(captrue.path, pic_path);
  1763. captrue.pictype = pictype;
  1764. captrue.ret = -1;
  1765. pData->type = TS_CAPTURE;
  1766. ::memcpy(pData->pmsg, &captrue, sizeof(ST_TSFINDPIC));
  1767. wchar_t pszEventName[MAX_PATH] = { 0 };
  1768. //DWORD nPid=0;
  1769. //GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  1770. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  1771. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  1772. if (picEvent)
  1773. {
  1774. ::WaitForSingleObject(picEvent, 10000);
  1775. ::CloseHandle(picEvent);
  1776. }
  1777. else
  1778. {
  1779. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  1780. ::WaitForSingleObject(picEvent, 3000);
  1781. ::CloseHandle(picEvent);
  1782. }
  1783. ST_TSFINDPIC* pcaptrue = (ST_TSFINDPIC*)pData->pmsg;
  1784. nret = pcaptrue->ret;
  1785. return nret;
  1786. }
  1787. }
  1788. //创建GIF图片
  1789. int DXBind::CaptureGif(LONG x1, LONG y1, LONG x2, LONG y2, wchar_t* pic_path, int delay, int time)
  1790. {
  1791. //先截图,然后合成GIF图片
  1792. wchar_t temppath[MAX_PATH] = { 0 };
  1793. //最多存储260*2张图片
  1794. wchar_t path[MAX_PATH * 4][MAX_PATH] = { 0 };
  1795. GetTempPath(MAX_PATH, temppath);
  1796. //DWORD pid=0;
  1797. //GetWindowThreadProcessId(m_hwnd,&pid);
  1798. DWORD time1 = GetTickCount();
  1799. int n = 0;
  1800. //先截图,存储临时图片到临时路径中
  1801. while (1)
  1802. {
  1803. swprintf(path[n], L"%s\\%d%d.bmp", temppath, GetTickCount(), nPid);
  1804. Capture(x1, y1, x2, y2, path[n], D3DXIFF_BMP);
  1805. n++;
  1806. DWORD time2 = GetTickCount();
  1807. if ((time2 - time1) > time || n == (MAX_PATH * 4 - 1))
  1808. break;
  1809. Sleep(delay);
  1810. }
  1811. //然后合成GIF图片
  1812. wchar_t savepath[MAX_PATH] = { 0 };
  1813. wchar_t* findpath = wcsstr(pic_path, L":");
  1814. if (wcslen(m_SetPath) > 1 && findpath == NULL)
  1815. {
  1816. swprintf(savepath, L"%s\\%s", m_SetPath, pic_path);
  1817. }
  1818. else
  1819. {
  1820. swprintf(savepath, L"%s", pic_path);
  1821. }
  1822. BYTE* palette = NULL;
  1823. BYTE* pData = NULL;
  1824. int nWidth, nHeight;
  1825. BYTE bitsPixel = 8;
  1826. //if(PathFileExists(savepath))
  1827. // {
  1828. // if(!DeleteFile(savepath)) //删除文件失败文件被占用
  1829. // return 0;
  1830. // }
  1831. HANDLE hfile = ::CreateFile(savepath, GENERIC_ALL, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);//打开文件//不存在创建
  1832. //写GIF头
  1833. CreateGIFHeard(hfile, nWidth, nHeight, bitsPixel);
  1834. int m = 0; //决定合成图片的个数
  1835. if (delay <= 0)
  1836. m = 1;
  1837. else if (time / delay < n)
  1838. m = time / delay;
  1839. else if (time / delay >= n)
  1840. m = n;
  1841. for (int i = 0; i < m; i++)
  1842. {
  1843. HBITMAP hBmp2 = (HBITMAP)LoadImage(NULL, path[i], IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
  1844. if (hBmp2 == NULL)
  1845. {
  1846. CloseGIF(hfile);
  1847. CloseHandle(hfile);
  1848. break;
  1849. //return 0;
  1850. }
  1851. if (GetData(hBmp2, &palette, &pData, &bitsPixel, &nWidth, &nHeight) == FALSE)
  1852. {
  1853. DeleteObject(hBmp2);
  1854. CloseGIF(hfile);
  1855. CloseHandle(hfile);
  1856. break;
  1857. //return 0;
  1858. }
  1859. DeleteObject(hBmp2);
  1860. short nTransparentColorIndex = -1;
  1861. //加入其它图片
  1862. AddImageToGIF(hfile, pData, palette, nWidth, nHeight, bitsPixel, delay, nTransparentColorIndex);
  1863. delete[]pData;
  1864. delete[]palette;
  1865. }
  1866. //结束GIF
  1867. CloseGIF(hfile);
  1868. CloseHandle(hfile);
  1869. //然后合成GIF图片
  1870. //清除临时图片到临时路径中图片
  1871. for (int i = 0; i < n; i++)
  1872. DeleteFile(path[i]);
  1873. return 1;
  1874. }
  1875. //Findstrtype=0的时候Findstr=Findstr;Findstrtype=1的时候Findstr=FindStrFast;
  1876. int DXBind::Findstr(LONG x1, LONG y1, LONG x2, LONG y2, wchar_t* findstring, wchar_t* color, DOUBLE sim, LONG& intX, LONG& intY, BYTE Findstrtype, wchar_t* retstring, int type)
  1877. {
  1878. //TSRuntime::add_log( "Findstr");
  1879. intX = -1;
  1880. intY = -1;
  1881. if (wcslen(color) > MAX_PATH)
  1882. return -1;
  1883. RECT clientrc;
  1884. ::GetClientRect(m_hwnd, &clientrc);
  1885. if (x1 >= clientrc.right || y1 >= clientrc.bottom)
  1886. {
  1887. intX = -1;
  1888. intY = -1;
  1889. return -1;
  1890. }
  1891. int findstringlen = wcslen(findstring);
  1892. if (findstringlen<1 || findstringlen>MAX_PATH * 4)
  1893. {
  1894. return -1;
  1895. }
  1896. wchar_t coloroffer[MAX_PATH] = { 0 };
  1897. swprintf(coloroffer, L"%s", color);
  1898. wchar_t fstring[MAX_PATH * 4] = { 0 };
  1899. wcscpy(fstring, findstring);
  1900. MyFindPictureClass fpic;
  1901. fpic.m_displayMode = m_displayMode;
  1902. fpic.m_parenthwnd = m_parenthwnd;
  1903. fpic.pWndBmpBuffer = pWndBmpBuffer;
  1904. fpic.pWndBmpStrxy = pWndBmpStrxy;
  1905. fpic.pBuffer = pBuffer;
  1906. int bret = -1;
  1907. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  1908. {
  1909. TSRuntime::NowUsingDictIndex = NowUsingDictIndex;
  1910. TSRuntime::nMyDictCountsize[TSRuntime::NowUsingDictIndex] = nMyDictCountsize[NowUsingDictIndex];
  1911. TSRuntime::nMaxStrHight[TSRuntime::NowUsingDictIndex] = nMaxStrHight[NowUsingDictIndex];
  1912. TSRuntime::MyDictCount[TSRuntime::NowUsingDictIndex] = MyDictCount[NowUsingDictIndex];
  1913. if (Findstrtype == 0) //FindStr
  1914. {
  1915. bret = fpic.FindStr(NULL, m_hwnd, x1,
  1916. y1,
  1917. x2,
  1918. y2,
  1919. fstring,
  1920. coloroffer,
  1921. sim,
  1922. intX, intY, MyDictCount, nMyDictCountsize, NowUsingDictIndex, NULL, retstring, type);
  1923. }
  1924. else if (Findstrtype == 1) //FindStrFast
  1925. {
  1926. bret = fpic.FindStrFast(NULL, m_hwnd, x1,
  1927. y1,
  1928. x2,
  1929. y2,
  1930. fstring,
  1931. coloroffer,
  1932. sim,
  1933. intX, intY, MyDictCount, nMyDictCountsize, NowUsingDictIndex, NULL, retstring, type);
  1934. }
  1935. //USES_CONVERSION;
  1936. //TSRuntime::add_log( "findPicture,findstring:%s,X:%d,Y:%d",W2A(findstring),intX,intY);
  1937. return bret;
  1938. }
  1939. else if (m_displayMode & DISPLAY_DX_3D)
  1940. {
  1941. if (!pData->ispichook)
  1942. return 0;//如果PIC钩子HOOK失败,直接返回失败
  1943. fpic.m_hwnd = m_hwnd;
  1944. fpic.m_parenthwnd = 0;
  1945. ST_TSFINDPIC getdata;
  1946. getdata.left = x1;
  1947. getdata.top = y1;
  1948. getdata.right = x2;
  1949. getdata.bottom = y2;
  1950. getdata.ret = -1;
  1951. pData->type = TS_GETSCREENDATA;
  1952. ::memcpy(pData->pmsg, &getdata, sizeof(ST_TSFINDPIC));
  1953. wchar_t pszEventName[MAX_PATH] = { 0 };
  1954. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  1955. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  1956. if (picEvent)
  1957. {
  1958. ::WaitForSingleObject(picEvent, 10000);
  1959. ::CloseHandle(picEvent);
  1960. }
  1961. else
  1962. {
  1963. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  1964. ::WaitForSingleObject(picEvent, 3000);
  1965. ::CloseHandle(picEvent);
  1966. }
  1967. DWORD(*Data)[2000] = (DWORD(*)[2000])pData->ColorDataInfo;
  1968. if (Findstrtype == 0) //FindStr
  1969. {
  1970. bret = fpic.FindStr(NULL, m_hwnd, x1,
  1971. y1,
  1972. x2,
  1973. y2,
  1974. fstring,
  1975. coloroffer,
  1976. sim,
  1977. intX, intY, MyDictCount, nMyDictCountsize, NowUsingDictIndex, Data, retstring, type);
  1978. }
  1979. else if (Findstrtype == 1) //FindStrFast
  1980. {
  1981. bret = fpic.FindStrFast(NULL, m_hwnd, x1,
  1982. y1,
  1983. x2,
  1984. y2,
  1985. fstring,
  1986. coloroffer,
  1987. sim,
  1988. intX, intY, MyDictCount, nMyDictCountsize, NowUsingDictIndex, Data, retstring, type);
  1989. }
  1990. return bret;
  1991. //TSRuntime::add_log( "dxFindstr");
  1992. //ST_TSFINDPIC pic;
  1993. //pic.left=x1;
  1994. //pic.top=y1;
  1995. //pic.right=x2;
  1996. //pic.bottom=y2;
  1997. //wcscpy(pic.path,findstring);
  1998. //wcscpy(pic.color_format,coloroffer);
  1999. //pic.simi=sim;
  2000. //pic.x=-1;
  2001. //pic.y=-1;
  2002. //pic.ret=-1;
  2003. //if(Findstrtype==0&&retstring==NULL) //FindStr
  2004. // pData->type=TS_FINDSTR;
  2005. //else if(Findstrtype==1&&retstring==NULL) //FindStrFast
  2006. // pData->type=TS_FINDSTRFAST;
  2007. //else if(Findstrtype==0&&retstring!=NULL&&type==0)//FindStrEx
  2008. // pData->type=TS_FINDSTREX;
  2009. //else if(Findstrtype==1&&retstring!=NULL&&type==0)//FindStrFastEx
  2010. // pData->type=TS_FINDSTRFASTEX;
  2011. //else if(Findstrtype==0&&retstring==NULL&&type==2) //FindStrS
  2012. // pData->type=TS_FINDSTRS;
  2013. //else if(Findstrtype==1&&retstring==NULL&&type==2) //FindStrFastS
  2014. // pData->type=TS_FINDSTRFASTS;
  2015. //else if(Findstrtype==0&&retstring!=NULL&&type==1) //FindStrExS
  2016. // pData->type=TS_FINDSTREXS;
  2017. //else if(Findstrtype==1&&retstring!=NULL&&type==1) //FindStrFastExS
  2018. // pData->type=TS_FINDSTRFASTEXS;
  2019. //::memcpy(pData->pmsg,&pic,sizeof(ST_TSFINDPIC));
  2020. //wchar_t pszEventName[MAX_PATH]={0};
  2021. ////DWORD nPid=0;
  2022. ////GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  2023. //::wsprintf(pszEventName,L"%s%d",TS_EVENT_NAME,nPid);
  2024. //HANDLE picEvent=::CreateEvent(NULL,TRUE,FALSE,pszEventName);
  2025. //::WaitForSingleObject(picEvent,INFINITE);
  2026. //::CloseHandle(picEvent);
  2027. //ST_TSFINDPIC *pPic=(ST_TSFINDPIC *)pData->pmsg;
  2028. //intX=pPic->x;
  2029. //intY=pPic->y;
  2030. //if(retstring!=NULL)
  2031. // wcscpy(retstring,pData->RetString);
  2032. //return pPic->ret;
  2033. }
  2034. }
  2035. int DXBind::findPic(LONG x1, LONG y1, LONG x2, LONG y2, wchar_t* pic_name, wchar_t* color, DOUBLE sim, LONG dir, LONG& intX, LONG& intY, wchar_t* retstring, int type)
  2036. {
  2037. //TSRuntime::add_log( "找图");
  2038. //if(TSRuntime::m_displayMode&DISPLAY_GDI)
  2039. if (wcslen(pic_name) >= 1024)
  2040. {
  2041. intX = -1;
  2042. intY = -1;
  2043. return -1;
  2044. }
  2045. RECT clientrc;
  2046. ::GetClientRect(m_hwnd, &clientrc);
  2047. if (x1 >= clientrc.right || y1 >= clientrc.bottom)
  2048. {
  2049. intX = -1;
  2050. intY = -1;
  2051. return -1;
  2052. }
  2053. int bret = 0;
  2054. MyFindPictureClass fpic;
  2055. fpic.m_displayMode = m_displayMode;
  2056. fpic.m_parenthwnd = m_parenthwnd;
  2057. fpic.pWndBmpBuffer = pWndBmpBuffer;
  2058. fpic.pBuffer = pBuffer;
  2059. if ((m_displayMode & DISPLAY_GDI) == DISPLAY_GDI || (m_displayMode & DISPLAY_NORMAL) == DISPLAY_NORMAL || (m_displayMode & DISPLAY_DX2) == DISPLAY_DX2 || (m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  2060. {
  2061. if (wcslen(m_SetPath) > 0)
  2062. wcscpy(fpic.SetPath, m_SetPath);
  2063. bret = fpic.findPicture(NULL, m_hwnd, x1,
  2064. y1,
  2065. x2,
  2066. y2,
  2067. pic_name,
  2068. color,
  2069. sim,
  2070. dir,
  2071. intX, intY, NULL, retstring, type);
  2072. //USES_CONVERSION;
  2073. //TSRuntime::add_log( "findPicture,pic_path:%s,X:%d,Y:%d",W2A(pic_name),intX,intY);
  2074. return bret;
  2075. }
  2076. else if (m_displayMode & DISPLAY_DX_3D)
  2077. {
  2078. if (!pData->ispichook)
  2079. return 0;//如果PIC钩子HOOK失败,直接返回失败
  2080. fpic.m_hwnd = m_hwnd;
  2081. fpic.m_parenthwnd = 0;
  2082. ST_TSFINDPIC getdata;
  2083. getdata.left = x1;
  2084. getdata.top = y1;
  2085. getdata.right = x2;
  2086. getdata.bottom = y2;
  2087. getdata.ret = -1;
  2088. pData->type = TS_GETSCREENDATA;
  2089. ::memcpy(pData->pmsg, &getdata, sizeof(ST_TSFINDPIC));
  2090. wchar_t pszEventName[MAX_PATH] = { 0 };
  2091. ::wsprintf(pszEventName, L"%s%d", TS_EVENT_NAME, nPid);
  2092. HANDLE picEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, pszEventName);
  2093. if (picEvent)
  2094. {
  2095. ::WaitForSingleObject(picEvent, 10000);
  2096. ::CloseHandle(picEvent);
  2097. }
  2098. else
  2099. {
  2100. picEvent = ::CreateEvent(NULL, TRUE, FALSE, pszEventName);
  2101. ::WaitForSingleObject(picEvent, 3000);
  2102. ::CloseHandle(picEvent);
  2103. }
  2104. DWORD(*Data)[2000] = (DWORD(*)[2000])pData->ColorDataInfo;
  2105. if (wcslen(m_SetPath) > 0)
  2106. wcscpy(fpic.SetPath, m_SetPath);
  2107. bret = fpic.findPicture(NULL, m_hwnd, x1,
  2108. y1,
  2109. x2,
  2110. y2,
  2111. pic_name,
  2112. color,
  2113. sim,
  2114. dir,
  2115. intX, intY, Data, retstring, type);
  2116. return bret;
  2117. //TSRuntime::add_log( "dx模式找图,ndx:%d",++add);
  2118. //ST_TSFINDPIC pic;
  2119. //pic.left=x1;
  2120. //pic.top=y1;
  2121. //pic.right=x2;
  2122. //pic.bottom=y2;
  2123. //wcscpy(pic.path,pic_name);
  2124. //wcscpy(pic.color_format,color);
  2125. ////pic.colorOffset=delta_color;
  2126. //pic.simi=sim;
  2127. //pic.dir=dir;
  2128. //pic.x=-1;
  2129. //pic.y=-1;
  2130. //pic.ret=-1;
  2131. //if(retstring==NULL)
  2132. // pData->type=TS_FINDPIC;
  2133. //else if(retstring&&type==2)
  2134. // pData->type=TS_FINDPICS;
  2135. //else if(retstring&&type==0)
  2136. // pData->type=TS_FINDPICEX;
  2137. //else if(retstring&&type==1)
  2138. // pData->type=TS_FINDPICEXS;
  2139. //::memcpy(pData->pmsg,&pic,sizeof(ST_TSFINDPIC));
  2140. ////DWORD nPid=0;
  2141. ////GetWindowThreadProcessId(m_hwnd,&nPid); //获取注入窗口的进程ID
  2142. //wchar_t pszEventName[MAX_PATH]={0};
  2143. //::wsprintf(pszEventName,L"%s%d",TS_EVENT_NAME,nPid);
  2144. //HANDLE picEvent=::CreateEvent(NULL,TRUE,FALSE,pszEventName);
  2145. //::WaitForSingleObject(picEvent,INFINITE);
  2146. //::CloseHandle(picEvent);
  2147. //ST_TSFINDPIC *pPic=(ST_TSFINDPIC *)pData->pmsg;
  2148. //intX=pPic->x;
  2149. //intY=pPic->y;
  2150. //if(retstring!=NULL)
  2151. // wcscpy(retstring,pData->RetString);
  2152. //return pPic->ret;
  2153. }
  2154. }
  2155. //判断是否卡屏
  2156. int DXBind::IsDisplayDead(LONG x1, LONG y1, LONG x2, LONG y2, int times)
  2157. {
  2158. //DWORD npid=0;
  2159. //GetWindowThreadProcessId(m_hwnd,&npid);
  2160. wchar_t path[MAX_PATH] = { 0 };
  2161. if (nPid != 0)
  2162. {
  2163. GetTempPath(MAX_PATH, path);
  2164. swprintf(path, L"%s\\tbmp%d.bmp", path, nPid);
  2165. Capture(x1, y1, x2, y2, path, D3DXIFF_BMP);
  2166. times = times * 1000;
  2167. LONG retx = -1, rety = -1;
  2168. int t1, t2, t3 = 0;
  2169. for (int i = 0; i < times; i = i + t3)
  2170. {
  2171. t1 = GetTickCount();
  2172. int bfind = findPic(x1 - 10, y1 - 10, x2 + 10, y2 + 10, path, L"000000", 1.0, 0, retx, rety);
  2173. if (bfind == -1 && retx == -1)
  2174. {
  2175. DeleteFile(path);
  2176. return 1;
  2177. }
  2178. Sleep(10);
  2179. t2 = GetTickCount();
  2180. t3 = t2 - t1;
  2181. }
  2182. }
  2183. DeleteFile(path);
  2184. return 0;
  2185. }
  2186. void DXBind::setIsBind(bool isbind)
  2187. {
  2188. m_isBind = isbind;
  2189. }
  2190. bool DXBind::isBind()
  2191. {
  2192. return m_isBind;
  2193. }
  2194. bool DXBind::TSMatchPicName(wchar_t* pic_name, wchar_t* ret_name)
  2195. {
  2196. if (wcslen(pic_name) <= 0)
  2197. return false;
  2198. wchar_t picname[MAX_PATH][MAX_PATH];//存储多个字符串
  2199. for (int i = 0; i < MAX_PATH; i++)
  2200. {
  2201. memset(picname[i], 0, 16);
  2202. }
  2203. int col = 0;
  2204. short nstr = 0;//要查找文件类型个数 ,最多支持(MAX_PATH)个
  2205. bool bfindxing = false; //找到*
  2206. bool bfindwen = false; //找到?
  2207. while (*pic_name)
  2208. {
  2209. if (*pic_name != L'|')
  2210. {
  2211. if (*pic_name == '*')
  2212. bfindxing = true;
  2213. if (*pic_name == '?')
  2214. bfindwen = true;
  2215. picname[nstr][col++] = *pic_name;
  2216. pic_name++;
  2217. continue;
  2218. }
  2219. else
  2220. {
  2221. if (col < 1)
  2222. {
  2223. pic_name++;
  2224. continue;
  2225. }
  2226. nstr++;
  2227. col = 0;
  2228. pic_name++;
  2229. }
  2230. }
  2231. if (col >= 1)
  2232. nstr++;
  2233. col = 0;
  2234. if (wcslen(m_SetPath) > 0)
  2235. {
  2236. wchar_t szFind[MAX_PATH] = { 0 };
  2237. TCHAR szFile[MAX_PATH] = { 0 };
  2238. wcscpy(szFind, m_SetPath);
  2239. wcscat(szFind, L"\\*.bmp");
  2240. WIN32_FIND_DATA FindFileData;
  2241. HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
  2242. if (INVALID_HANDLE_VALUE == hFind) return false;
  2243. int retnamelen = 0;
  2244. while (TRUE)
  2245. {
  2246. if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
  2247. {
  2248. //FindFileData.cFileName
  2249. for (int i = col; i < nstr; i++)
  2250. {
  2251. int len = wcslen(FindFileData.cFileName);
  2252. wchar_t pic[MAX_PATH] = { 0 };
  2253. bool findxing = false; //找到*
  2254. bool findwen = false; //找到?
  2255. bool bcontinue = true;
  2256. bool bfind = false;
  2257. for (int j = 0; j < len; j++)
  2258. {
  2259. if (FindFileData.cFileName[j] == '.')
  2260. {
  2261. for (int n = 0; n < nstr; n++)
  2262. {
  2263. if (wcscmp(FindFileData.cFileName, picname[n]) == 0)
  2264. {
  2265. bfind = true;
  2266. break;
  2267. }
  2268. }
  2269. if (!bfind)
  2270. {
  2271. //bcontinue=false;
  2272. break;
  2273. }
  2274. if (retnamelen == 0)
  2275. retnamelen = wcslen(ret_name);
  2276. if (retnamelen == 0)
  2277. swprintf(ret_name, L"%s", FindFileData.cFileName);
  2278. else
  2279. swprintf(ret_name, L"%s|%s", ret_name, FindFileData.cFileName);
  2280. if (!bfindxing && !bfindwen)
  2281. col++;
  2282. bcontinue = false;
  2283. break;
  2284. }
  2285. if (picname[i][j] == '*')
  2286. findxing = true;
  2287. else if (picname[i][j] == '?')
  2288. findwen = true;
  2289. else if (picname[i][j] != '*' || picname[i][j] != '?')
  2290. pic[j] = picname[i][j];
  2291. if (findxing)//找到*
  2292. {
  2293. int cmp = wcsncmp(FindFileData.cFileName, pic, j);
  2294. if (cmp == 0)
  2295. {
  2296. if (retnamelen == 0)
  2297. retnamelen = wcslen(ret_name);
  2298. if (retnamelen == 0)
  2299. swprintf(ret_name, L"%s", FindFileData.cFileName);
  2300. else
  2301. swprintf(ret_name, L"%s|%s", ret_name, FindFileData.cFileName);
  2302. bcontinue = false;
  2303. break;
  2304. }
  2305. }
  2306. else if (findwen)//找到?
  2307. {
  2308. int filelen = wcslen(pic);
  2309. filelen = len - filelen;
  2310. int cmp = wcsncmp(FindFileData.cFileName, pic, j);
  2311. if (cmp == 0)
  2312. {
  2313. for (int k = 0; k < filelen; k++)
  2314. {
  2315. if (FindFileData.cFileName[j + k] == '.')
  2316. {
  2317. if (retnamelen == 0)
  2318. retnamelen = wcslen(ret_name);
  2319. if (retnamelen == 0)
  2320. swprintf(ret_name, L"%s", FindFileData.cFileName);
  2321. else
  2322. swprintf(ret_name, L"%s|%s", ret_name, FindFileData.cFileName);
  2323. bcontinue = false;
  2324. break;
  2325. }
  2326. if (picname[i][j + k] == '?')
  2327. {
  2328. pic[j + k] = FindFileData.cFileName[j + k];
  2329. continue;
  2330. }
  2331. else
  2332. break;
  2333. }
  2334. if (!bcontinue)
  2335. break;
  2336. }
  2337. }
  2338. }
  2339. if (!bcontinue)
  2340. {
  2341. break;
  2342. }
  2343. }
  2344. }
  2345. if (!FindNextFile(hFind, &FindFileData) || col == nstr) break;
  2346. }
  2347. FindClose(hFind);
  2348. }
  2349. }
  2350. void DXBind::InitGdiNormal()
  2351. {
  2352. //if((m_displayMode&DISPLAY_GDI)==DISPLAY_GDI||(m_displayMode&DISPLAY_NORMAL)==DISPLAY_NORMAL||(m_displayMode&DISPLAY_DX2)==DISPLAY_DX2||(m_displayMode&DISPLAY_GDI2)==DISPLAY_GDI2)
  2353. {
  2354. if (pWndBmpBuffer == NULL)
  2355. {
  2356. pWndBmpBuffer = new COLORREF * [2000]; /////二维数组 用来存储图像的颜色值
  2357. for (int i = 0; i < 2000; i++)
  2358. {
  2359. pWndBmpBuffer[i] = new COLORREF[2000];
  2360. }
  2361. }
  2362. if (pWndBmpStrxy == NULL)
  2363. {
  2364. pWndBmpStrxy = new short* [2000];
  2365. for (int i = 0; i < 2000; i++)
  2366. {
  2367. pWndBmpStrxy[i] = new short[2000];
  2368. memset(pWndBmpStrxy[i], 0, 2000);
  2369. for (int j = 0; j < 2000; j++)
  2370. pWndBmpStrxy[i][j] = 0;
  2371. }
  2372. }
  2373. if (pBuffer == NULL)
  2374. {
  2375. pBuffer = new BYTE[(2000 * 3 + 3) / 4 * 4 * 2000];
  2376. }
  2377. }
  2378. }
  2379. void DXBind::mysleep(DWORD delaytime)
  2380. {
  2381. char eventname[MAX_PATH] = { 0 };
  2382. sprintf(eventname, "MYSLEEPEVENT:%d", nPid);
  2383. HANDLE hEvent = ::CreateEventA(NULL, TRUE, FALSE, eventname);
  2384. DWORD dtime1 = ::GetTickCount();
  2385. DWORD dtime2 = dtime1;
  2386. DWORD result;
  2387. MSG msg;
  2388. while (1)
  2389. {
  2390. result = ::MsgWaitForMultipleObjects(1, &hEvent, FALSE, INFINITE, QS_ALLINPUT);
  2391. dtime1 = ::GetTickCount();
  2392. if ((dtime1 - dtime2) >= delaytime)
  2393. {
  2394. CloseHandle(hEvent);
  2395. break;
  2396. }
  2397. if (result == WAIT_OBJECT_0 + 1)
  2398. {
  2399. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  2400. {
  2401. TranslateMessage(&msg);
  2402. DispatchMessage(&msg);
  2403. }
  2404. }
  2405. }
  2406. }
  2407. bool DXBind::TSEnableRealMouse(LONG X, LONG Y)
  2408. {
  2409. bool bret = false;
  2410. double DouMovelen = 0.0;
  2411. ULONG Movelen = ::abs(X - LastMouseMoveX) * ::abs(X - LastMouseMoveX) + ::abs(Y - LastMouseMoveY) * ::abs(Y - LastMouseMoveY);//(x*x+y*y)后在开方求长度
  2412. DouMovelen = ::sqrt((double)Movelen);//开方
  2413. Movelen = (ULONG)DouMovelen;
  2414. short x = X, y = Y;
  2415. DWORD MouseMoveX = LastMouseMoveX;
  2416. DWORD MouseMoveY = LastMouseMoveY;
  2417. while (mousestep < Movelen)
  2418. {
  2419. if (X == LastMouseMoveX)
  2420. {
  2421. x = X;
  2422. if (Y > LastMouseMoveY)
  2423. {
  2424. y = MouseMoveY + mousestep;
  2425. MouseMoveY = y;
  2426. }
  2427. else
  2428. {
  2429. y = MouseMoveY - mousestep;
  2430. MouseMoveY = y;
  2431. }
  2432. }
  2433. else if (X > LastMouseMoveX)
  2434. {
  2435. if (Y > LastMouseMoveY)
  2436. {
  2437. x = (X - MouseMoveX) * mousestep / Movelen + MouseMoveX;
  2438. y = (Y - MouseMoveY) * mousestep / Movelen + MouseMoveY;
  2439. MouseMoveX = x;
  2440. MouseMoveY = y;
  2441. }
  2442. else
  2443. {
  2444. x = (X - MouseMoveX) * mousestep / Movelen + MouseMoveX;
  2445. y = MouseMoveY - (MouseMoveY - Y) * mousestep / Movelen;
  2446. MouseMoveX = x;
  2447. MouseMoveY = y;
  2448. }
  2449. }
  2450. else if (X < LastMouseMoveX)
  2451. {
  2452. if (Y > LastMouseMoveY)
  2453. {
  2454. x = MouseMoveX - (MouseMoveX - X) * mousestep / Movelen;
  2455. y = (Y - MouseMoveY) * mousestep / Movelen + MouseMoveY;
  2456. MouseMoveX = x;
  2457. MouseMoveY = y;
  2458. }
  2459. else
  2460. {
  2461. x = MouseMoveX - (MouseMoveX - X) * mousestep / Movelen;
  2462. y = MouseMoveY - (MouseMoveY - Y) * mousestep / Movelen;
  2463. MouseMoveX = x;
  2464. MouseMoveY = y;
  2465. }
  2466. }
  2467. if ((m_mouseMode & MOUSE_NORMAL) == MOUSE_NORMAL)
  2468. {
  2469. if (TSRuntime::SetSimModeType == 0)//普通模式
  2470. ::SetCursorPos(x, y);
  2471. }
  2472. else
  2473. {
  2474. if (m_mouseMode & MOUSE_WINDOW)
  2475. PostMessage(m_mousekeyhwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
  2476. else
  2477. {
  2478. SendMsg(TS_MOUSEMOVE, 0, MAKELPARAM(x, y));
  2479. //*nret=gDxObj.SendMsg(TS_MOUSEMOVEOVER);
  2480. }
  2481. }
  2482. Movelen -= mousestep;
  2483. Sleep(mousedelay);
  2484. }
  2485. x = X, y = Y;
  2486. if ((m_mouseMode & MOUSE_NORMAL) == MOUSE_NORMAL)
  2487. {
  2488. if (TSRuntime::SetSimModeType == 0)//普通模式
  2489. ::SetCursorPos(x, y);
  2490. }
  2491. else
  2492. {
  2493. if (m_mouseMode & MOUSE_WINDOW)
  2494. PostMessage(m_mousekeyhwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
  2495. else
  2496. {
  2497. SendMsg(TS_MOUSEMOVE, 0, MAKELPARAM(x, y));
  2498. }
  2499. }
  2500. return bret;
  2501. }
  2502. DWORD DXBind::TSRealKeyMousSleepTime(DWORD type)
  2503. {
  2504. srand(::GetTickCount());
  2505. DWORD sleeptime = 0;
  2506. if (type == 0)//鼠标
  2507. {
  2508. DWORD Moustart = MouseClicksleep / 2; //范围的最小值
  2509. DWORD Mouend = MouseClicksleep + Moustart;//范围的最大值
  2510. int randnum = rand();
  2511. if (randnum <= Moustart)
  2512. {
  2513. Moustart += (Moustart - randnum);
  2514. sleeptime = Moustart;
  2515. }
  2516. else
  2517. {
  2518. if (randnum / Mouend == 0)
  2519. sleeptime = Mouend - (Mouend - randnum);
  2520. else
  2521. {
  2522. sleeptime = Mouend - (randnum - Mouend * (randnum / Mouend));
  2523. if (sleeptime < Moustart)
  2524. sleeptime = sleeptime + Moustart;
  2525. }
  2526. }
  2527. }
  2528. else//键盘
  2529. {
  2530. DWORD Keytart = KeyDownsleep / 2; //范围的最小值
  2531. DWORD Keyend = KeyDownsleep + Keytart;//范围的最大值
  2532. int randnum = rand();
  2533. if (randnum <= Keytart)
  2534. {
  2535. Keytart += (Keytart - randnum);
  2536. sleeptime = Keytart;
  2537. }
  2538. else
  2539. {
  2540. if (randnum / Keyend == 0)
  2541. sleeptime = Keyend - (Keyend - randnum);
  2542. else
  2543. {
  2544. sleeptime = Keyend - (randnum - Keyend * (randnum / Keyend));
  2545. if (sleeptime < Keytart)
  2546. sleeptime = sleeptime + Keytart;
  2547. }
  2548. }
  2549. }
  2550. return sleeptime;
  2551. }
  2552. void DXBind::TSMoveToEx(ULONG x, ULONG y, ULONG w, ULONG h, wchar_t* retstring)
  2553. {
  2554. Sleep(5);
  2555. srand(::GetTickCount());
  2556. int randnum = rand();
  2557. DWORD movx = 0, movy = 0;
  2558. DWORD xw = w + x;
  2559. DWORD hy = h + y;
  2560. if (randnum <= x)
  2561. {
  2562. movx = x + (x - randnum);
  2563. if (movx > xw)
  2564. movx = x + (movx - xw);
  2565. }
  2566. else
  2567. {
  2568. if (randnum / xw == 0)
  2569. movx = randnum;
  2570. else
  2571. {
  2572. movx = x + (randnum - w * (randnum / w));
  2573. }
  2574. }
  2575. if (randnum <= y)
  2576. {
  2577. movy = y + (y - randnum);
  2578. if (movy > hy)
  2579. movy = y + (movy - hy);
  2580. }
  2581. else
  2582. {
  2583. if (randnum / h == 0)
  2584. movy = randnum;
  2585. else
  2586. movy = y + (randnum - h * (randnum / h));
  2587. }
  2588. swprintf(retstring, L"%d,%d", movx, movy);
  2589. }
  2590. //////////////////////////////////这里是注入游戏远程使用的API接口///////////////////////////////
  2591. //这里拦截API,并且开启检测线程
  2592. bool DXBind::hookApi()
  2593. {
  2594. //开启线程
  2595. //if(TSRuntime::MapViewThread==NULL)
  2596. TSRuntime::MapViewThread = (HANDLE)_beginthread(ReadShareMemoryThreadFunc, 0, (void*)true);//启动注入HOOK线程
  2597. //ReadShareMemoryThreadFunc((void*)true);//HookandUnhookAPI((void*)false);
  2598. return true;
  2599. }
  2600. //获取机器码
  2601. bool DXBind::TSGetMachineCode(wchar_t* retcode)
  2602. {
  2603. return true;
  2604. }