CefInitialize won't work from a mfc dll - mfc

I want to use Chromium Embeded Framework in an mfc dll. Therefore I created a mfc test exe and tested a minimalistic implementation of cef (which worked). My next step was to do exactly the same thing with a test mfc dll. The code is almost the same but the cef browser window won't come up.
The application hangs in CefInitialize() and won't return. After debugging, I found that it gets stuck in a WaitForSingleObject/WaitForMultipleObject windows api.
BOOL CMyBrowserDllApp::InitInstance()
{
HINSTANCE hInstance = GetModuleHandle(NULL);
CefMainArgs main_args(hInstance);
CefSettings settings;
settings.no_sandbox = true;
settings.multi_threaded_message_loop = true;
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, cefApplication.get(), NULL);
if (exit_code >= 0)
return exit_code;
if(!CefInitialize(main_args, settings, cefApplication.get(), NULL))
{
OutputDebugStringA("Error: CefInitialize failed");
}else
OutputDebugStringA("Info: CefInitialize succeeded");
CWinApp::InitInstance();
return TRUE;
}
Same implementation works if used in a mfc exe directly.

GetModuleHandle returns the base for the exe not the dll. You might want to stash the dll base from:
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *reserved)

Related

C++ Windows application menus stop responding

I'm an electrical engineer and am creating my first C++ Windows 10 application in Visual Studio.
When I start the app that I've created, and my application window starts with its menus, I can open a file and start processing that file. This processing takes several minutes and
can't be interrupted for too long (less than a millisecond - hopefully).
The problem I'm having is that when the file processing is underway my window's menus don't respond. This is tolerable when I'm running under the Visual Studio IDE, but when I run the standalone app's .exe file then the app's graphics soon stops rendering and Windows posts a "Not responding" error message.
Can someone please point me in the right direction to solve these related problems. There must be a simple way to temporarily give control back to WinMain or window_callback.
Here's a simplified structure of the code:
LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, ...) {
switch (uMsg) {
case WM_CREATE:
AddMenus(hwnd);
break;
case MY_FILE_OPEN:
open_file(hwnd);
break;
...
}
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, ...) {
// Create Window Class
...
// Register Class
...
// Create Window
HWND window = CreateWindow(window_class.lpszClassName, L"My Window NAME", ...);
hdc = GetDC(window);
// My application's initialization code
...
while (running) {
MSG message;
while (PeekMessage(&message, window, ...) {
TranslateMessage(&message);
DispatchMessage(&message);
}
}
}
void open_file(HWND hwnd) {
here's where I call the file processing code (a several-minute loop);
}
If your application is not running the main message processing loop of PeekMessage, TranslateMessage, and DispatchMessage then it will become completely non-responsive as you have noticed. That's the nature of message-driven event loops such as used by Windows.
You either need to run open_file in a different thread, or segment it in some way that it can process a small piece at a time and return and get called again to process another piece.
E.g.
while (running) {
MSG message;
while (PeekMessage(&message, window, ...) {
TranslateMessage(&message);
DispatchMessage(&message);
}
open_file_piece();
}

vsc++ ,Can I using MFC in a console project and show window?

I create a win32 console project with option add MFC, and MSVS produce some code automate for me,if I wanted drawing some windows, do I need derive my own class from CWinApp? or use an instance of CWinApp directly?
If I use an instance of CWinApp directly,how do I custom InitInstance function to get the frame that I like?
Simply put,If I want make some windows using the code below,how to do that?
int main()
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(nullptr);
if (hModule != nullptr)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
wprintf(L"Fatal Error: MFC initialization failed\n");
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
}

Why simple console app runs but dialog based does not run in WIN CE 6.0?

I am developing an application for Windows CE 6.0 in embedded Visual C++ 4.
I created a simple console application (WCE Application) with platform "Pocket PC 2003" with the following simple code:
#include "stdafx.h"
#include <stdio.h>
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
FILE * pFile;
char c;
pFile=fopen("alphabet.txt","wt");
for (c = 'A' ; c <= 'Z' ; c++) {
putc (c , pFile);
}
fclose (pFile);
return 0;
}
This simple code works correctly on my WinCE 6.0 device and "alphabet.txt" is created.
But when I create a dialog based project (WCE MFC AppWizard(exe)) and put this code in main class of my project before the initialization of my dialog window it does not work and no "alphabet.txt" file is created and my app does not open without any messages.
BOOL CFffffApp::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
FILE * pFile;
char c;
pFile=fopen("alphabet.txt","wt");
for (c = 'A' ; c <= 'Z' ; c++) {
putc (c , pFile);
}
fclose (pFile);
CFffffDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
Why it does not work and how can I resolve this problem?
Thanks in advance,
Does the target device have the MFC runtimes on it? They also have to be the ones your app is built for. Be aware that eVC 4.0 used mfcce400.dll, which did not ship with Platform Builder 6.0 at all (in fact IIRC MFC isn't even in the CE 6.0 OS catalog and Studio '08 used a newer MFC version for devices). You'll have to distribute the mfcce400 binaries (they're in the eVC SDKs) along with your app.
My C++ is very rusty, but you still have to initialize your controls.
CFffffDlg dlg = new CFffffDlg(); // << Initialize the dlg
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
Right? Is that all you need?

Problems using IFileDialog on Windows 7

I'm facing some weird (at least for me) behavior on using the Common Item Dialogs in my MFC Windows application running on Windows 7 or Vista.
According to the MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/bb776913(v=vs.85).aspx I'm using the new interfaces to display file open and save dialogs:
bool OpenFileDialog(CString& strFile, CString strTitle, CStringArray& astrFilter, CStringArray& astrFilterExtension, ULONG nFlags, HWND hParentWnd)
{
USES_CONVERSION;
INT_PTR nResult = 0;
INT_PTR nFilterCount = astrFilter.GetCount();
IFileDialog* pfod = 0;
HRESULT hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfod));
if(SUCCEEDED(hr))
{
// New dialog starting with Vista/Windows 7
COMDLG_FILTERSPEC* pOpenTypes = 0;
if((nFilterCount > 0) && (nFilterCount == astrFilterExtension.GetCount()))
{
pOpenTypes = new COMDLG_FILTERSPEC[nFilterCount];
for(int nIdx = 0; nIdx < nFilterCount; nIdx++)
{
pOpenTypes[nIdx].pszName = astrFilter[nIdx].GetBuffer();
pOpenTypes[nIdx].pszSpec = astrFilterExtension[nIdx].GetBuffer();
}
}
// Set the file types to display.
if(pOpenTypes)
{
hr = pfod->SetFileTypes(nFilterCount, pOpenTypes);
if(SUCCEEDED(hr))
hr = pfod->SetFileTypeIndex(0);
}
if(!strFile.IsEmpty())
pfod->SetFileName(strFile);
if(!strTitle.IsEmpty())
pfod->SetTitle(strTitle);
if(SUCCEEDED(hr))
{
// Ensure the dialog only returns file system paths.
DWORD dwFlags;
hr = pfod->GetOptions(&dwFlags);
if(SUCCEEDED(hr))
{
dwFlags |= FOS_FORCEFILESYSTEM;
if(nFlags & OFN_FILEMUSTEXIST)
dwFlags |= FOS_FILEMUSTEXIST;
if(nFlags & OFN_PATHMUSTEXIST)
dwFlags |= FOS_PATHMUSTEXIST;
hr = pfod->SetOptions(dwFlags);
if(SUCCEEDED(hr))
{
// Create an event handling object, and hook it up to the dialog.
IFileDialogEvents* pfde = NULL;
DWORD dwCookie;
// Actually only added for debugging purposes
/*hr = CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde));
if(SUCCEEDED(hr))
{
// Hook up the event handler.
hr = pfod->Advise(pfde, &dwCookie);
if(!SUCCEEDED(hr))
{
pfde->Release();
pfde = 0;
}
}*/
// Now show the dialog. Usually called with hParent == 0
if(hParentWnd)
hr = pfod->Show(::GetWindow(hParentWnd, GW_OWNER));
else
hr = pfod->Show(0);
// do something with the path when the dialog was closed...
So the dialog appears and works fine if I want to select a file from a normal drive. I can navigate through the folders and select any file I want. On leaving the dialog I also get the correct file information.
But it doesn't work for one of the Libraries in the navigation pane on the left side. Whenever I try to select a Library like Documents, Videos or Pictures the dialog doesn't update the right pane which shows the folder/library content.
What I noticed is that on clicking a Library in the file open/save dialog the OnFolderChanging() event of the IFileDialogEvents interface is fired but the OnFolderChange() and OnSelectionChange() are not. Those events are fired if I click and navigate on a "normal" drive like C.
I also tried to call the dialogs early in my InitInstance method to avoid possible side-effects with my other code but this didn't help either.
Is there someone who had the same behavior and was able to resolve this?
Thanks a lot!
So I finally found the answer to this issue. Creating the new MFC project for the application was the actual hint to solve this. The reason was that the "Stack reserve size" was too big. The settings in the old VS6.0 project had the stack size increased to more than 100MB. Apparently the IFileDialog based dialogs do not work properly when the reserved stack size is simply too large (other thing might don't work also as expected). So I had to set it back to 15MB in my case.

How to implement code for multiple buttons using c++ in Silverlight for Windows Embedded

I have referred the following link:
Silverlight for Windows Embedded
By referring this link i created a demo application which consist of two buttons created using Microsoft expression blend 2 tools. And then written a code referring the above site. Now my button names are "Browser Button" and "Media Button". On click of any one of the button i should able to launch the respective application. I was able to do for "Browser Button" but not for "Media Button" and if i do for "Media Button" then i am not able to do for "Browser Button".. I mean to say that how should i create event handler for both the buttons.
This is the code in c++ which i should modify
class BtnEventHandler
{
public:
HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
{
RETAILMSG(1,(L"Browser event"));
Execute(L"\\Windows\\iesample.exe",L"");
return S_OK;
}
};
// entry point for the application.
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,int nCmdShow)
{
PrintMessage();
int exitCode = -1;
HRESULT hr = S_OK;
if (!XamlRuntimeInitialize())
return -1;
HRESULT retcode;
IXRApplicationPtr app;
if (FAILED(retcode=GetXRApplicationInstance(&app)))
return -1;
if (FAILED(retcode=app->AddResourceModule(hInstance)))
return -1;
XRWindowCreateParams wp;
ZeroMemory(&wp, sizeof(XRWindowCreateParams));
wp.Style = WS_OVERLAPPED;
wp.pTitle = L"Bounce Test";
wp.Left = 0;
wp.Top = 0;
XRXamlSource xamlsrc;
xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINTRESOURCE(IDR_XAML1));
IXRVisualHostPtr vhost;
if (FAILED(retcode=app->CreateHostFromXaml(&xamlsrc, &wp, &vhost)))
return -1;
IXRFrameworkElementPtr root;
if (FAILED(retcode=vhost->GetRootElement(&root)))
return -1;
IXRButtonBasePtr btn;
if (FAILED(retcode=root->FindName(TEXT("BrowserButton"), &btn)))
return -1;
IXRDelegate<XRMouseButtonEventArgs>* clickdelegate;
BtnEventHandler handler;
if(FAILED(retcode=CreateDelegate
(&handler,&BtnEventHandler::OnClick,&clickdelegate)))
return -1;
if (FAILED(retcode=btn->AddClickEventHandler(clickdelegate)))
return -1;
UINT exitcode;
if (FAILED(retcode=vhost->StartDialog(&exitcode)))
return -1;
return exitCode;
}
I have to add event handler for both the button so that on emulator whenever i click on any one of the button i should be able to launch the respective applications.
Thanks in advance
You can create two seperate functions to be the handlers for each button.
If you want the same handler to identify which button was pressed and act accordingly you can read the following MSDN article that demonstrates that.
I have not tried this, but you can also use IXRDependencyObject::GetName of the source object to know which button was pressed.
Your handler would look like:
HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
{
BSTR pName[50];
source->GetName(pName);
if (_tcscmp(L"BrowserEvent", LPCWSTR(pName)) == 0)
{
RETAILMSG(1,(L"Browser event"));
Execute(L"\\Windows\\iesample.exe",L"");
}
else if (_tcscmp(L"BrowserEvent", LPCWSTR(pName)) == 0)
{
/* Handle another button or element */
}
return S_OK;
}