Question

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.

Was it helpful?

Solution

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;
}

OTHER TIPS

Isn't an OCX an ActiveX User Control? (something that you put onto a form for the user to interact with)?

The easiest way I know of to test COM/ActiveX stuff is to use excel. (Yes I know it sounds dumb, bear with me)

  1. Run Excel, create a new file if it hasn't done this for you
  2. Press Alt+F11 to launch the Visual Basic Editor (if you have excel 2007 it's on the 'Developer' ribbon tab thing

Now that you're in happy visual basic land...

  1. From the Tools menu, select References
  2. Select your OCX/COM object from the list, or click Browse... to find the file if it's not registered with COM - You may be able to skip this step if your OCX is already registered.
  3. From the Insert menu, select UserForm
  4. In the floating Toolbox window, right click and select Additional Controls
  5. Find your OCX in the list and tick it
  6. You can then drag your OCX from the toolbox onto the userform
  7. From the Run menu, run it.
  8. Test your OCX and play around with it.

  9. SAVE THE EXCEL FILE so you don't have to repeat these steps every time.

@orion thats so cool. Never thought of it that way.

Well @jschroedl thats was fun indeed.

Testing an activex in console app is fun. But I think its worth not trying down that path. You can call the methods or set and get the properties either through the way @jschroedl had explained or you can call the IDIspatch object through the Invoke function.

The first step is to GetIDsByName and call the function through Invoke and parameters to the function should be an array of VARIANTS in the Invoke formal parameter list.

All is fine and dandy. But once you get to events its downhill from there. Windows application requires a message pump to fire events. On a console you don't have one. I went down the path to implement a EventNotifier for the events just like you implement a CallBack interface in classic C++ way. But the events doesn't get to your implemented interface.

I am pretty sure this cannot be done on a console application. But I am really hoping someone out there will have a different take on events in a console application

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top