Skip to content

Commit

Permalink
Fix Memory crash in DCBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin Jae-yeon committed Oct 21, 2016
1 parent 44c88d7 commit 1f51fe7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
31 changes: 14 additions & 17 deletions DaramCam/DaramCam.Bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ unsigned DCBitmap::GetStride () { return stride; }

unsigned DCBitmap::GetByteArraySize ()
{
return width * height * 3;
return ( ( ( stride + 3 ) / 4 ) * 4 ) * height;
}

IWICBitmap * DCBitmap::ToWICBitmap ( IWICImagingFactory * factory )
Expand All @@ -34,21 +34,30 @@ IWICBitmap * DCBitmap::ToWICBitmap ( IWICImagingFactory * factory )

unsigned arrLen = GetByteArraySize ();
WICInProcPointer ptr;
unsigned wicStride = 0;
bitmapLock->GetDataPointer ( &arrLen, &ptr );
memcpy ( ptr, byteArray, arrLen );
bitmapLock->GetStride ( &wicStride );
//memcpy ( ptr, byteArray, arrLen );
for ( unsigned i = 0; i < height; ++i )
{
int pos1 = i * wicStride;
int pos2 = i * stride;
memcpy ( ptr + ( pos1 ), byteArray + ( pos2 ), stride );
}
bitmapLock->Release ();

return bitmap;
}

void DCBitmap::Resize ( unsigned _width, unsigned _height )
{
if ( _width == 0 && _height == 0 ) return;
if ( _width == width && _height == _height ) return;
if ( byteArray ) delete [] byteArray;
if ( _width == 0 && _height == 0 ) { byteArray = nullptr; return; }

width = _width; height = _height;
stride = ( _width * 24 + 7 ) / 8;
byteArray = new unsigned char [ stride * height ];
byteArray = new unsigned char [ GetByteArraySize () ];
}

COLORREF DCBitmap::GetColorRef ( unsigned x, unsigned y )
Expand All @@ -65,18 +74,6 @@ void DCBitmap::SetColorRef ( COLORREF colorRef, unsigned x, unsigned y )
byteArray [ basePos + 2 ] = GetBValue ( colorRef );
}

void GetImageDataFromHBITMAP ( HDC hdc, HBITMAP bitmap, DCBitmap * cb )
{
BITMAPINFO bmpInfo;
memset ( &bmpInfo, 0, sizeof ( BITMAPINFOHEADER ) );
bmpInfo.bmiHeader.biSize = sizeof ( BITMAPINFOHEADER );
GetDIBits ( hdc, bitmap, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS );
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;
for ( unsigned i = 0; i < cb->GetHeight (); ++i )
GetDIBits ( hdc, bitmap, i, 1, ( cb->GetByteArray () + ( ( cb->GetHeight () - 1 - i ) * cb->GetStride () ) ), &bmpInfo, DIB_RGB_COLORS );
}

void DCBitmap::CopyFrom ( HDC hDC, HBITMAP hBitmap )
{
BITMAPINFO bmpInfo = { 0, };
Expand All @@ -87,7 +84,7 @@ void DCBitmap::CopyFrom ( HDC hDC, HBITMAP hBitmap )
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;
for ( unsigned i = 0; i < height; ++i )
GetDIBits ( hDC, hBitmap, i, 1, ( GetByteArray () + ( ( GetHeight () - 1 - i ) * GetStride () ) ), &bmpInfo, DIB_RGB_COLORS );
GetDIBits ( hDC, hBitmap, i, 1, ( byteArray + ( ( height - 1 - i ) * stride ) ), &bmpInfo, DIB_RGB_COLORS );
}

void DCBitmap::CopyFrom ( IDXGISurface * dxgiSurface )
Expand Down
4 changes: 3 additions & 1 deletion DaramCam/DaramCam.GDIScreenCapturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
DCGDIScreenCapturer::DCGDIScreenCapturer ( HWND hWnd )
: capturedBitmap ( 0, 0 ), hWnd ( hWnd )
{
desktopDC = /*hWnd == NULL ? */GetDC ( hWnd );// : GetWindowDC ( hWnd );
desktopDC = GetDC ( hWnd );
captureDC = CreateCompatibleDC ( desktopDC );
SetRegion ( nullptr );
}
Expand All @@ -24,10 +24,12 @@ void DCGDIScreenCapturer::Capture ()
BitBlt ( captureDC, 0, 0, capturedBitmap.GetWidth (), capturedBitmap.GetHeight (), desktopDC, 0, 0, SRCCOPY );
}
else
{
BitBlt ( captureDC,
0, 0,
captureRegion->right - captureRegion->left, captureRegion->bottom - captureRegion->top,
desktopDC, captureRegion->left, captureRegion->top, SRCCOPY );
}
SelectObject ( captureDC, oldBitmap );

capturedBitmap.CopyFrom ( captureDC, captureBitmap );
Expand Down
6 changes: 5 additions & 1 deletion DaramCam/DaramCam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ DARAMCAM_EXPORTS HWND DCGetActiveWindowFromProcess ( DWORD pId )
DWORD pId;
ENUMWINDOWPARAM * param = ( ENUMWINDOWPARAM* ) lParam;
GetWindowThreadProcessId ( hWnd, &pId );
if ( param->process != pId || ( GetWindow ( hWnd, GW_OWNER ) == 0 && IsWindowVisible ( hWnd ) ) )
if ( param->process != pId || !( GetWindow ( hWnd, GW_OWNER ) == 0 && IsWindowVisible ( hWnd ) ) || GetWindowLong(hWnd, WS_CHILD ) )
return 1;

RECT cr;
Expand All @@ -75,10 +75,14 @@ DARAMCAM_EXPORTS HWND DCGetActiveWindowFromProcess ( DWORD pId )
return 0;
}, ( LPARAM ) &hasChild );

if ( !param->returnHWND )
param->returnHWND = hWnd;

if ( hasChild )
return 1;

param->returnHWND = hWnd;

return 0;
}, ( LPARAM ) &lParam );
return lParam.returnHWND;
Expand Down

0 comments on commit 1f51fe7

Please sign in to comment.