Instance of COMObject...

Hi!

This is a easy question I think, but I don't know the answer.
I have been working with COm-object before, but then I used MFC and all I did was "Add new class" with the Wizard, chose a .tbl or .dll and there was my wrapperclass, ready to use.
Now I´m working in a project without MFC, and then I can't use COleDispatchDriver and all that stuff that I got before.
So, HOW do I now create the object and use the functions??
Do I need the _com_ptr_t class, or what do I do?
(I dont have very many points, but you can get a littla bit more if this is a harder question than I think and you have the best answer)
THANKS!!
ScattanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

basantCommented:
The answer is use Win32 APIs

Use CoCreateInstance for creating
object
and then call methods on the objecs.

The following code shows how
to use COM object from Win32 calls.

You should have *_i.c file to
include with this code.
say idl file name : one.idl
then midl compiler generates
one_i.c and one.h

Which you have to include
with the source code I have posted.




void f()
{
if( ! InitCOM() ) return;
cout << "\n Method CoGetClassObject() - ";

LPCLASSFACTORY pCf;
HRESULT hErr = CoGetClassObject ( CLSID_IBeep ,
            CLSCTX_ALL ,
            NULL ,
                  IID_IClassFactory,
            (LPVOID FAR*) &pCf );

if ( hErr )
            cout << "\n Error = " << hErr << " " << REGDB_E_CLASSNOTREG;
if( hErr == REGDB_E_CLASSNOTREG)
            cout << "\n Class Not Registred ";
      LPBEEP pBeep;
      if ( FAILED( GetScode( hErr ) ) )
      {
            cout << "\n Failed CoGetClassObject ";
            PrintLastError();
      }
      else
      {
            cout << "\n Class has been Registered : "
                  " CoGetClassObject Successful";
            
            hErr = pCf->CreateInstance( NULL , IID_IBeep,
                  (LPVOID FAR*) &pBeep );
            pCf->Release();
            if ( FAILED( GetScode( hErr ) ) )
            {
                  cout << "\n Failed CreateInstance ";
            }
            else
            {
                  cout << "\n Calling DoBeep Function ";
                  pBeep->DoBeep( -1 );
                  pBeep->Release();
            }
      }
      
      // Other way for Calling COM
      cout << "\n\n\n Other Way to Call COM Object :";
      cout.flush();
      Sleep( 5000);
      LPUNKNOWN pUnk;
      hErr = CoCreateInstance( CLSID_IBeep, NULL,
                        CLSCTX_INPROC_SERVER,
                        IID_IUnknown,
                        (LPVOID FAR*) &pUnk);
      if( hErr )
            cout << "\n Error = " << hErr << " " << REGDB_E_CLASSNOTREG;
      if( hErr == REGDB_E_CLASSNOTREG)
            cout << "\n Class Not Registred ";
      if ( FAILED( GetScode( hErr ) ) )
      {
            cout << "\n Failed CoGetClassObject ";
            PrintLastError();
      }
      else
      {
            hErr = pUnk->QueryInterface( IID_IBeep,
                  (LPVOID FAR*) &pBeep );
            pUnk->Release();
            if ( FAILED( GetScode( hErr ) ) )
            {
                  cout << "\n Failed CreateInstance ";
            }
            else
            {
                  cout << "\n DoBeep After CoCreateInstace ";
                  pBeep->DoBeep( -1 );
                  pBeep->Release();
            }
      }
      DeInitCOM();
}







BOOL InitCOM()
{
      DWORD coVersion = CoBuildVersion();
      if ( HIWORD( coVersion) != rmm ||
            LOWORD( coVersion) < rup)
      {
            cout << "\n Bad Version Of COM ";
            return FALSE;
      }
      HRESULT hErr = CoInitialize( NULL );
      if ( FAILED( GetScode( hErr ) ) )
      {
            cout << "\n Failed to Initialize OLE ";
            return FALSE;
      }
return TRUE;
}

void DeInitCOM()
{
      CoUninitialize();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
basantCommented:
The beauti of the COM object
is that you can call it from any
m/c and from many languages e.g.
VB/Java.
0
ScattanAuthor Commented:
Thank you for a very complete answer!

I'm very much a beginner with these things, so I have to ask wath you mean by:
"You should have *_i.c file to
include with this code. ",

the only file I have with this component I want to use is a .dll-file.

Anyway, the rest I thank you very much for!
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ScattanAuthor Commented:
Sorry, I thougt I had the answer, but I still don't get it!
I give you some more points if you just tell me, HOW do I get this "one.h" (in this case) file, the interface - or wrapper class, so you can use this pBeep variable??
The only thing I have is the .dll- file, and I don't understand how to get the interface definition. In all he examples I see there is a .lib file to import, or a .tbl file or they assume that I allready have this .h-file to get the interface-class.

Thanks, I really need your help....
0
basantCommented:
You can use oleview which
is shipped with the VC++.
There you open the *.DLL file
this will show you
the idl file.
Save the file say one.idl

Go to Command Prompt
Run the Command
C:> midl one.idl
this will generate one.h
and one_i.c.
You can use these two
files in your project.

If you have any other problem, tell me.
0
ScattanAuthor Commented:
Thank you!

Thats just the thing I needed to know!

I guess there something wrong with my object.dll file then, because when I try to open it in the oleviewer it says
"IMoniker::BindToObject failed....etc."

But now I know next time how to get the h.file, thanks!

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.