Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Other Objects

Status
Not open for further replies.

ryan

Programmer
Nov 13, 2000
73
US
I'm wonder how a MSVC++ DLL might interact with other objects on the system. In VB you can create the object like so and use it:

Dim MyObject
Set MyObject = Server.CreateObject("x.x")

How would I could I access an object like this from C?

RGK
 
Straight C -- Do a LoadLibrary() followed by GetProcAddress() calls for each of the exported functions you want to use. This would be "late bound"

Or...
Link the DLL statically into your project. This would be "early bound".

Chip H.
 
Hi
chiph, it is a COM program.
Ryank,
------------------------------------
GUID guid;
IDispatch* pDisp;
HRESULT hr;
hr=CLSIDFromProgID(L"x.x",&guid);
hr=CoCreateInstance(guid,NULL,CLSCTX_INPROC_SERVER,
DDI_IDispatch,&pDisp);
//do something
pDisp->Release();
////the interface pDisp is an analog of you object in VB.
-----------------------------------
Also a better way is to obtain at the first the interface IUnknown:
GUID guid;
IDispatch* pDisp;
IUnknown* pUnk;
HRESULT hr;
hr=CLSIDFromProgID(L"x.x",&guid);
hr=CoCreateInstance(guid,NULL,CLSCTX_INPROC_SERVER,
DDI_IUnknown,&pUnk);
hr=pUnk->QueryInterface(IID_IDispatch,&pDisp);
//do something
pDisp->Release();
pUnk->Release();
With interface IUnknown you can query all interfaces what the object expose.
----------------------------------
you should check on errors the hresult (hr) after each function executed.
if(FAILED(hr))
{
....
}
or
if(SUCCEDED(hr))
{
}
-------------
To begin working with COM you must call
hresult=OleInitialize(NULL);
or CoInitialize(...).
after end of COM programming call
OleUninitialize().
 
There is a DDI_I..., please replace with IID_I...
It is my mistake.
 
Using the ATL smart pointers and the '#import' statement to generate the template typedefs is WAY easier and safer than interfacing directly with COM API's.

"But, that's just my opinion... I could be wrong".
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top