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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ and COM Commnication without MFC

Status
Not open for further replies.

kapil

Instructor
Apr 26, 2001
3
US
Hi

I would like to know, how do i instantiate my vb-COM
object without using MFC (microsoft foundation classes)
i.e directly thru C++

Regards
Kapil
 
well using stdio.h you can declare a pointer to a file object then open the file using fopen and use the name of the port as the file path .
then write to the file using fwrite
somthing like this

#include <stdio.h>
FILE*port;
port=fopen(&quot;COM2&quot;,&quot;w+&quot;);
fwrite(pointer_to_my_object,sizeof(myobject),1,port);
fclose(port);

this code may need a bit tweaking but it can be implemented as a function in a seperate generic class and called from a visual c++ dialog.


hope this helps

Tim222


[sig][/sig]
 
I think &quot;Tim222&quot; misread the original post which inquired about COM objects, not COM ports.

Here is a &quot;quick and dirty&quot; method of doing exactly what you want (without creating an IDL file, etc):

1) Import the executable COM object directly into your C++ source code using #import (i.e. #import &quot;class.dll&quot;)

2) Use something like the following snippet of code to fire up COM and create a pointer to your object (c1 in this example):

_c1 ptr;
CoInitialize(NULL);
HRESULT hr = ptr.CreateInstance(__uuidof(c1),NULL,CLSCTX_ALL);

Now you can access the properties and methods of c1 by saying &quot;ptr->Method()&quot; or &quot;ptr->Property = x;

Hope that helps!
[sig]<p>Pat Gleason<br><a href=mailto:gleason@megsinet.net>gleason@megsinet.net</a><br><a href= > </a><br> [/sig]
 
Your code will look like this. The function nvocation is in pDisp->Invoke, before it you need the Disp ID. All other code is pure initialisations.




IUnknown* pUnk;
IDispatch* pDisp;

HRESULT hResult;
OleInitialize(NULL);
hResult=::CLSIDFromProgID(&quot;ObjecBlablabla.Application&quot;,&clsid);
if(FAILED(hResult))
{
::MessageBox(NULL,&quot;application is not found&quot;,&quot;error&quot;,MB_OK|MB_ICONEXCLAMATION);
return;
}
setDispGuid(IID_IDispatch);
setUnkGuid(IID_IUnknown);
hResult=::CoCreateInstance(clsid,NULL,CLSCTX_SERVER,gUnk.toGUID(),(void FAR*FAR*)&pUnk);
if(FAILED(hResult))
{
::MessageBox(NULL,&quot;failed IUnknown&quot;,&quot;error&quot;,MB_OK|MB_ICONEXCLAMATION);
return;
}
hResult=pUnk->QueryInterface(gDisp.toGUID(),(void FAR* FAR*)&pDisp);
if(FAILED(hResult))
{
MessageBox(NULL,&quot;this class doesn't suppport IDispatch&quot;,&quot;warning&quot;,MB_OK|MB_ICONEXCLAMATION);
return;
}
IUnknown* pUnk;
IDispatch* pDisp;
HRESULT hResult;
DISPID dispid;
DISPPARAMS dpar;
DISPID dPut=DISPID_PROPERTYPUT;
VARIANTARG varg;
unsigned short* method=L&quot;Visible&quot;;
dpar.rgvarg=&varg;
dpar.rgdispidNamedArgs=&dPut;
dpar.cArgs=1;
dpar.cNamedArgs=1;
::VariantInit(&varg);
V_VT(&varg)=VT_BOOL;
V_BOOL(&varg)=_visible;

hResult=pDisp->GetIDsOfNames(IID_NULL,&method,1,LOCALE_SYSTEM_DEFAULT,&dispid);
if(FAILED(hResult))
{
::MessageBox(NULL,&quot;function doesn't exists&quot;,&quot;error&quot;,MB_OK|MB_ICONEXCLAMATION);
::VariantClear(&varg);
}
hResult=pDisp->Invoke(dispid,IID_NULL,LOCALE_SYSTEM_DEFAULT,DISPATCH_PROPERTYPUT,
&dpar,NULL,NULL,NULL);
if(FAILED(hResult))
{
::MessageBox(NULL,&quot;can't execute function&quot;,&quot;error&quot;,MB_OK|MB_ICONEXCLAMATION);
::VariantClear(&varg);
}


::VariantClear(&varg); [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top