Skip to content

Commit

Permalink
添加opengl 截图
Browse files Browse the repository at this point in the history
  • Loading branch information
WallBreaker2 committed Jan 22, 2019
1 parent b3ea78c commit b769909
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 103 deletions.
6 changes: 2 additions & 4 deletions PythonTest/PythonTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
from win32com.client import Dispatch
op=Dispatch("op.opsoft");
print("op ver:",op.Ver());
hwnd=op.FindWindow("","Fire");
hwnd=op.FindWindow("","新建文本文档.txt - 记事本");
r=op.SetDict(0,"dm_soft.txt");
print("SetDict:",r);
r=0;
if hwnd:
r=op.BindWindow(hwnd,"opengl","normal","windows",0);
r=op.BindWindow(hwnd,"gdi","normal","windows",0);
if r:
print("bind ok.");
r=op.Sleep(1000);
print("try screencap");
r=op.capture("screen.bmp");
s = op.Ocr(0,0,106,50,"000000-0f0f0f",1.0);
print("ocr:",s);
r,x,y=op.FindColor(0,0,121,159,"000000-050505");
print(r,x,y);
if r:
Expand Down
Binary file added doc/struct.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions op.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "opPS", "opPS\opPS.vcxproj",
{EA90DCB4-3591-4FA3-9C10-08FEE47A4BFA} = {EA90DCB4-3591-4FA3-9C10-08FEE47A4BFA}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dll_test", "dll_test\dll_test.vcxproj", "{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ConsoleTest", "ConsoleTest\ConsoleTest.vcxproj", "{68E33E18-50FF-4418-AE8D-DD7D17E7A245}"
EndProject
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PythonTest", "PythonTest\PythonTest.pyproj", "{70EE008E-A047-4299-84AA-7C0079CFF4CE}"
Expand Down Expand Up @@ -44,16 +42,6 @@ Global
{E5FC54F2-4EE1-41E4-8E8B-95D2B22E246D}.Release|Any CPU.ActiveCfg = Release|Win32
{E5FC54F2-4EE1-41E4-8E8B-95D2B22E246D}.Release|x64.ActiveCfg = Release|x64
{E5FC54F2-4EE1-41E4-8E8B-95D2B22E246D}.Release|x86.ActiveCfg = Release|Win32
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|Any CPU.ActiveCfg = Debug|Win32
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x64.ActiveCfg = Debug|x64
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x64.Build.0 = Debug|x64
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x86.ActiveCfg = Debug|Win32
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x86.Build.0 = Debug|Win32
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|Any CPU.ActiveCfg = Release|Win32
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x64.ActiveCfg = Release|x64
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x64.Build.0 = Release|x64
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x86.ActiveCfg = Release|Win32
{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x86.Build.0 = Release|Win32
{68E33E18-50FF-4418-AE8D-DD7D17E7A245}.Debug|Any CPU.ActiveCfg = Debug|Win32
{68E33E18-50FF-4418-AE8D-DD7D17E7A245}.Debug|x64.ActiveCfg = Debug|x64
{68E33E18-50FF-4418-AE8D-DD7D17E7A245}.Debug|x64.Build.0 = Debug|x64
Expand Down
3 changes: 2 additions & 1 deletion op/3rd_party/kiero/kiero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <Windows.h>

#ifdef KIERO_USE_MINHOOK
# include <MinHook.h>
#include <MinHook.h>
#endif

// Uncomment a needed graphical library (you can include all)
Expand Down Expand Up @@ -708,6 +708,7 @@ void kiero::bind(uint16_t _index, void** _original, void* _function)
void kiero::unbind() {
if (g_renderType > 0)
{
MH_RemoveHook(MH_ALL_HOOKS);
kiero::shutdown();
//MH_DisableHook((void*)g_methodsTable[_index]);
//MH_RemoveHook((void*)g_methodsTable[_index]);
Expand Down
15 changes: 14 additions & 1 deletion op/Bkbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ class Bkbase
long get_widht();
long RectConvert(long&x1, long&y1, long&x2, long&y2);
long get_image_type() {
return _display == BACKTYPE::NORMAL || _display == BACKTYPE::GDI ? -1 : 0;
switch (_display)
{
case BACKTYPE::NORMAL:
return 0;
case BACKTYPE::GDI:
return -1;
case BACKTYPE::DX:
return 0;
case BACKTYPE::OPENGL:
return -1;
default:
return 0;
}

}
private:
HWND _hwnd;
Expand Down
2 changes: 1 addition & 1 deletion op/DX9hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ long opengl_screen_capture() {
g_shared_res_name,
boost::interprocess::read_write);
//Map the whole shared memory in this process

boost::interprocess::mapped_region region(shm, boost::interprocess::read_write);
auto *p = static_cast<char*>(region.get_address());

Expand All @@ -139,6 +138,7 @@ void __stdcall hkglEnd(void) {
opengl_screen_capture();
}
void hook_init(HWND hwnd) {
g_hwnd = hwnd;
sprintf(g_shared_res_name, SHARED_RES_NAME_FORMAT, hwnd);
sprintf(g_mutex_name, MUTEX_NAME_FORMAT, hwnd);
try {
Expand Down
57 changes: 27 additions & 30 deletions op/Injecter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BOOL Injecter::EnablePrivilege(BOOL enable)
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL))
return FALSE;

// 关闭令牌句柄
Expand All @@ -38,51 +38,47 @@ BOOL Injecter::EnablePrivilege(BOOL enable)
}


HMODULE Injecter::InjectDll(LPCTSTR commandLine, LPCTSTR dllPath/*, DWORD* pid, HANDLE* process*/)
long Injecter::InjectDll(DWORD pid, LPCTSTR dllPath,long& error_code)
{
TCHAR* commandLineCopy = new TCHAR[32768]; // CreateProcess可能修改这个
_tcscpy_s(commandLineCopy, 32768, commandLine);
int cdSize = _tcsrchr(commandLine, _T('\\')) - commandLine + 1;
TCHAR* cd = new TCHAR[cdSize];
_tcsnccpy_s(cd, cdSize, commandLine, cdSize - 1);
// 创建进程并暂停
STARTUPINFO startInfo = {};
PROCESS_INFORMATION processInfo = {};
if (!CreateProcess(NULL, commandLineCopy, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, cd, &startInfo, &processInfo))
{
delete commandLineCopy;
delete cd;
return 0;
}
delete commandLineCopy;
delete cd;


auto jhandle=::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
/**pid = processInfo.dwProcessId;
*process = processInfo.hProcess;*/

if (!jhandle) {
error_code = ::GetLastError();
return -1;
}
DWORD dllPathSize = ((DWORD)_tcslen(dllPath) + 1) * sizeof(TCHAR);

// 申请内存用来存放DLL路径
void* remoteMemory = VirtualAllocEx(processInfo.hProcess, NULL, dllPathSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
void* remoteMemory = VirtualAllocEx(jhandle, NULL, dllPathSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (remoteMemory == NULL)
{
//setlog(L"申请内存失败,错误代码:%u\n", GetLastError());
return 0;
error_code = ::GetLastError();
return -2;
}

// 写入DLL路径
if (!WriteProcessMemory(processInfo.hProcess, remoteMemory, dllPath, dllPathSize, NULL))
if (!WriteProcessMemory(jhandle, remoteMemory, dllPath, dllPathSize, NULL))
{
//setlog(L"写入内存失败,错误代码:%u\n", GetLastError());
return 0;
error_code = ::GetLastError();
return -3;
}

// 创建远线程调用LoadLibrary
HANDLE remoteThread = CreateRemoteThread(processInfo.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibrary, remoteMemory, 0, NULL);
auto lpfn=GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW");
if (!lpfn) {
error_code = ::GetLastError();
return -4;
}
HANDLE remoteThread = CreateRemoteThread(jhandle, NULL, 0, (LPTHREAD_START_ROUTINE)lpfn, remoteMemory, 0, NULL);
if (remoteThread == NULL)
{
//setlog(L"创建远线程失败,错误代码:%u\n", GetLastError());
return NULL;
error_code = ::GetLastError();
return -5;
}
// 等待远线程结束
WaitForSingleObject(remoteThread, INFINITE);
Expand All @@ -91,11 +87,12 @@ HMODULE Injecter::InjectDll(LPCTSTR commandLine, LPCTSTR dllPath/*, DWORD* pid,
GetExitCodeThread(remoteThread, &remoteModule);

// 恢复线程
ResumeThread(processInfo.hThread);
//ResumeThread(processInfo.hThread);

// 释放
CloseHandle(remoteThread);
VirtualFreeEx(processInfo.hProcess, remoteMemory, dllPathSize, MEM_DECOMMIT);

return (HMODULE)remoteModule;
VirtualFreeEx(jhandle, remoteMemory, dllPathSize, MEM_DECOMMIT);
CloseHandle(jhandle);
error_code = 0;
return 1;
}
4 changes: 2 additions & 2 deletions op/Injecter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Injecter
Injecter();
~Injecter();
static BOOL EnablePrivilege(BOOL enable);
// 程序运行时注入DLL,返回模块句柄(64位程序只能返回低32位)
static HMODULE InjectDll(LPCTSTR commandLine, LPCTSTR dllPath/*, DWORD* pid, HANDLE* process*/);
//
static long InjectDll(DWORD pid, LPCTSTR dllPath,long& error_code);
};

16 changes: 9 additions & 7 deletions op/OpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@

#include "Cmder.h"
#include "Injecter.h"
#include "Tool.h"
// OpInterface

HRESULT OpInterface::Ver(BSTR* ret) {
#ifndef _WIN64
static const wchar_t* ver = L"0.2110.x86";
const char* ver = "0.2110.x86";
#else
static const wchar_t* ver = L"0.2110.x64";

#endif;
CComBSTR bstr;
bstr.Append(ver);
bstr.CopyTo(ret);
Tool::setlog("address=%d,str=%s",ver,ver);
CComBSTR newstr;
newstr.Append(ver);
newstr.CopyTo(ret);

return S_OK;
}
Expand Down Expand Up @@ -56,9 +58,9 @@ STDMETHODIMP OpInterface::Sleep(LONG millseconds, LONG* ret) {
STDMETHODIMP OpInterface::InjectDll(BSTR process_name, BSTR dll_name, LONG* ret) {
//auto proc = _wsto_string(process_name);
//auto dll = _wsto_string(dll_name);
Injecter::EnablePrivilege(TRUE);
auto h = Injecter::InjectDll(process_name, dll_name);
*ret = (h ? 1 : 0);
//Injecter::EnablePrivilege(TRUE);
//auto h = Injecter::InjectDll(process_name, dll_name);
*ret = 0;
return S_OK;
}

Expand Down
46 changes: 4 additions & 42 deletions op/WinApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2596,49 +2596,11 @@ bool WinApi::ClientToScreen(LONG hwnd, LONG &x, LONG &y)
}
bool WinApi::FindWindow(wchar_t *class_name, wchar_t*title, LONG &rethwnd, DWORD parent)
{
bool bret = false;
rethwnd = 0;
HWND p = NULL;
if (parent == 0)
p = ::GetWindow(GetDesktopWindow(), GW_CHILD); //获取桌面窗口的子窗口
else
p = ::GetWindow((HWND)parent, GW_CHILD); //获取桌面窗口的子窗口
if (p == NULL)
return bret;
p = ::GetWindow(p, GW_HWNDFIRST);
while (p != NULL)
{
if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
{
if (wcslen(class_name) < 1 && wcslen(title) < 1)
{
rethwnd = (LONG)p;
bret = true;
break;
}
else
{
wchar_t WindowClassName[MAX_PATH] = { 0 };
::GetClassName(p, WindowClassName, MAX_PATH);
wchar_t WindowTitle[MAX_PATH] = { 0 };
::GetWindowText(p, WindowTitle, MAX_PATH);
if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
{
wchar_t *strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
wchar_t *strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
if ((wcslen(class_name) >= 1 && strfindclass) || (wcslen(title) >= 1 && strfindtitle))
{
rethwnd = (LONG)p;
bret = true;
break;
}
}
}
}
p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
}
if (class_name[0] == '\0')
class_name = nullptr;
rethwnd=(LONG)::FindWindowW(class_name, title);

return bret;
return 1;
}

bool WinApi::FindWindowByProcess(wchar_t *class_name, wchar_t *title, LONG &rethwnd, wchar_t *process_name, DWORD Pid)
Expand Down
15 changes: 12 additions & 3 deletions op/bkopengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "bkopengl.h"
#include "Tool.h"

#include "Injecter.h"
bkopengl::bkopengl()
{
_process_id = 0;
Expand Down Expand Up @@ -39,7 +39,16 @@ long bkopengl::Bind(HWND hwnd, long flag) {
auto _dllptr = _process.modules().GetModule(_dllname);
if (!_dllptr) {
Tool::setlog(L"inject..");
reg_ret = _process.modules().Inject(buff);
//reg_ret = _process.modules().Inject(buff);
/*long error_code = 0;
if (!Injecter::EnablePrivilege(true))
Tool::setlog("Injecter::EnablePrivilege False.");
reg_ret.status = Injecter::InjectDll(id, buff, error_code);
Tool::setlog("inject ret=%d,error_code=%d", reg_ret.status, error_code);
*/
Tool::setlog(buff);
auto& modules = _process.modules();
reg_ret = modules.Inject(buff);
Tool::setlog(L"inject finish...");
}
else {
Expand Down Expand Up @@ -120,7 +129,7 @@ long bkopengl::capture(const std::wstring& file_name) {

bih.biBitCount = 32;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = -_height;//高度
bih.biHeight = _height;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = _width * _height * 4;//图像数据大小
Expand Down
3 changes: 3 additions & 0 deletions op/op.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IgnoreImportLibrary>true</IgnoreImportLibrary>
<LinkIncremental>true</LinkIncremental>
<IncludePath>D:\minhook\include;$(IncludePath)</IncludePath>
<LibraryPath>D:\minhook\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IgnoreImportLibrary>true</IgnoreImportLibrary>
Expand Down Expand Up @@ -227,6 +229,7 @@
<ClCompile Include="3rd_party\kiero\kiero.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Bkbase.cpp" />
<ClCompile Include="bkdisplay.cpp" />
Expand Down

0 comments on commit b769909

Please sign in to comment.