#include "gdutil.h"
#include "gdimscreenshot.h"
#include "RunCurrentUserProcess.h"
#include "zlibex/zlibex.h"
#include "gdcharconv.h"
#include "Markup.h"
#include "gdlog.h"
#include "gdbase64.h"
#include <shlobj.h>
#include <gdiplus.h>
#include <shlwapi.h>
#include <path_utils.h>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
static BOOL g_bInited = FALSE;
static ULONG_PTR g_ulGdiToken = 0;
static HANDLE g_hStartShotEvent = NULL;
static HANDLE g_hShotOverEvent = NULL;
static HANDLE g_hUserScreenMutex = NULL;
extern HMODULE g_thisModule;
static HANDLE _CreateEveryoneEvent(BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName)
{
DWORD dwAclLength;
PSID psidEveryone = NULL;
PACL pDACL = NULL;
BOOL bResult = FALSE;
SECURITY_ATTRIBUTES SecAttr;
SECURITY_DESCRIPTOR SecDesc;
PACCESS_ALLOWED_ACE pACE = NULL;
SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY ;
SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
if (InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
{
if (AllocateAndInitializeSid(&siaWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &psidEveryone))
{
dwAclLength = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + GetLengthSid(psidEveryone);
pDACL = (PACL)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwAclLength);
if (pDACL != NULL)
{
if (InitializeAcl(pDACL, dwAclLength, ACL_REVISION))
{
if (AddAccessAllowedAce(pDACL, ACL_REVISION, GENERIC_ALL, psidEveryone))
{
if (SetSecurityDescriptorDacl(&SecDesc, TRUE, pDACL, FALSE))
{
SecAttr.nLength = sizeof(SecAttr);
SecAttr.bInheritHandle = FALSE;
SecAttr.lpSecurityDescriptor = &SecDesc;
return CreateEvent(&SecAttr, bManualReset, bInitialState, lpName);
}
}
}
HeapFree(GetProcessHeap(), 0, pDACL);
}
FreeSid(psidEveryone);
}
}
return NULL;
}
static ULARGE_INTEGER _GetStreamSize(IStream *pStream)
{
ULARGE_INTEGER ulcb = {0, 0};
ULARGE_INTEGER ulBegin, ulEnd;
LARGE_INTEGER lMov = {0, 0};
pStream->Seek(lMov, STREAM_SEEK_END, &ulEnd);
pStream->Seek(lMov, STREAM_SEEK_SET, &ulBegin);
ulcb.QuadPart = ulEnd.QuadPart - ulBegin.QuadPart;
return ulcb;
}
static int _GetEncoderClsid(LPCWSTR format, CLSID *pClsid)
{
int nRet = -1;
UINT num = 0, size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
if (Ok == GetImageEncodersSize(&num, &size) && size > 0)
{
ImageCodecInfo* pImageCodecInfo = (ImageCodecInfo*)malloc(size);
if (pImageCodecInfo != NULL)
{
if (Ok == GetImageEncoders(num, size, pImageCodecInfo))
{
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
nRet = j;
break;
}
}
}
free(pImageCodecInfo);
}
}
return nRet;
}
static BOOL _SaveDesktopJpgFileStream(IStream **ppStream, LONG quality)
{
BOOL bRet = GenericError;
HDC hdc = GetWindowDC(NULL);
if(hdc != NULL)
{
int x = GetDeviceCaps(hdc, HORZRES);
int y = GetDeviceCaps(hdc, VERTRES);
HBITMAP hbmp = CreateCompatibleBitmap(hdc, x, y);
HDC hmemdc = CreateCompatibleDC(hdc);
if (hbmp != NULL && hmemdc != NULL)
{
HBITMAP hbmpHold = (HBITMAP)SelectObject(hmemdc, hbmp);
BitBlt(hmemdc, 0, 0, x, y, hdc, 0, 0, SRCCOPY);
CLSID clsidJpeg;
EncoderParameters encoderParameters;
Bitmap *pBitmap = Bitmap::FromHBITMAP(hbmp, (HPALETTE)GetStockObject(DEFAULT_PALETTE));
if(pBitmap != NULL)
{
_GetEncoderClsid(L"image/jpeg", &clsidJpeg);
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
encoderParameters.Parameter[0].Value = &quality;
*ppStream = NULL;
CreateStreamOnHGlobal(NULL, TRUE, ppStream);
if(*ppStream != NULL)
{
if (Ok == pBitmap->Save(*ppStream, &clsidJpeg, &encoderParameters))
{
bRet = TRUE;
}
}
delete pBitmap;
}
SelectObject(hmemdc, hbmpHold);
DeleteDC(hmemdc);
DeleteObject(hbmp);
}
ReleaseDC(NULL, hdc);
}
return bRet;
}
static BOOL _SaveScreenShotFile()
{
BOOL bSucc = FALSE;
IStream* pStream = NULL;
HGLOBAL hGlobal = NULL;
TCHAR szFilePath[MAX_PATH] = {0};
GetWindowsDirectory(szFilePath, RTL_NUMBER_OF(szFilePath));
PathAppend(szFilePath, TEXT("GSSoft\\cis75\\gdimcache"));
SHCreateDirectory(NULL, szFilePath);
PathAppend(szFilePath, TEXT("imscreen.jpg"));
if (_SaveDesktopJpgFileStream(&pStream, 50))
{
DWORD dwStreamSize = _GetStreamSize(pStream).LowPart;
if (S_OK == GetHGlobalFromStream(pStream, &hGlobal))
{
LPCSTR lpBuffer = (LPCSTR)GlobalLock(hGlobal);
if (lpBuffer != NULL)
{
HANDLE hFile = CreateFile(
szFilePath,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL,
CREATE_ALWAYS,
0,
NULL
);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwWritten = 0;
if (WriteFile(hFile, lpBuffer, dwStreamSize, &dwWritten, NULL))
{
bSucc = TRUE;
}
CloseHandle(hFile);
}
GlobalUnlock(hGlobal);
}
}
pStream->Release();
}
return bSucc;
}
VOID _ClearScreenShotFile()
{
TCHAR szFilePath[MAX_PATH] = {0};
GetWindowsDirectory(szFilePath, RTL_NUMBER_OF(szFilePath));
PathAppend(szFilePath, TEXT("GSSoft\\cis75\\gdimcache\\imscreen.jpg"));
DeleteFile(szFilePath);
}
static VOID _ImUserScreenWork()
{
DWORD dwLastCount = 0;
while (TRUE)
{
MSG msg;
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
WaitForSingleObject(g_hStartShotEvent, INFINITE);
DWORD dwTickCount = GetTickCount();
if (dwTickCount - dwLastCount > 1000)
{
if (!_SaveScreenShotFile())
{
GdWriteLog(
LL_ERROR,
TEXT("gdimuserscreen"),
TEXT("SaveScreenShotFile失败,错误码%lu"),
GetLastError()
);
_ClearScreenShotFile();
}
else
{
dwLastCount = GetTickCount();
}
}
SetEvent(g_hShotOverEvent);
}
}
static BOOL _IsUserScreenRunning()
{
BOOL bExist = FAL
windows API 屏幕截屏
5星 · 超过95%的资源 185 浏览量
2016-02-26
13:47:08
上传
评论 3
收藏 8KB RAR 举报
多异点
- 粉丝: 0
- 资源: 10
最新资源
- C语言期末/课程设计:通讯录管理系统(Dev-C++)
- 5G网优案例:开启 BWP2 节能开关后导致 VONR MOS 低案例.docx
- 验文档+程序 基于matlab实现的该LSSVM模型采用PSR(相空间重建)算法选择输入的最佳嵌入维数
- Sprongboot3.1中文文档
- 数字电路多功能数字钟multisim仿真电路设计源码文件+课设设计文档.zip
- hal-spi-master
- 5G网优案例:核心网问题引起XN切换耗时较长导致VONR通话MOS打分低处理案例.docx
- 基于matlab实现的将粒子群算法应用于机械手的参数辨识,源码经亲测完整 效果良好.rar
- 基于matlab实现的利用3层DBN提取嵌入在原始图像数据中的特征
- 基于matlab实现的利用PSO粒子群搜索算法,辨识PID参数-Kp,Ki,Kd
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈