Skip to content

Commit 795743f

Browse files
committed
Fixed warnings found when building with Visual Studio 2017
1 parent 24b0a74 commit 795743f

File tree

2 files changed

+42
-49
lines changed

2 files changed

+42
-49
lines changed

src/Win32Utils.cpp

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ namespace Win32Utils
283283

284284
PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
285285
{
286-
BITMAP bmp;
286+
BITMAP bmp = {0};
287287
PBITMAPINFO pbmi;
288288
WORD cClrBits;
289289

@@ -348,7 +348,7 @@ namespace Win32Utils
348348
}
349349

350350
//https://stackoverflow.com/questions/24720451/save-hbitmap-to-bmp-file-using-only-win32
351-
void CreateBmpFile(const char * pszFile, HBITMAP hBMP)
351+
bool CreateBmpFile(const char * pszFile, HBITMAP hBMP)
352352
{
353353
HANDLE hf; // file handle
354354
BITMAPFILEHEADER hdr; // bitmap file-header
@@ -368,13 +368,12 @@ namespace Win32Utils
368368

369369
pbih = (PBITMAPINFOHEADER) pbi;
370370
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
371-
372-
assert(lpBits) ;
371+
if (!lpBits)
372+
return false;
373373

374374
// Retrieve the color table (RGBQUAD array) and the bits
375375
// (array of palette indices) from the DIB.
376-
assert(GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,
377-
DIB_RGB_COLORS));
376+
GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS);
378377

379378
// Create the .BMP file.
380379
hf = ::CreateFileA(pszFile,
@@ -383,41 +382,40 @@ namespace Win32Utils
383382
NULL,
384383
CREATE_ALWAYS,
385384
FILE_ATTRIBUTE_NORMAL,
386-
(HANDLE) NULL);
387-
assert(hf != INVALID_HANDLE_VALUE) ;
385+
(HANDLE) NULL);
386+
if (hf == INVALID_HANDLE_VALUE)
387+
{
388+
GlobalFree((HGLOBAL)lpBits);
389+
return false;
390+
}
388391

389392
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
390393
// Compute the size of the entire file.
391-
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
392-
pbih->biSize + pbih->biClrUsed
393-
* sizeof(RGBQUAD) + pbih->biSizeImage);
394+
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
394395
hdr.bfReserved1 = 0;
395396
hdr.bfReserved2 = 0;
396397

397398
// Compute the offset to the array of color indices.
398-
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
399-
pbih->biSize + pbih->biClrUsed
400-
* sizeof (RGBQUAD);
399+
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
401400

402401
// Copy the BITMAPFILEHEADER into the .BMP file.
403-
assert(WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
404-
(LPDWORD) &dwTmp, NULL));
402+
WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL);
405403

406404
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
407-
assert(WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
408-
+ pbih->biClrUsed * sizeof (RGBQUAD),
409-
(LPDWORD) &dwTmp, ( NULL)));
405+
WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, (NULL));
410406

411407
// Copy the array of color indices into the .BMP file.
412408
dwTotal = cb = pbih->biSizeImage;
413409
hp = lpBits;
414-
assert(WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL));
410+
WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL);
415411

416-
// Close the .BMP file.
417-
assert(CloseHandle(hf));
412+
// Close the .BMP file.
413+
CloseHandle(hf);
418414

419415
// Free memory.
420416
GlobalFree((HGLOBAL)lpBits);
417+
418+
return true;
421419
}
422420

423421
BOOL IsFullyTransparent(HBITMAP hBitmap)
@@ -457,10 +455,10 @@ namespace Win32Utils
457455

458456
std::string GetMenuItemDetails(HMENU hMenu, UINT pos)
459457
{
460-
MENUITEMINFOA info = {0};
461-
info.cbSize = sizeof(MENUITEMINFOA);
458+
MENUITEMINFOW info = {0};
459+
info.cbSize = sizeof(MENUITEMINFOW);
462460
info.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_STRING | MIIM_SUBMENU;
463-
BOOL wInfoSuccess = GetMenuItemInfoA(hMenu, pos, TRUE, &info);
461+
BOOL wInfoSuccess = GetMenuItemInfoW(hMenu, pos, TRUE, &info);
464462
if (!wInfoSuccess)
465463
return "";
466464

@@ -472,39 +470,34 @@ namespace Win32Utils
472470

473471
//compute display name
474472
static const int BUFFER_SIZE = 1024;
475-
char title[BUFFER_SIZE] = {0};
473+
char title_utf8[BUFFER_SIZE] = {0};
476474
char tmp[BUFFER_SIZE] = {0};
475+
wchar_t tmpW[BUFFER_SIZE] = {0};
477476
if (IsSeparator)
478477
{
479-
strcpy(title, "------------------------");
478+
strcpy(title_utf8, "------------------------");
480479
}
481480
//try with ansi text
482-
else if (GetMenuStringA(hMenu, id, tmp, BUFFER_SIZE, 0))
483-
{
484-
sprintf(title, "%s", tmp, pos, id);
485-
}
486-
else if (GetMenuStringW(hMenu, id, (WCHAR*)tmp, BUFFER_SIZE/2, 0))
481+
else if (GetMenuStringW(hMenu, id, tmpW, BUFFER_SIZE, 0))
487482
{
488-
//Can't log unicode characters, convert to ansi.
489-
//Assume some characters might get dropped
490-
std::wstring wtext = (WCHAR*)tmp;
491-
std::string atext = ra::unicode::UnicodeToAnsi(wtext);
492-
sprintf(title, "%s", atext.c_str(), pos, id);
483+
//Can't log unicode characters, convert to utf-8.
484+
std::string atext = ra::unicode::UnicodeToUtf8(tmpW);
485+
sprintf(title_utf8, "%s", atext.c_str());
493486
}
494487

495488
//build full menu description string
496-
std::string description;
497-
description.append(title);
498-
description.append(" (");
489+
std::string description_utf8;
490+
description_utf8.append(title_utf8);
491+
description_utf8.append(" (");
499492
sprintf(tmp, "pos=%lu, id=%lu", pos, id);
500-
description.append(tmp);
493+
description_utf8.append(tmp);
501494
if (isChecked)
502-
description.append(", checked");
495+
description_utf8.append(", checked");
503496
if (isDisabled && !IsSeparator)
504-
description.append(", disabled");
505-
description.append(")");
497+
description_utf8.append(", disabled");
498+
description_utf8.append(")");
506499

507-
return description;
500+
return description_utf8;
508501
}
509502

510503
std::string GetMenuTree(HMENU hMenu, int indent)
@@ -520,10 +513,10 @@ namespace Win32Utils
520513
//Detect if this menu is a parent menu
521514
HMENU hSubMenu = NULL;
522515
{
523-
MENUITEMINFOA info = {0};
524-
info.cbSize = sizeof(MENUITEMINFOA);
516+
MENUITEMINFOW info = {0};
517+
info.cbSize = sizeof(MENUITEMINFOW);
525518
info.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_STRING | MIIM_SUBMENU;
526-
BOOL wInfoSuccess = GetMenuItemInfoA(hMenu, i, TRUE, &info);
519+
BOOL wInfoSuccess = GetMenuItemInfoW(hMenu, i, TRUE, &info);
527520
if (wInfoSuccess)
528521
{
529522
if (info.hSubMenu)

src/Win32Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Win32Utils
3737
BOOL FillTransparentPixels(HBITMAP hBitmap, COLORREF background_color);
3838
HBITMAP CopyAsBitmap(HICON hIcon, const int bitmap_width, const int bitmap_height);
3939
HBITMAP CopyAsBitmap(HICON hIcon);
40-
void CreateBmpFile(const char * path, HBITMAP hBitmap);
40+
bool CreateBmpFile(const char * path, HBITMAP hBitmap);
4141
BOOL IsFullyTransparent(HBITMAP hBitmap);
4242
BOOL IsFullyTransparent(const std::string & buffer);
4343
std::string GetMenuItemDetails(HMENU hMenu, UINT pos);

0 commit comments

Comments
 (0)