以下のコードでは、主にGetEncoderClsid()
にあります。プログラムがデバッガモードまたはデバッガなしで実行されている場合は、私は未知数Exception
を取得しています。解決方法GetEncoderClsid()関数で発生した例外を解決するにはどうすればよいですか?
int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
:
これは、VSデバッガ上especifiedされるラインのですか?
ありがとうございます。
#include "stdafx.h"
#include <Windows.h>
#include <psapi.h>
#include <iostream>
#include <gdiplus.h>
#pragma comment (lib, "gdiplus.lib")
using namespace Gdiplus;
using namespace std;
#define MAX_PROCESSES 1024
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1;
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1;
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if(wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
DWORD FindProcess(__in_z LPCTSTR lpcszFileName)
{
LPDWORD lpdwProcessIds;
LPTSTR lpszBaseName;
HANDLE hProcess;
DWORD i, cdwProcesses, dwProcessId = 0;
lpdwProcessIds = (LPDWORD)HeapAlloc(GetProcessHeap(), 0, MAX_PROCESSES*sizeof(DWORD));
if (lpdwProcessIds != NULL)
{
if (EnumProcesses(lpdwProcessIds, MAX_PROCESSES*sizeof(DWORD), &cdwProcesses))
{
lpszBaseName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(TCHAR));
if (lpszBaseName != NULL)
{
cdwProcesses /= sizeof(DWORD);
for (i = 0; i < cdwProcesses; i++)
{
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, lpdwProcessIds[i]);
if (hProcess != NULL)
{
if (GetModuleBaseName(hProcess, NULL, lpszBaseName, MAX_PATH) > 0)
{
if (!lstrcmpi(lpszBaseName, lpcszFileName))
{
dwProcessId = lpdwProcessIds[i];
CloseHandle(hProcess);
break;
}
}
CloseHandle(hProcess);
}
}
HeapFree(GetProcessHeap(), 0, (LPVOID)lpszBaseName);
}
}
HeapFree(GetProcessHeap(), 0, (LPVOID)lpdwProcessIds);
}
return dwProcessId;
}
HWND GetProcessWindow(DWORD processId)
{
bool bFound = false;
HWND prevWindow = 0;
while (! bFound) {
HWND desktopWindow = GetDesktopWindow();
if (! desktopWindow)
break;
HWND nextWindow = FindWindowEx(desktopWindow, prevWindow, NULL, NULL);
if (! nextWindow)
break;
DWORD procId = -1;
GetWindowThreadProcessId(nextWindow, &procId);
if (procId == processId) {
wchar_t windowText[ 1024 ];
if (IsWindowVisible(nextWindow) && ! IsIconic(nextWindow) && GetWindowText(nextWindow, (LPWSTR)windowText, sizeof(windowText)/sizeof(wchar_t))
&& ! GetParent(nextWindow))
return nextWindow;
break;
}
prevWindow = nextWindow;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
RECT rc;
HWND hwnd = GetProcessWindow(FindProcess(L"chrome.exe"));
if (hwnd == NULL)
{
cout << "it can't find any 'Google Chrome' window" << endl;
system("pause");
return 0;
}
GetClientRect(hwnd, &rc);
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);
PrintWindow(hwnd, hdc, PW_CLIENTONLY);
Bitmap *image = new Bitmap(hbmp, NULL);
CLSID myClsId;
int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
image->Save(L"output.bmp", &myClsId, NULL);
delete image;
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL, hdcScreen);
cout << "success captured!" << endl;
system("pause");
return 0;
}
[私のためにコンパイル](http://rextester.com/WYHLA17688)。エラーメッセージの正確で完全なテキストをすべての行とともに表示できますか?通常は、コンパイラが考慮したさまざまなオーバーロードを指します。 –
@IgorTandetnikは、実行時に、行の 'Exception'を生成します:146、これは私の質問で上記のエラーが原因で発生したと思います。それとも? –
コンパイラエラーとコンパイルされたプログラムは、2つの非常に異なるものです。どちらですか? –