-
January 7th, 2013, 01:41 PM
#1
Create window form C++ function using MFC or Win32 API
Hello,
My setup is as follows:
There is function written C++, that must be compiled to DLL. This DLL is linked to some CAD (computer aded design) tool, that has special interface for it.
I want to add to this function some GUI (graphical user interface). So the creation of a window is necessary.
I've tried already with Win32, but without success.
The problem is that CreateWindow function requires application instance handle, that is provided by Windows itself when window is created in "normal conditions".
Any ideas ?
Perhaps in the MFC there is some particular staff to do this ...
Regards.
Pavel.
-
January 7th, 2013, 02:21 PM
#2
Re: Create window form C++ function using MFC or Win32 API
Code:
HINSTANCE hInst = GetModuleHandle(NULL);
Best regards,
Igor
-
January 7th, 2013, 02:41 PM
#3
Re: Create window form C++ function using MFC or Win32 API
Thanks Igor,
I've tried already this. hInst isnt NULL and CreateWindow also return non-NULL handler, but when I try to show window
BOOL b_ShowWindowRSLT = ShowWindow(hWnd, SW_SHOWDEFAULT );where hWnd - handler, returned by CreateWindow, it fails - i.e. b_ShowWindowRSLT is FALSE.
I tried also dll filename instead of NULL. The same result...
Regards.
Pavel.
-
January 7th, 2013, 03:13 PM
#4
Re: Create window form C++ function using MFC or Win32 API
Best regards,
Igor
-
January 7th, 2013, 03:18 PM
#5
Re: Create window form C++ function using MFC or Win32 API
The last error - ShowWindow fails.
-
January 7th, 2013, 03:24 PM
#6
Re: Create window form C++ function using MFC or Win32 API
Just pay a little bit more attention to the documentation.
If ShowWindow function returns zero (FALSE), means "the window was previously hidden", not "function failed".
Besides, hWnd is a handle (with no final 'r') and not a handler.
-
January 7th, 2013, 03:55 PM
#7
Re: Create window form C++ function using MFC or Win32 API
Ok, you are right. Thanks Ovidiu.
Here is a bit of code, that doesn't work:
HWND hWnd = CreateWindow(szAppName, L"title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(L"vpi_win32.dll"), 0 );
vpi_printf("Window handle: %d\n", hWnd);
if(hWnd != NULL)
{
BOOL b_ShowWindowRSLT = ShowWindow(hWnd, SW_SHOWDEFAULT);
while(GetMessage(&msg, 0, 0, 0))
DispatchMessage(&msg);
vpi_printf("pass 3\n");
}
Here is CAD console output (vpi_printf):

So, as you can constate, the execution is blocked inside of GetMessage function.
Regards,
Pavel
-
January 7th, 2013, 04:11 PM
#8
Re: Create window form C++ function using MFC or Win32 API
 Originally Posted by Pavel_47
The last error - ShowWindow fails.
Windows classics. The last error is what GetLastError returns. So next time when you're asked about last error please give no meaningless answers.
Best regards,
Igor
-
January 7th, 2013, 04:30 PM
#9
Re: Create window form C++ function using MFC or Win32 API
So next time when you're asked about last error please give no meaningless answers
Ok, will keep in mind
-
January 7th, 2013, 04:38 PM
#10
Re: Create window form C++ function using MFC or Win32 API
 Originally Posted by Pavel_47
Code:
HWND hWnd = CreateWindow(szAppName, L"title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(L"vpi_win32.dll"), 0 );
vpi_printf("Window handle: %d\n", hWnd);
if(hWnd != NULL)
{
BOOL b_ShowWindowRSLT = ShowWindow(hWnd, SW_SHOWDEFAULT);
while(GetMessage(&msg, 0, 0, 0))
DispatchMessage(&msg);
vpi_printf("pass 3\n");
}
A few notes on the code:- szAppName - this must be the name of a registered window class. Is it?
- WS_OVERLAPPEDWINDOW - did you try to explicitly specify WS_VISIBLE. Why all that mess with ShowWindow?
- SW_SHOWDEFAULT - did you try SW_SHOWNORMAL or just SW_SHOW?
- GetMessage - this will always block, this is the main intention of using GetMessage.
If you expected some other behavior, you need some other type of message pump here.
BTW, while(GetMessage(&msg, 0, 0, 0)) is a bad style. GetMessage may return -1 indicating something bad happened, but you're always gonna be locked in looping. As Ovidiu already said, try to pay some more attention to reading MSDN docs.
Best regards,
Igor
-
January 7th, 2013, 05:07 PM
#11
Re: Create window form C++ function using MFC or Win32 API
Thanks Igor,
I followed your proposition and finally get my window !
Here is code:
Code:
PLI_INT32 vpi_CreateWindow(PLI_BYTE8 *user_data)
{
WNDCLASSEX WindowClass; // Structure to hold our window's attributes
static LPCTSTR szAppName = L"OFWin"; // Define window class name
MSG msg; // Windows message structure
WindowClass.cbSize = sizeof(WNDCLASSEX); // Set structure size
// Redraw the window if the size changes
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
// Define the message handling function
WindowClass.lpfnWndProc = WindowProcedure;
WindowClass.cbClsExtra = 0; // No extra bytes after the window class
WindowClass.cbWndExtra = 0; // structure or the window instance
WindowClass.hInstance = GetModuleHandle(L"vpi_win32.dll"); // Application instance handle
vpi_printf("Instance handle: %d\n", WindowClass.hInstance);
// Set default application icon
WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
// Set window cursor to be the standard arrow
WindowClass.hCursor = LoadCursor(0, IDC_ARROW);
// Set gray brush for background color
WindowClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
WindowClass.lpszMenuName = 0; // No menu
WindowClass.lpszClassName = szAppName; // Set class name
WindowClass.hIconSm = 0; // Default small icon
RegisterClassEx(&WindowClass);
HWND hWnd = CreateWindow(szAppName, L"title", WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(L"vpi_win32.dll"), 0 );
vpi_printf("Window handle: %d\n", hWnd);
if(hWnd != NULL)
{
BOOL b_ShowWindowRSLT = ShowWindow(hWnd, SW_SHOW);
//while(GetMessage(&msg, 0, 0, 0))
// DispatchMessage(&msg);
//vpi_printf("pass 3\n");
}
return(0);
}
But to achieve this I commented GetMessage loop. What can be alternative for it ? Unfortunately I'm not programmer, and all this staff I learned/copied from www on-th-fly to get worked my principal project, which is in Verilog.
Regards.
Pavel.
-
January 7th, 2013, 05:26 PM
#12
Re: Create window form C++ function using MFC or Win32 API
I think that my function doesn't need GetMessage - the parent CAD window does this job: I could move the window around the screen with GetMessage commented, which proves my guess. Isn't it ?
Last edited by Pavel_47; January 7th, 2013 at 05:28 PM.
-
January 7th, 2013, 05:47 PM
#13
Re: Create window form C++ function using MFC or Win32 API
I've just tried to do the same using MFC, as creating/processing all of controls with Win32 is complicated. Here is a code that doesn't work. Doesn't work means - nothing happens - non window appears:
Code:
class COurApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class COurWnd:public CFrameWnd
{
public:
COurWnd()
{
Create(0, L"MFC Application");
}
};
BOOL COurApp::InitInstance(void)
{
m_pMainWnd = new COurWnd;
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}
PLI_INT32 vpi_CreateWindow(PLI_BYTE8 *user_data)
{
COurApp app;
BOOL aaa = app.InitInstance();
return 0;
}
FYI the CAD tools calls vpi_CreateWindow function.
Any ideas ?
Apart from Win32 and MFC theoretically there is 3rd possibility - WindowsForms. But keeping in mind that this last concept uses CLR, that is unlikely supported by CAD, I won't try this approach.
-
January 7th, 2013, 08:07 PM
#14
Re: Create window form C++ function using MFC or Win32 API
 Originally Posted by Pavel_47
I've just tried to do the same using MFC, as creating/processing all of controls with Win32 is complicated.
Why don't you take a working example, get it compile, run it, and understand why that example works?
Learning how to write a Windows API program shouldn't be done by trying to cobble up code yourself. You should be taking a working example already written by an experienced person and understand the different parts of it -- the creation of the window class, the message loop, the window procedure, etc. Writing a Windows program in a proper way cannot be done (I have yet to see it done) without first getting a working example and learning from the example programs.
There are thousands of these examples all over the Internet. Or get a good book, such as the ones written by Charles Petzold.
For example, the GetMessage() loop is documented in again, thousands of places, and more pointedly, MSDN as looking like this:
Code:
BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Does your loop look like this? Look at all of the pieces missing from your example compared to a documented example from MSDN.
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 7th, 2013 at 08:18 PM.
-
January 8th, 2013, 01:15 AM
#15
Re: Create window form C++ function using MFC or Win32 API
 Originally Posted by Pavel_47
I've just tried to do the same using MFC, as creating/processing all of controls with Win32 is complicated. Here is a code that doesn't work. Doesn't work means - nothing happens - non window appears:
[...]
Code:
PLI_INT32 vpi_CreateWindow(PLI_BYTE8 *user_data)
{
COurApp app;
BOOL aaa = app.InitInstance();
return 0;
}
Just to mention a little C++ basics detail: app object is locally defined in vpi_CreateWindow function. When it goes out of scope (the function returns), that object is destroyed. So, even something would happen, it happens for a very, very short time.
 Originally Posted by Pavel_47
Apart from Win32 and MFC theoretically there is 3rd possibility - WindowsForms. But keeping in mind that this last concept uses CLR, that is unlikely supported by CAD, I won't try this approach.
Sure. To avoid even more painful headaches, in your particular case, keep away of .NET stuff!
Last edited by ovidiucucu; January 8th, 2013 at 01:18 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|