help remove errors in program

Can you help me remove these errors? I try to start to build program, one of my first tries, to capture content of window... I took this script which originally should capture screen and try to remake it.

h
1
2
3
4
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h> 


update
cpp: pls see the actual code and reports bellow
Last edited on
Looks like it doesn't know what CDC is on line 6 in capture.cpp. Is some #include missing maybe?
CDC Is unknown type. It is missing. I think. It should be type of Device Concept. Do I need to include some GDI library or this is standard?
Last edited on
EDIT

I had forgotten there must me main function. I removed the code for this one, looks better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// capture.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <Windows.h>

using namespace std;

int main() {
HWND m_hWnd = FindWindow(NULL, _T("Map Viewer") ); 
cout << m_hWnd << " << " << endl;
getch();

// get the device context of the screen
HDC hScreenDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);     

// and a device context to put it in
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

int x = GetDeviceCaps(hScreenDC, HORZRES);
int y = GetDeviceCaps(hScreenDC, VERTRES);

// maybe worth checking these are positive values
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);

// get a new bitmap
HBITMAP hOldBitmap = SelectObject(hMemoryDC, hBitmap);

BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = SelectObject(hMemoryDC, hOldBitmap);

// clean up
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
/*
HWND WINAPI FindWindow(
  _In_opt_  LPCTSTR lpClassName,
  _In_opt_  LPCTSTR lpWindowName
);
*/
}
Last edited on
EDIT
It still generates errors:

------ Rebuild All started: Project: capture2, Configuration: Debug Win32 ------
stdafx.cpp
capture.cpp
s:\temp\c++\algoritmy\capture2\capture\capture.cpp(13): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
p:\programy\programování\c++\visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
s:\temp\c++\algoritmy\capture2\capture\capture.cpp(28): error C2440: 'initializing' : cannot convert from 'HGDIOBJ' to 'HBITMAP'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
s:\temp\c++\algoritmy\capture2\capture\capture.cpp(30): error C2065: 'width' : undeclared identifier
s:\temp\c++\algoritmy\capture2\capture\capture.cpp(30): error C2065: 'height' : undeclared identifier
s:\temp\c++\algoritmy\capture2\capture\capture.cpp(31): error C2440: '=' : cannot convert from 'HGDIOBJ' to 'HBITMAP'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Generating Code...
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Last edited on
I did some changes so I can get hande of Window now, but the rest is mystic. Anybody can help?
I have finally found good script which after correction works. I remake it now to capture from window, but here I have a question. I try to use GdipCreateBitmapFromHBITMAP()

 
HBitmap = GdipCreateBitmapFromHBITMAP(HBitmap, 0, pBitmap);
but result is error:

error C3861: 'GdipCreateBitmapFromHBITMAP': identifier not found
I even try to include
#include <gdiplus.h> But still the same. How to fix?
Last edited on
I also get 'GdiPlus' : is not a class or namespace name
if I try to access the function through GdiPlus::, if I try define namespace, it occurs same error
Can you help to solve unresolved external problem in program using GDI+ Flat API?

stdafx.h:
1
2
3
4
5
6
7
8
9
10
11
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <fstream>
#include <stdio.h>

#include <wingdi.h>
#include <gdiplus.h>
#include <ostream> 


capture3/capture/capture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*Window capture example*/
#include "stdafx.h"
using namespace Gdiplus;
using namespace Gdiplus::DllExports;
// using namespace GdiPlus; 

#define SCREENWIDTH GetSystemMetrics(SM_CXSCREEN)
#define SCREENHEIGHT GetSystemMetrics(SM_CYSCREEN)

// HBITMAP g_hDeskBmp;
HDC g_hMemDC;
int g_nDCdata;

int main()
{
 // get desktop window handle (but can be handle of any window)
 HWND HCapture = FindWindow(NULL, _T("Map Viewer") );
//	HWND HCapture = GetDesktopWindow();
 if(!IsWindow(HCapture)) return 1;

/*
 BOOL PrintWindow(
  HWND hwnd, // A handle to the window that will be copied.
  HDC hdcBlt, // A handle to the device context.
  UINT nFlags // The drawing options: PW_CLIENTONLY
                        //     Only the client area of the window is copied to hdcBlt. By default, the entire window is copied.
);
*/

 // get window dimensions
 RECT rect;
 GetWindowRect(HCapture, &rect);

 size_t dx = rect.right - rect.left;
 size_t dy = rect.bottom - rect.top;

 // create BITMAPINFO structure
 // used by CreateDIBSection
 BITMAPINFO info;
 info.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
 info.bmiHeader.biWidth         = dx;
 info.bmiHeader.biHeight        = dy;
 info.bmiHeader.biPlanes        = 1;
 info.bmiHeader.biBitCount      = 24;
 info.bmiHeader.biCompression   = BI_RGB;
 info.bmiHeader.biSizeImage     = 0;
 info.bmiHeader.biXPelsPerMeter = 0;
 info.bmiHeader.biYPelsPerMeter = 0;
 info.bmiHeader.biClrUsed       = 0;
 info.bmiHeader.biClrImportant  = 0;

 // a bitmap handle and a pointer its bit data
 HBITMAP HBitmap = 0;
 BYTE*   memory = 0;

 /** We should create Bitmap first and then Device Context,
     however when I want to create snapshot of window, I need to use
	 fnc PrintWindow to copy the visual window to Device Context.
	 So I need to create DC first. The DC will be compatible with
	 current screen.
 */
 
 // 1. FIRST we need to Create DC for PrintWindow function
 // HDC HDevice = GetDC(HCapture);
 HDC HDevice = CreateCompatibleDC(NULL);
 
 // 2. SECOND we need to CREATE BITMAP (Device Independent Bitmap)
 // bitmap = CreateDIBSection(HDevice, &info, DIB_RGB_COLORS, (void**)&memory, 0, 0);
 // THIS WAS WRONG DECLARATION: unsigned int * pBitmap;
 GpBitmap *pBitmap = NULL; // type Gdiplus::GpBitmap

  if (!PrintWindow(HCapture, HDevice, PW_CLIENTONLY)) return 2;
  
  SelectObject (HDevice, pBitmap); // hdc, hbm is GpBitmap is hgdiobj

  DllExports::GdipCreateBitmapFromHBITMAP(HBitmap, 0, &pBitmap);
 ReleaseDC(HCapture, HDevice);
 if(!HBitmap || !memory) return 1;

 // blit the contents of the desktop (winDC)
 // to the bitmap (selected in memDC)
 HDC winDC = GetWindowDC(HCapture);
 HDC memDC = CreateCompatibleDC(winDC);
 SelectObject(memDC, HBitmap);
 BitBlt(memDC, 0, 0, dx, dy, winDC, 0, 0, SRCCOPY);
 DeleteDC(memDC);
 ReleaseDC(HCapture, winDC);

 /** THIS IS WRONG! VARIABLE CANNOT POINT TO NOWHERE!*/
 // char *buffer; // First set the type and range and then make pointer:
  char *buffer = new char[50]; // RIGHT DECLARATION
 sprintf(buffer,"capture%d%d.bmp",dx,dy);
 // create bitmap file
 std::basic_ofstream <char> file(buffer, std::ios::binary);
 if(!file) { DeleteObject(HBitmap); return 1; }

 // initialize bitmap file headers
 BITMAPFILEHEADER fileHeader;
 BITMAPINFOHEADER infoHeader;

 fileHeader.bfType      = 0x4d42;
 fileHeader.bfSize      = 0;
 fileHeader.bfReserved1 = 0;
 fileHeader.bfReserved2 = 0;
 fileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

 infoHeader.biSize          = sizeof(infoHeader);
 infoHeader.biWidth         = dx;
 infoHeader.biHeight        = dy;
 infoHeader.biPlanes        = 1;
 infoHeader.biBitCount      = 24;
 infoHeader.biCompression   = BI_RGB;
 infoHeader.biSizeImage     = 0;
 infoHeader.biXPelsPerMeter = 0;
 infoHeader.biYPelsPerMeter = 0;
 infoHeader.biClrUsed       = 0;
 infoHeader.biClrImportant  = 0;

 // save file headers
 file.write((char*)&fileHeader, sizeof(fileHeader));
 file.write((char*)&infoHeader, sizeof(infoHeader));

 // save 24-bit bitmap data
 int wbytes = (((24*dx + 31) & (~31))/8);
 int tbytes = (((24*dx + 31) & (~31))/8)*dy;
 file.write((char*)memory, tbytes);
 // delete bitmap
 DeleteObject(HBitmap);
 HBitmap = 0;
 memory = 0;
return 0;
}



Using Visual C++ Express 2010, getting this error:
------ Rebuild All started: Project: capture3, Configuration: Debug Win32 ------
stdafx.cpp
capture.cpp
\capture3\capture\capture.cpp(92): warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
\visual studio 10.0\vc\include\stdio.h(371) : see declaration of 'sprintf'
Generating Code...
capture.obj : error LNK2019: unresolved external symbol _GdipCreateBitmapFromHBITMAP@12 referenced in function _main
\capture3\Debug\capture3.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Topic archived. No new replies allowed.