CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 35 of 35

Thread: Errors in code

  1. #31
    Join Date
    May 2014
    Posts
    205

    Re: Errors in code

    Edit - this part is Solved:
    3rd argument not solved but I see there can be same problem with 1st argument HBitmap. HBitmap is 0.
    The function call
    Code:
    PrintWindow(HCapture, device, PW_CLIENTONLY)
    does not use Hbitmap, it uses device. So I need to select the bitmap from DC first. So I will get the handle. Correct?

    The 3rd argument... i am lost... idk how to solve it

    I am translating from original AHK code which uses
    Code:
    obm := SelectObject(hdc, hbm) ; // selected the bitmap first
    PrintWindow(hwnd, hdc)  ; // GdipCreateBitmapFromHBITMAP call, they passed uint ... the reason for confusion
    pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
    Last edited by crazy boy; May 4th, 2014 at 03:10 AM.

  2. #32
    Join Date
    May 2014
    Posts
    205

    Re: Errors in code

    I am pasting actual code without headers:

    Code:
    /*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;
    //......................................................................................
    }
    ------ Rebuild All started: Project: capture3, Configuration: Debug Win32 ------
    stdafx.cpp
    capture.cpp
    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 ==========

    We passed over the basic errors; now it continued in compilation until unresolved externals. I need to recall how to fix this coz it's quite about 8 months when I solved this problems first time.

    Thank you for your help

    PS: Is here a way how to display formatted code?
    Last edited by crazy boy; May 4th, 2014 at 03:32 AM.

  3. #33
    Join Date
    May 2014
    Posts
    205

    Re: Errors in code

    Do you know where to get program called `nm'
    referenced here http://www.cplusplus.com/forum/general/113904/ ? Could be useful to solve unresolved externals

  4. #34
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Errors in code

    Quote Originally Posted by crazy boy View Post
    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'
    See http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

    Quote Originally Posted by crazy boy View Post
    capture.obj : error LNK2019: unresolved external symbol _GdipCreateBitmapFromHBITMAP@12 referenced in function _main
    capture3\Debug\capture3.exe : fatal error LNK1120: 1 unresolved externals
    Did you add the Gdiplus.lib?

    Quote Originally Posted by crazy boy View Post
    Is here a way how to display formatted code?
    Yes! use Code tags
    Victor Nijegorodov

  5. #35
    Join Date
    May 2014
    Posts
    205

    Re: Errors in code

    Quote Originally Posted by VictorN View Post
    Did you add the Gdiplus.lib?

    Yes! use Code tags
    I mean to highlight syntax in the code block.

    Well, thank you. I added it into project configuration into Linker/Input/Additional dependencies. It works now :-) Great relief.
    Last edited by crazy boy; May 4th, 2014 at 06:13 AM.

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured