Click to See Complete Forum and Search --> : How to call COM interface calling from VC++ application?--Urgent


May 5th, 1999, 05:21 PM
HI,

Environment: VC++5.0,WinNT.


I have an VC++ application from which I need to call an external COM interface.
Can any body tell how to do it. I have't worked much in COM. Please let me know in detail.

Thanx in advance.

NMNB

Bob Clarke
May 5th, 1999, 10:39 PM
You are asking a lot in one short question, but this should get you started. I suggest that you go to your favorite library or bookstore and find one or two books that fit your knowledge level for further assistance.

1. In your stdafx.h file, place an import statement for the type library, something like this:

#import "Funcs.tlb" no_namespace named_guids // for ITextFile

2. Where you need the interface, instantiate it:

ITextFile* pITextFile = NULL;
HRESULT hTemp = CoCreateInstance( CLSID_TextFile,
NULL,
CLSCTX_ALL,
IID_ITextFile,
(void**) &pITextFile );
if(FAILED(hTemp))
{
::MessageBox(m_hWnd, "Creation of the Text File object failed,\n \
Trading system will not be able to verify prices.", "CoCreateInstance Failure", MB_OK|MB_ICONERROR);
return;
}


3. Use the interface, such as:

USES_CONVERSION;
BSTR bstrFilename = A2BSTR(cPath.GetBuffer(2));
if(pITextFile->Create( &bstrFilename))
{
// do something with the file
}
pITextFile->Close();

4. When done, release the pointer:

if(pITextFile != NULL)
{
pITextFile->Release();
pITextFile = NULL;
}

ksheeraj
May 6th, 1999, 02:53 AM
In ATL3.0 there is no need to set your smart pointers to NULL.
And moreover in ATL2.0 we ues to use statment like
if(m_spSomeServer!=NULL)
{
dosomething();
}
now you can call it like
if (m_spSomeServer)
{
dosomething();
}

39639,Leslie St.
Apt #157
Fremont USA 94538

May 6th, 1999, 09:32 AM
Hi,

Thanx for the solution and I will do that. I have another question if I want to do the same stuff in a VC++ application with out using MFC How should I go about? what I mean is if I have a VC ++ console application then how do I go about calling COM interface from this application?

NMNB

Bob Clarke
May 6th, 1999, 11:20 PM
The only MFC-specific information in my example was the reference to stdafx.h, but you can put the import statement in a different header, or in a cpp module. See the MSDN docs under "#import" for details.