Exception while user callback (MFC) - c++

I have a function with this header
void LiveviewDlg::OnSize(UINT nType, int cx, int cy)
In this function I calculate some coordinates for my controls. I use for this
CRect rect;
GetWindowRect(&rect);
to get some values from my MFC control. After the line
GetDlgItem(IDSTART)->MoveWindow(dialogWidth - 120, 0, 100, 50);
I get the exception
Unhandled exception at 0x5DBA4F14 (mfc120ud.dll) in Liveview.exe: 0xC000041D: Exception while user callback (translated from german to english)
On my Windows 7 system my program works fine. On my windows 10 system I get the exception. I don't know, why is this exception thrown.

Related

Getting Properties of a Running Program

I would like to develop a program which ID's a card as it is played in another running program such as a poker or hearts game or whatever. I'm starting by trying to get the information I need on that game program which is already running, and I'm having problems right from the start. I'm running MSVC++ 2013 and developing an MFC application. Right now I'm playing with the Hearts game and here's the code:
HWND hwnd = FindWindowA(NULL, "Hearts");
if (hwnd == NULL)
{ /* window not found*/
}
else
{ /* window was found */
RECT rect;
GetWindowRect(hwnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
}
So I get hwnd just fine and the code works depending on whether I have Hearts open or not. But the line GetWindowRect(hwnd, &rect); won't compile saying
"error C2660: 'CWnd::GetWindowRect' : function does not take 2 arguments".
There is a GetWindowRect function which only has the rect argument, but gets the properties of the program window I'm working on. There is lot's of documentation on GetWindowRect which shows the two arguments as above, but how do I invoke that subroutine?
As you are inside an MFC window class, you are calling the CWnd::GetWindowRect function - you want to call the one in the Win32 API, so:
::GetWindowRect(hwnd, &rect);
where the :: scope resolution operator (without a namespace or class name on the left-hand side) says to call the function in the global scope.

VS2017 MFC Assert: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxwin2.inl Line:84

next is my code (debug mode run error, release no problem):
"CControlPanel" class derived from CFormView
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
m_wndSplitterFrame.CreateStatic(this, 2, 1);
m_wndSplitterFrame.CreateView(0, 0, RUNTIME_CLASS(CControlPanel), CSize(0, 180), pContext); //assert error line
m_wndSplitterSchema.CreateStatic(&m_wndSplitterFrame, 1, 2, WS_CHILD | WS_VISIBLE, m_wndSplitterFrame.IdFromRowCol(1, 0));
m_wndSplitterSchema.CreateView(0, 0, RUNTIME_CLASS(CTabView), CSize(250, 200), pContext);
m_wndSplitterData.CreateStatic(&m_wndSplitterSchema, 1, 2, WS_CHILD | WS_VISIBLE, m_wndSplitterSchema.IdFromRowCol(0, 1));
m_wndSplitterData.CreateView(0, 0, RUNTIME_CLASS(CItemsListView), CSize(700, 0), pContext);
m_wndSplitterData.CreateView(0, 1, RUNTIME_CLASS(CDetailHtmlView), CSize(0, 0), pContext);
....
}
But I commented "OnSize" is no assert error!
void CControlPanel::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
#ifndef _DEBUG
RECT rect1;
m_CountList.GetWindowRect(&rect1);
ScreenToClient(&rect1);
....
m_CountList.MoveWindow(&rect1);
....
#endif
}
Search said that adding the following code to solve.
AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0);
But again this error: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\appinit.cpp
I think this approach is wrong, ask you how to solve this problem?
Looks like OnSize is called before m_CountList's windows is created or not yet attached to Windows window -- has no HWND handle. You get ASSERTs form the framework that you'll try to call API without having a handle. In release build the asserts are removed and that's why you don't get these errors.
The MFC CWnd derived classes have a corresponding Windows window object with HWND on which they call operate attached.
You can test whether your control/windows objects have attached window objects by checking their handle before calling method that call Windows API functions that need window handle.
void CControlPanel::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
if (m_CountList.GetSafeHwnd())
{
RECT rect1;
m_CountList.GetWindowRect(&rect1);
ScreenToClient(&rect1);
....
m_CountList.MoveWindow(&rect1);
}
....
}
See also ASSERT(::IsWindow(m_hWnd)) fails in Afxwin2.inl

Problems with sending and getting messages from one class to another class. MFC program

Here is my code:
This function is called when i click the left button of the mouse and send te message:
#define WM_MYMESSAGE WM_USER+7
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
counter=0;
CWnd::OnLButtonDown(nFlags, point);
CRect wdRect;
GetClientRect(&wdRect);
HWND hwnd;
hwnd=::FindWindow(NULL,"Client");
if(wdRect.PtInRect(point))
{
counter++;
PostMessage(WM_MYMESSAGE,point.x,point.y);
}
}
in another file Mainfraim.cpp with the help of ON_MESSAGE(WM_MYMESSAGE, OnNameMsg) i send message to ONNameMsg function.This function opens the bmp file. The problem is that the function OnNameMsg does not respond to the message and this function does not work. What should i do to make this function respond on this message. Can you help me with this problem?? Here is the code.
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_SETFOCUS()
ON_MESSAGE(WM_MYMESSAGE, OnNameMsg)
ON_COMMAND(ID_EDIT_LINE, OnEditLine)
END_MESSAGE_MAP()
afx_msg LRESULT CMainFrame::OnNameMsg(WPARAM wParam,LPARAM IParam)
{
MSG msg;
char FileName[500];
char FileTitle[100];
FileName[0]='\0';
GetMessage(&msg,NULL,WM_MOUSEFIRST,0);
CFileDialog file(TRUE);
file.m_ofn.lpstrFilter=TEXT("Bitmap picture files *.bmp\0*.bmp\0All Files *.*\0*.*\0\0");
file.m_ofn.lpstrFileTitle=FileTitle;
file.m_ofn.lpstrFile=FileName;
file.m_ofn.lpstrTitle="Open BMP File";
file.DoModal();
//if (FileName[0]=='\0')return;
SetWindowText(FileTitle);
HANDLE hdibCurrent1 = OpenDIB(FileName);
hbm=0;
hbm=BitmapFromDib(hdibCurrent1,0);
GetObject(hbm,sizeof(BITMAP),(LPSTR)&bm);
CRect wdRect;
GetClientRect(&wdRect);
ClientToScreen(&wdRect);
int j=wdRect.Height();
int i=wdRect.Width();
//SetWindowPos(NULL,wdRect.left,wdRect.top, i,j,NULL);
if(hbm) { CClientDC dc(this);
HDC hdc=::GetDC(m_hWnd);
HDC hdcBits=::CreateCompatibleDC(hdc);
SelectObject(hdcBits,hbm);
//CRect wdRect;
GetClientRect(&wdRect);
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
dc.FillRect(&wdRect,&brush);
BitBlt(hdc, 0, 0, bm.bmWidth,bm.bmHeight,hdcBits,0,0, SRCCOPY);
DeleteDC(hdcBits);
::ReleaseDC(m_hWnd,hdc);
}
return 1;
}
You are sending the message to CChildView, and not to the CMainFrame. Since you want to send message to main frame, and handling it there, you must send (PostMessage) to the window handle of main frame.
The PostMessage you are calling is method is from CWnd, which is not same as ::PostMessage API, and hence it is sending to this. You need a pointer of CMainFrame, and call through that pointer. Let's assume you get main frame's pointer into pMainFrame, and then you can call:
pMainFrame->PostMessage(WM_MYMESSAGE,point.x,point.y);

WTL RedrawWindow parameterrs

I'm new to the WTL C++. I'm really confused about the parameters that go into the RedrawWindows function especially for the flags. I'm just trying to update a window everytime I draw a line, but I don't exactly understand how
LRESULT CDrawView::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int xPos= GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
end.X = xPos;
end.Y = yPos;
Pen pen(Color(0, 0, 255));
m_GraphicsImage.DrawLine(&pen, start.X, start.Y, end.X, end.Y);
I try to call RedrawWindow here,
RedrawWIndow(NULL,NULL, NULL, RDW_INTERNALPAINT)
So everytime I release the left mouse button the window gets updated. I'm having a really hard time understanding the parameters that go into the Redraw Function. I tried putting them all null minus the last one but Visual studio says that the function doesn't take 4 parameters even though I read the msdn microsoft...
You are not calling the global RedrawWindow.
You're calling the member function CWindow::RedrawWindow, which takes 3 parameters.
BOOL RedrawWindow(
LPCRECT lpRectUpdate = NULL,
HRGN hRgnUpdate = NULL,
UINT flags = RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE
); throw()
Edit:
These three parameters all have default arguments, meaning they don't need to be supplied an RedrawWindow() alone should work.
This is not the way you should use and you are supposed to be using the API. Your mouse button handler should call Invalidate() or InvalidateRect with specific part of the window you are marking as needing an update. Your window will then receive WM_PAINT event at the first OS convenience and your paint handler would paint the line.
RedrawWindow might work out this time, however is likely to be a base of a next problem very soon because you are already on the wrong way.

Windows API dialogs without using resource files

I'm trying to create a dialog box using C++ and the windows API, but I don't want the dialog defined in a resource file. I can't find anything good on this on the web, and none of the examples I've read seem to define the dialog programmatically.
How can I do this?
A simple example is fine. I'm not doing anything complicated with it yet.
Raymond Chen wrote a few posts about the dialog manager:
The dialog manager, part 1: Warm-ups
The dialog manager, part 2: Creating the frame window
The dialog manager, part 3: Creating the controls
The dialog manager, part 4: The dialog loop
The dialog manager, part 5: Converting a non-modal dialog box to modal
The dialog manager, part 6: Subtleties in message loops
The dialog manager, part 7: More subtleties in message loops
The dialog manager, part 8: Custom navigation in dialog boxes
The dialog manager, part 9: Custom accelerators in dialog boxes
If all you want to do is show a window with controls, it's possible to create a window without using resource (.rc) files / scripts.
This isn't the same as a dialog, but it might be easier than creating a dialog programmatically.
First, a few notes about how this is done:
Instead of designing the dialog in the rc file, you could manually use CreateWindow (or CreateWindowEx) to create child windows of a main window. (for .NET Windows Forms programmers, these windows are like Controls).
This process will not be graphical at all (you will need to manually type in the location and size of each window), but I think this can be a great way to understand how dialogs are created under the hood.
There are some disadvantages to not using a real dialog, namely that tab will not work when switching between controls.
About the example:
This example features a dialog box with two buttons, an edit box (.NET Windows Forms programmers would think of it as a TextBox), and a check box.
It has been tested under the following conditions:
x86 build
x64 build
Unicode build (UNICODE and _UNICODE defined)
Non-Unicode build (UNICODE and _UNICODE not defined)
Built with Visual Studio's C compiler
Built with Visual Studio's C++ compiler
OS: Windows 10 64 bit
Note: UNICODE
As of the time of writing, UTF-8 is still in beta for Windows 10
If you have not enabled this setting, you should assume that any char* is ACP, not UTF-8, this applies to standard library functions too
Even though in Linux, that same standard library function would be UTF-8.
Sadly, some C++ standard library features only work with char* (e.g., exception messages).
You can still use UTF-8 in Windows without the option set, you will just have to encode it back to UTF-16 before calling winapi functions.
Here is a reddit thread with a reply from somebody who claims to have worked on UTF-8 on Windows, it has some good information.
UNICODE in Windows means "UTF-16", not "UTF-8".
Using Unicode of some kind is strongly recommended for any version of Windows that is not very old.
Be aware that if you don't use Unicode, your program may be utterly unable to open file names containing Unicode characters, handle directories (e.g., usernames) with non-ACP characters, etc.
Using ACP functions (SendMessageA,etc) without somehow verifying that UTF-8 is enabled (it's disabled by default) is probably a bug.
For max portability/flexibility, I would recommend using UTF-16 and the W version of all API functions, translating from UTF-8 to UTF-16 at the last minute. Read this page very carefully.
Now for the code:
Note that a large amount of comments have been added to try to document the windows functions, I recommend copy/pasting this into a text editor, for best results.
// This sample will work either with or without UNICODE, it looks like
// it's recommended now to use UNICODE for all new code, but I left
// the ANSI option in there just to get the absolute maximum amount
// of compatibility.
//
// Note that UNICODE and _UNICODE go together, unfortunately part
// of the Windows API uses _UNICODE, and part of it uses UNICODE.
//
// tchar.h, for example, makes heavy use of _UNICODE, and windows.h
// makes heavy use of UNICODE.
#define UNICODE
#define _UNICODE
//#undef UNICODE
//#undef _UNICODE
#include <windows.h>
#include <tchar.h>
// I made this struct to more conveniently store the
// positions / size of each window in the dialog
typedef struct SizeAndPos_s
{
int x, y, width, height;
} SizeAndPos_t;
// Typically these would be #defines, but there
// is no reason to not make them constants
const WORD ID_btnHELLO = 1;
const WORD ID_btnQUIT = 2;
const WORD ID_CheckBox = 3;
const WORD ID_txtEdit = 4;
const WORD ID_btnShow = 5;
// x, y, width, height
const SizeAndPos_t mainWindow = { 150, 150, 300, 300 };
const SizeAndPos_t btnHello = { 20, 50, 80, 25 };
const SizeAndPos_t btnQuit = { 120, 50, 80, 25 };
const SizeAndPos_t chkCheck = { 20, 90, 185, 35 };
const SizeAndPos_t txtEdit = { 20, 150, 150, 20 };
const SizeAndPos_t btnShow = { 180, 150, 80, 25 };
HWND txtEditHandle = NULL;
// hwnd: All window processes are passed the handle of the window
// that they belong to in hwnd.
// msg: Current message (e.g., WM_*) from the OS.
// wParam: First message parameter, note that these are more or less
// integers, but they are really just "data chunks" that
// you are expected to memcpy as raw data to float, etc.
// lParam: Second message parameter, same deal as above.
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
// Create the buttons
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Note that the "parent window" is the dialog itself. Since we are
// in the dialog's WndProc, the dialog's handle is passed into hwnd.
//
//CreateWindow( lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam
//CreateWindow( windowClassName, initial text, style (flags), xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
CreateWindow( TEXT("Button"), TEXT("Hello"), WS_VISIBLE | WS_CHILD, btnHello.x, btnHello.y, btnHello.width, btnHello.height, hwnd, (HMENU)ID_btnHELLO, NULL, NULL);
CreateWindow( TEXT("Button"), TEXT("Quit"), WS_VISIBLE | WS_CHILD, btnQuit.x, btnQuit.y, btnQuit.width, btnQuit.height, hwnd, (HMENU)ID_btnQUIT, NULL, NULL);
// Create a checkbox
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CreateWindow( TEXT("button"), TEXT("CheckBox"), WS_VISIBLE | WS_CHILD | BS_CHECKBOX, chkCheck.x, chkCheck.y, chkCheck.width, chkCheck.height, hwnd, (HMENU)ID_CheckBox, NULL, NULL);
// Create an edit box (single line text editing), and a button to show the text
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Handle = CreateWindow(windowClassName, windowName, style, xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
txtEditHandle = CreateWindow(TEXT("Edit"), TEXT("Initial Text"), WS_CHILD | WS_VISIBLE | WS_BORDER, txtEdit.x, txtEdit.y, txtEdit.width, txtEdit.height, hwnd, (HMENU)ID_txtEdit, NULL, NULL);
//CreateWindow( windowClassName, windowName, style, xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
CreateWindow( TEXT("Button"), TEXT("Show"), WS_VISIBLE | WS_CHILD, btnShow.x, btnShow.y, btnShow.width, btnShow.height, hwnd, (HMENU)ID_btnShow, NULL, NULL);
// Create an Updown control. Note that this control will allow you to type in non-number characters, but it will not affect the state of the control
break;
// For more information about WM_COMMAND, see
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647591(v=vs.85).aspx
case WM_COMMAND:
// The LOWORD of wParam identifies which control sent
// the WM_COMMAND message. The WM_COMMAND message is
// sent when the button has been clicked.
if (LOWORD(wParam) == ID_btnHELLO)
{
MessageBox(hwnd, TEXT("Hello!"), TEXT("Hello"), MB_OK);
}
else if (LOWORD(wParam) == ID_btnQUIT)
{
PostQuitMessage(0);
}
else if (LOWORD(wParam) == ID_CheckBox)
{
UINT checked = IsDlgButtonChecked(hwnd, ID_CheckBox);
if (checked)
{
CheckDlgButton(hwnd, ID_CheckBox, BST_UNCHECKED);
MessageBox(hwnd, TEXT("The checkbox has been unchecked."), TEXT("CheckBox Event"), MB_OK);
}
else
{
CheckDlgButton(hwnd, ID_CheckBox, BST_CHECKED);
MessageBox(hwnd, TEXT("The checkbox has been checked."), TEXT("CheckBox Event"), MB_OK);
}
}
else if (LOWORD(wParam) == ID_btnShow)
{
int textLength_WithNUL = GetWindowTextLength(txtEditHandle) + 1;
// WARNING: If you are compiling this for C, please remember to remove the (TCHAR*) cast.
TCHAR* textBoxText = (TCHAR*) malloc(sizeof(TCHAR) * textLength_WithNUL);
GetWindowText(txtEditHandle, textBoxText, textLength_WithNUL);
MessageBox(hwnd, textBoxText, TEXT("Here's what you typed"), MB_OK);
free(textBoxText);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// hInstance: This handle refers to the running executable
// hPrevInstance: Not used. See https://blogs.msdn.microsoft.com/oldnewthing/20040615-00/?p=38873
// lpCmdLine: Command line arguments.
// nCmdShow: a flag that says whether the main application window
// will be minimized, maximized, or shown normally.
//
// Note that it's necessary to use _tWinMain to make it
// so that command line arguments will work, both
// with and without UNICODE / _UNICODE defined.
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS mainWindowClass = { 0 };
// You can set the main window name to anything, but
// typically you should prefix custom window classes
// with something that makes it unique.
mainWindowClass.lpszClassName = TEXT("JRH.MainWindow");
mainWindowClass.hInstance = hInstance;
mainWindowClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
mainWindowClass.lpfnWndProc = WndProc;
mainWindowClass.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&mainWindowClass);
// Notes:
// - The classname identifies the TYPE of the window. Not a C type.
// This is a (TCHAR*) ID that Windows uses internally.
// - The window name is really just the window text, this is
// commonly used for captions, including the title
// bar of the window itself.
// - parentHandle is considered the "owner" of this
// window. MessageBoxes can use HWND_MESSAGE to
// free them of any window.
// - menuHandle: hMenu specifies the child-window identifier,
// an integer value used by a dialog box
// control to notify its parent about events.
// The application determines the child-window
// identifier; it must be unique for all
// child windows with the same parent window.
//CreateWindow( windowClassName, windowName, style, xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
CreateWindow( mainWindowClass.lpszClassName, TEXT("Main Window"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, mainWindow.x, mainWindow.y, mainWindow.width, mainWindow.height, NULL, 0, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
// This code is based roughly on tutorial code present at http://zetcode.com/gui/winapi/
Further reading
The builtin set of window classes are rather limited, so you might be curious as to how you can define your own window classes ("Controls") using the Windows API, see the articles below:
Custom Controls in Win32 API: The Basics (Code Project)
The WINE emulator source serves as a good example of how the Windows API could be implemented, and how you can make your own window classes that imitate the behavior of builtin classes.
Zetcode.com's tutorials
NOTE: I originally intended this post to cover the creation of dialogs programmatically. Due to a mistake on my part I didn't realize that you can't just "show" a window as a dialog. Unfortunately I wasn't able to get the setup mentioned by Raymond Chen working. Even looking at WINE's source, it's not super clear.
Take a look at this toolkit that describes how to create dialogs without resource files.
It's in WTL. However, I'm sure you can pick apart the internals to achieve the same thing using the Win32 API directly.
Here you can find how to use Windows API dialogs without using resource files.
The Windows API (only the C Win32 API, no MFC) tutorial:
Windows API tutorial
Try to search MSDN for "dialog templates in memory".
See this for example: Dialog Boxes