Re: ATL console applcation using ActiveX control
Hi Brian and Alexander!
Thank you very much for helping me in this case!
First of all I want to clearify some things which I have not made clear:
1) My activeX control which I try to invoke a method from is created in VC++
using MFC.
2) What I would like to do is, if possible (i dont know if it is): create an
atl console application (by using c++) and invoke a method from the control,
which only returns a number.
3) As it stands now, my code is a little modified from what I posted here
first. My code is posted below. The hres is now S_OK, which indicate the that
I have "contact" with my control. It crashes when I call the method:
x=calc->getAge();
One should think that this indicates that there is something wrong with my
activeX control, but I have tested it in different containers and everything
works well. All these other containers are typical dialog based application
written in VC++ or VB.
And another argument that the control is working properly is that it is
created using the MFC activex wizzard, and then just adding a simple method.
So what do you think is happening?
My atl console app code is now:
#include "stdafx.h"
#include <atlcomcli.h>
#import "Calc.ocx" no_namespace
#include <iostream>
#include "conio.h"
using namespace std;
int main ()
{
CoInitialize (NULL);
// Smart Pointer
CComQIPtr <_DCalc> calc;
// Instantiate Class using Prog Id
HRESULT hRes = calc.CoCreateInstance (L"Calc.CalcCtrl.1");
if (SUCCEEDED (hRes))
{
cout<<"Created the instance"<<endl;
short x =19;
x=calc->getAge();
cout<<"Age : "<<x<<endl;
cout<<"Press any key to continue....";
_getch();
calc.Release();
}
else
{
cout<<"There was an error.."<<endl;
cout<<"Press any key to continue....";
_getch();
}
CoUninitialize();
return 0;
}
Thanks again guys!
"Brian Muth" wrote:
"Alexander Nickolov" <agnickolov@mvps.org> wrote in message
news:OrFPHTUxGHA.1216@TK2MSFTNGP03.phx.gbl...
Controls require hosting on a form/dialog. You can't just instantiate
the control's object and start callign its methods. ATL already
implements hosting support for ActiveX Controls. See: AtlAxWinInit,
CAxWindow. For your purposes you can host the control in a
hidden window I guess.
It really depends on what the OP means by "what's wrong with this code". As
it stands, the code should run, instantiate the control, and call the method
getAge() successfully without displaying the VB form.
Brian