Atl CDialogImpl not showing on DoModal if called from static library - windows

I wrote a super simple ATL dialog inside a project. Even when I noted that every example on the web implemented the CDialogImpl class inline (that is, the class definition and it's implmentation where in the definition itself) I wrote it normally, separating my definition in a .h file and implementation in a .cpp file. This class is summarized below:
CMainDialog.hpp
class CMainDialog: public CDialogImpl<CMainDialog>
{
public:
enum { IDD = IDD_MYDIALOGS_DIALOG};
BEGIN_MSG_MAP(CMainDialog)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
END_MSG_MAP()
CMainDialog();
~CMainDialog();
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam,
BOOL& bHandled);
LRESULT OnCancel(UINT uMsg, WORD wID, HWND hWndCtl, BOOL& bHandled);
}
CMainDialog.cpp
CMainDialog::CMainDialog()
{
}
CMainDialog::~CMainDialog()
{
}
LRESULT CMainDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam,
BOOL& bHandled)
{
}
LRESULT CMainDialog::OnCancel(UINT uMsg, WORD wID, HWND hWndCtl,
BOOL& bHandled)
{
}
If I call this class from the same project, everything goes fine. The dialog shows. Example:
Calling DoModal
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
CMainDialog dialog;
dialog.DoModal();
return 0;
}
So I converted this project from .exe to static library and removed _tWinMain.
So, if I create a new ATL project, reference my newly created library and call CMainDialog.DoModal ... well, nothing happens. The constructor does get called, but the messages never start dispatching and the program ends inmediatly. Maybe I'm missing something?
I'm totally new to Win32 programming (although definitely not new to c++) so any help would be appreciated.

A static library does not have resources associated with it. Most likely the dialog code is trying to load the dialog template from the program resources but can't find it.

Related

How can I get a message box to appear every time I left-click in a window?

I want to open a message box with the word "left" when I left click the mouse.
So I used wndproc and MK_LBUTTON, but the wndproc function wrote the code, but the WinMain part doesn't know how to write the code.
I don't want to open a window, but when I searched on Google, I only have a code example that only shows a window.
(Opening the window did not solve the problem ..)
What should I do? Help
(If you've written as much as possible but don't understand the question, please ask me and I'll answer it.
And I'm not good at English, so I wrote a translator.)
my code(try)
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
, LPSTR lpszCmdParam, int nCmdShow)
{
//???
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
switch (iMessage) {
case MK_LBUTTON:
MessageBox(hWnd, TEXT("left"),TEXT("message"), MB_OK);
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
MK_LBUTTON is not a message, you need to catch WM_LBUTTONDOWN, WM_LBUTTONUP or WM_LBUTTONDBLCLK.
These messages are only sent to the active window where the mouse is clicked. If you want to catch clicks on all windows then you need to use a mouse hook and pump messages.

How to add WINAPI code to MFC?

I had to combine MFC and WinAPI: add WINAPI code to MFC,
the following are MFC and WinAPI code:
MFC code
void MyMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
......
}
WinAPI code
LRESULT CALLBACK Win32Fun(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
......
}
Can I do like this:
void MyMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
Win32Fun(hwnd, msg, wParam, lParam);
}
Yes. There's no magic involved. The fact that the base class of your class is taken from the MFC library doesn't change the fact that it's C++. The WINAPI is C code, and C++ can call C.

How to use DialogBox in Windows API

I'm learning windows api for some weeks, now I got a problem, my code is like this
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case(...)
DialogBox(hInstance,MAKEINTRESOURCE(IDD_MYDIALOG),hwnd,(DLGPROC)MyDialogProc);
return 0;
}
bool MyDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch (message){
...
}
return false;
}
I don't know what else should I do before I use DialogBox where should I place EndDialog(). IDD_MYDIALOG is a resource file created by myself. I don't understand what is hInstance and how to get it, I think I just need a simple example, than I can know how use DialogBox. Thank you for helping!

Zombie Process after using a Windows subclass callback

I'm using the SetWindowSubclass(...) method of the Windows API to kind of "hook" messages transmitted to a WinProc that is out of the scope of my application.
My application is made of a core and plugins in DLL.
I've implemented such a Subclass in one of my plugins DLL.
I set the Subclass like this:
class MyPlugin
{
private:
static HWND s_OgrehWnd;
UINT_PTR m_uIdSubclass; //The ID of the Subclass WinProc
DWORD_PTR m_pdwRefData;
//....
public:
void MyPlugin::init();
LRESULT CALLBACK MyPlugin::windowProcSubclass(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
void MyPlugin::shutdown();
//....
};
//Set the subclass
void MyPlugin::init()
{
//...
bool resultsc = SetWindowSubclass(
s_hWnd,
MultitouchPlugin::windowProcSubclass,
m_uIdSubclass,
m_pdwRefData
);
//...
}
//The subclass
LRESULT CALLBACK MyPlugin::windowProcSubclass(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch(message)
{
case WM_GESTURE:
return MultitouchPlugin::g_cGestureEngine.WndProc(hWnd,wParam,lParam);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//Remove the subclass
void MyPlugin::shutdown()
{
//shutdown called - unregister stuff here
bool isSublcassed = GetWindowSubclass(s_hWnd, MultitouchPlugin::windowProcSubclass, m_uIdSubclass, &m_pdwRefData);
if(isSublcassed)
{
RemoveWindowSubclass(s_OgrehWnd, MultitouchPlugin::windowProcSubclass, m_uIdSubclass);
}
}
My problem is that when I quit the application, I can see the process that keeps running on in the 'Windows Task Manager' tool.
In debug mode, i've checked that it goes calls RemoveWindowSubclass(), and it does it.
If I remove my plugin with this code, there is no zombie process....
Does somebody has an idea about a solution to this problem?
Thanx in advance for the help

Error in windows api program: cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,LPARAM,WPARAM) to WNDPROC

i've made a simple windows api program having the functions WinMain() and WinProc() , but i'm getting this error :
error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,LPARAM,WPARAM)' to 'WNDPROC'
1> This conversion requires a reinterpret_cast, a C-style cast or function-style cast
#include<windows.h>
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, LPARAM lParam, WPARAM wParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WindowClass;
static LPCTSTR szAppName = L"OFWin";
HWND hWnd;
MSG msg;
WindowClass.cbSize = sizeof(WNDCLASSEX);
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = WindowProc; // error
....
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, LPARAM lParam, WPARAM wParam)
{ ..... }
the program's taken word to word from my book (ivor horton's beginning visual c++ 2010), what's wrong?
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, LPARAM lParam, WPARAM wParam);
Here's your problem: the LPARAM and WPARAM are backwards, it should be:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
WPARAM and LPARAM have two different types (UINT_PTR and INT_PTR respectively - for historical reasons mostly), so you get a type related error if you accidentally swap them around. Which is a lucky thing in your case: if they were the same type, then instead of getting a compiler error, the code would compile fine, and you'd instead spend some time wondering why your wndproc was apparently getting mixed up parameters passed to it!

Resources