We have answer of your question!

100% solved queries, no empty question

Question: Using an ocx in a console application


0

I want to quickly test an ocx. How do I drop that ocx in a console application. I have found some tutorials in CodeProject and but are incomplete.

Question author Rptony | Source

Answer


1


Sure..it's pretty easy. Here's a fun app I threw together. I'm assuming you have Visual C++.

Save to test.cpp and compile: cl.exe /EHsc test.cpp

To test with your OCX you'll need to either #import the typelib and use it's CLSID (or just hard-code the CLSID) in the CoCreateInstance call. Using #import will also help define any custom interfaces you might need.

#include "windows.h"
#include "shobjidl.h"
#include "atlbase.h"

//
// compile with:  cl /EHsc test.cpp
//

// A fun little program to demonstrate creating an OCX.
// (CLSID_TaskbarList in this case)
//

BOOL CALLBACK RemoveFromTaskbarProc( HWND hwnd, LPARAM lParam )
{
    ITaskbarList* ptbl = (ITaskbarList*)lParam;
    ptbl->DeleteTab(hwnd);	
    return TRUE;
}

void HideTaskWindows(ITaskbarList* ptbl)
{
    EnumWindows( RemoveFromTaskbarProc, (LPARAM) ptbl);
}

// ============

BOOL CALLBACK AddToTaskbarProc( HWND hwnd, LPARAM lParam )
{
    ITaskbarList* ptbl = (ITaskbarList*)lParam;
    ptbl->AddTab(hwnd);	

    return TRUE;// continue enumerating
}

void ShowTaskWindows(ITaskbarList* ptbl)
{
    if (!EnumWindows( AddToTaskbarProc, (LPARAM) ptbl))
    	throw "Unable to enum windows in ShowTaskWindows";
}

// ============

int main(int, char**)
{
    CoInitialize(0);

    try {
    	CComPtr<IUnknown> pUnk;

    	if (FAILED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**) &pUnk)))
    		throw "Unabled to create CLSID_TaskbarList";


    	// Do something with the object...

    	CComQIPtr<ITaskbarList> ptbl = pUnk;
    	if (ptbl)
    		ptbl->HrInit();

    	HideTaskWindows(ptbl);
    	MessageBox( GetDesktopWindow(), _T("Check out the task bar!"), _T("StackOverflow FTW"), MB_OK);
    	ShowTaskWindows(ptbl);
    }
    catch( TCHAR * msg ) {
    	MessageBox( GetDesktopWindow(), msg, _T("Error"), MB_OK);
    }		

    CoUninitialize();

    return 0;
}
Answer author Jschroedl

Tickanswer.com is providing the only single recommended solution of the question Using an ocx in a console application under the categories i.e c++ , visual-c++ , console , activex , . Our team of experts filter the best solution for you.


You may also add your answer

Thanks for contributing an answer to Tick Answer!