Click here to Skip to main content
15,918,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had com initialization as the first code line in WinMain. Because I had to use help, I added htmlhelp initialization as the first line of code. When I ran the code , com threw an exemption requesting to be the first line in the code. So, I wrote com initialization first instead, but when I ran the code, htmlhelp threw an exception requesting to be the first line of the code. How do I resolve this?

The relevant code is shown below:

C++
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        AppErrMessage msg{ AppErrMessage::ERM_COM_INITIALIZE_FAILED };
        pair<wstring, wstring> err = GetAppErrMessage(msg);

        XMessageBox(nullptr, err.first.c_str(), err.second.c_str(), MB_OK);

        return FALSE;
    }
    else
    {
        eCOMState = COMState::COM_INITIALIZED;
    }

    //DWORD_PTR dwCookie = NULL;
    //HtmlHelp(nullptr, nullptr, HH_INITIALIZE, (DWORD_PTR)&dwCookie);

    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    INITCOMMONCONTROLSEX cc;
    cc.dwSize = sizeof(INITCOMMONCONTROLSEX);
    cc.dwICC = ICC_UPDOWN_CLASS | ICC_DATE_CLASSES | ICC_USEREX_CLASSES | ICC_STANDARD_CLASSES;
    InitCommonControlsEx(&cc);

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_RESULTSHEETS, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance(hInstance, nCmdShow))
    {
        Gdiplus::GdiplusShutdown(gdiplusToken);
        CoUninitialize();
        //HtmlHelp(nullptr, nullptr, HH_UNINITIALIZE, (DWORD_PTR)&dwCookie);

        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RESULTSHEETS));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!IsWindow(hDlgPrint) || !IsDialogMessage(hDlgPrint, &msg))
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }

    Gdiplus::GdiplusShutdown(gdiplusToken);
    CoUninitialize();

    //HtmlHelp(nullptr, nullptr, HH_UNINITIALIZE, (DWORD_PTR)&dwCookie);

    return (int)msg.wParam;
}


What I have tried:

I have spent lengthy time debugging the program.
Posted
Updated 29-May-24 18:59pm
v4
Comments
0x01AA 23-May-24 12:20pm    
In case your question is related to 'winhlp32.exe' and you try that on W10 and later: It is not longer supported.
Gbenbam 23-May-24 14:21pm    
No it's winhtml help.
0x01AA 23-May-24 14:36pm    
Best is you share your actual code.
Gbenbam 29-May-24 7:48am    
I have added the relevant code. Perhaps you should take a look at it.
CHill60 24-May-24 10:18am    
Nope - I've really tried hard but my crystal ball is obviously not going to work. Could you possibly post the code in the question so we have a chance to work out what might be wrong?

Unfortunately, HTML help files are no longer offered when creating a Visual Studio project. Especially with MFC, this option was previously offered and the required files were also created. In order to use your own chm file, an absolute path must be specified and it makes sense to check whether the file actually exists there.

In a Win32 program the display of the help with the system call HtmlHelp() was successful if HH_INITIALIZE was NOT called before. It worked after InitInstance(), whereby you can then specify the main window or also before if you specify NULL.
C
// DWORD dwCookie = NULL;
// HWND wrtn = HtmlHelp( NULL, helpFilePath,
//    HH_INITIALIZE, (DWORD_PTR)&dwCookie); // Cookie returned by Hhctrl.ocx.

HWND wrtn = HtmlHelp(NULL, helpFilePath, HH_DISPLAY_TOPIC, 0);

if (wrtn == NULL) {
     DWORD error = GetLastError();
     WCHAR errorMessage[255];
     _stprintf(errorMessage, _T("Error displaying HTML help. Error code: %lu"), error);
     MessageBox(NULL, errorMessage, _T("Error"), MB_OK | MB_ICONERROR);
 }

With an MFC program it is much easier, as you can overwrite the member function OnHelp().
C
void Chtmlhelp3App::OnHelp()
{
  if (!PathFileExists(m_helpFilePath)) {
     MessageBox(NULL, _T("No Help found!"), _T("Error"), MB_OK | MB_ICONERROR);
     return;
}

HWND result = ::HtmlHelp(m_pMainWnd->GetSafeHwnd(), m_helpFilePath, HH_DISPLAY_TOPIC, 0);

if (result == NULL) {
  DWORD error = GetLastError();
  ...
  }
}

Apparently not every old chm file can be opened, I have created a completely new chm file for testing. HelpNDoc may also be helpful here.
 
Share this answer
 
v2
Comments
Gbenbam 1-Jun-24 6:08am    
I created my own chm file with HelpNDoc. Are you saying it is not necessary to initialize help in WinMain function. Actually my code successfully opened the chm file and launched help from the help menu, so I simply left it at that even though it failed to launch the chm file for context help. Because I did not know the implication of this, I decided to come here.
Gbenbam 1-Jun-24 6:11am    
Or are you saying that initialization should be dine at the time of launching help to view topics?
You better define COM variables and initialize them. Because it may take some milliseconds you may be careful before using it. Use some messaging for it.

You need also to call CoInitializEx() at first.
 
Share this answer
 
Comments
Gbenbam 29-May-24 7:48am    
I have added the relevant code. Perhaps you should take a look at it.
KarstenK 30-May-24 1:03am    
That is looking fine. Please describe the problem more in detail. Why arent you initialize the help module when the user is first using it?
Gbenbam 30-May-24 3:38am    
You mean I should initialize it when I try to launch help? So, when do I uninitialize it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900