📄 如何在vc++中调用标准com组件.mht
字号:
void main()
{
BSTR bstrDesc;
try
{
CoInitialize(NULL);
short st =3D 2;
short st1;
// Declare the Interface Pointer for your Visual Basic object. =
Here,
// _Class1Ptr is the Smart pointer wrapper class representing the
// default interface of the Visual Basic object.
_Class1Ptr ptr;
// Create an instance of your Visual Basic object, here
// __uuidof(Class1) gets the CLSID of your Visual Basic object.
ptr.CreateInstance(__uuidof(Class1));
st1 =3D ptr->MyVBFunction(&st);
}
catch(_com_error &e)
{
bstrDesc =3D e.Description();
}
CoUninitialize();
}
</PRE></CODE></TD></TR>
<TR>
<TD class=3Dnumber>3.</TD>
<TD class=3Dtext>Compile and run your project. The message box =
from=20
Project1.DLL should appear. </TD></TR></TBODY></TABLE>The =
#import method=20
can be used in a Win32 Application, a Console Application, or in =
MFC as=20
well. <BR><BR>Method 3 - Pure COM Interface <BR><BR>
<TABLE class=3D"list ol">
<TBODY>
<TR>
<TD class=3Dnumber>1.</TD>
<TD class=3Dtext>Start Visual C++ and select New from the File =
menu.=20
Choose MFC AppWizard (Exe), name the project tst1, and click =
OK.=20
When the MFC Appwizard dialog box appears, select Dialog =
Based and=20
click Finish. Click OK when the next dialog box appears. =
</TD></TR>
<TR>
<TD class=3Dnumber>2.</TD>
<TD class=3Dtext>The Resource Editor is started by default. =
Delete all=20
the controls on the dialog box and add a Command button on =
it,=20
retaining the default caption "Button1". </TD></TR>
<TR>
<TD class=3Dnumber>3.</TD>
<TD class=3Dtext>Double-click Button1 to display the Add =
Member=20
Function dialog box. Click OK to accept the name OnButton1. =
</TD></TR>
<TR>
<TD class=3Dnumber>4.</TD>
<TD class=3Dtext>Click OLE/COM Object Viewer on the Tools =
menu. Select=20
View Typelib from the File menu and choose the Project1.dll =
you=20
created earlier. Click Open to display the ITypeLib Viewer, =
which=20
contains the .idl file for your DLL. </TD></TR>
<TR>
<TD class=3Dnumber>5.</TD>
<TD class=3Dtext>Copy the contents of your .idl file (contents =
of the=20
right pane) to the Clipboard. Hold the SHIFT key down while =
paging=20
or scrolling from the first character to the end of the text =
in the=20
pane. Press Ctrl+C to copy the marked text to the Clipboard. =
</TD></TR>
<TR>
<TD class=3Dnumber>6.</TD>
<TD class=3Dtext>Click New on Visual C++ File menu. Select =
Text File=20
on the New dialog box, name the file test1.idl, and click =
OK. </TD></TR>
<TR>
<TD class=3Dnumber>7.</TD>
<TD class=3Dtext>A blank text file appears. Paste the data =
from the=20
Clipboard into it and save the file. </TD></TR>
<TR>
<TD class=3Dnumber>8.</TD>
<TD class=3Dtext>Select Settings from the Project menu, expand =
the=20
tst1 and Source Files nodes of the tree view, and select =
test1.idl.=20
Click the MIDL tab, enter test1.h in the "Output header file =
name"=20
box, and click OK. </TD></TR>
<TR>
<TD class=3Dnumber>9.</TD>
<TD class=3Dtext>Open tst1Dlg.cpp and add the following files =
to the=20
includes section: <CODE><PRE class=3Dcode> #include =
<initguid.h>
#include "test1.h"
</PRE></CODE></TD></TR>
<TR>
<TD class=3Dnumber>10.</TD>
<TD class=3Dtext>Click the ClassWizard on the View menu, =
select=20
Ctst1App in the Class Name box, and double-click =
InitInstance in the=20
Messages box. Click Edit Code to bring up the code for: =
<CODE><PRE class=3Dcode> BOOL CTst1App::InitInstance()
</PRE></CODE>Find the line: <CODE><PRE class=3Dcode> =
AfxEnableControlContainer();
</PRE></CODE>Add the following line before it: <CODE><PRE =
class=3Dcode> AfxOleInit();
</PRE></CODE></TD></TR>
<TR>
<TD class=3Dnumber>11.</TD>
<TD class=3Dtext>Open the ClassWizard again. On the Message =
Maps tab=20
select CTst1Dlg in the Class Name box and IDC_BUTTON1 in the =
Object=20
IDs box. Double-click BN_CLICKED in the Messages box, and =
click Edit=20
Code to bring up the code for void CTst1Dlg::OnButton1(). =
Replace=20
the OnButton1() function with the following code: <CODE><PRE =
class=3Dcode> void CTst1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here.
_Class1 *pClass =3D NULL;
IUnknown *pUnk =3D NULL;
// HRESULT hr =3D CoCreateInstance(CLSID_Class1,NULL,
// CLSCTX_INPROC_SERVER,IID__Class1,(void **)&pClass);
// You can directly get the Interface ID as in the previous line =
or
// you can do a QueryInterface on IUnknown to get the IID
// as in the following three lines:
HRESULT hr =3D =
CoCreateInstance(CLSID_Class1,NULL,CLSCTX_INPROC_SERVER,
IID_IUnknown,(void **)&pUnk);
hr =3D pUnk->QueryInterface(IID__Class1,(void **)&pClass);
pUnk->Release();
// Once you have the IID, you can make use of the interface =
pointer
// to access our Visual Basic DLL.
short st =3D 2;
short st1;
hr =3D pClass->MyVBFunction(&st,&st1);
pClass->Release();
}
</PRE></CODE></TD></TR>
<TR>
<TD class=3Dnumber>12.</TD>
<TD class=3Dtext>Compile your .exe file (F7) and run your =
application=20
(F5). Click Button1 in the dialog box. The message box from =
the=20
Visual Basic DLL appears.</TD></TR></TBODY></TABLE>
<P class=3DtopOfPage><A=20
=
href=3D"http://support.microsoft.com/default.aspx?scid=3Dhttp://support.m=
icrosoft.com:80/support/kb/articles/Q194/8/73.ASP&NoWebContent=3D1#to=
p"><IMG=20
alt=3D""=20
=
src=3D"http://support.microsoft.com/library/images/support/kbgraphics/pub=
lic/en-us/uparrow.gif">Back=20
to the top</A></P></DIV>
<H2 class=3DsubTitle id=3DtocHeadRef>REFERENCES</H2>
<SCRIPT type=3Dtext/javascript>loadTOCNode(1, =
'references');</SCRIPT>
<DIV class=3Dsbody>For additional information, please see the =
following=20
articles in the Microsoft Knowledge Base:=20
<DIV class=3Dindent><A class=3DKBlink=20
=
href=3D"http://support.microsoft.com/kb/178749/EN-US/">178749</A><SPAN=20
class=3DpLink> =
(http://support.microsoft.com/kb/178749/EN-US/)</SPAN> How To=20
Create an Automation Project Using MFC and a Type Library =
<BR><BR><A=20
class=3DKBlink=20
=
href=3D"http://support.microsoft.com/kb/188817/EN-US/">188817</A><SPAN=20
class=3DpLink> =
(http://support.microsoft.com/kb/188817/EN-US/)</SPAN> How To=20
VC++ MFC Client for the ComCallingJava Sample </DIV>
<P class=3DtopOfPage><A=20
=
href=3D"http://support.microsoft.com/default.aspx?scid=3Dhttp://support.m=
icrosoft.com:80/support/kb/articles/Q194/8/73.ASP&NoWebContent=3D1#to=
p"><IMG=20
alt=3D""=20
=
src=3D"http://support.microsoft.com/library/images/support/kbgraphics/pub=
lic/en-us/uparrow.gif">Back=20
to the top</A></P></DIV></DIV>
<DIV class=3DappliesTo>
<HR>
<A id=3Dappliesto></A>
<H5>APPLIES TO</H5>
<TABLE class=3Dlist>
<TBODY>
<TR>
<TD class=3Dbullet>=E2=80=A2</TD>
<TD class=3Dtext>Microsoft Visual C++ 5.0 Enterprise =
Edition</TD></TR>
<TR>
<TD class=3Dbullet>=E2=80=A2</TD>
<TD class=3Dtext>Microsoft Visual C++ 6.0 Enterprise =
Edition</TD></TR>
<TR>
<TD class=3Dbullet>=E2=80=A2</TD>
<TD class=3Dtext>Microsoft Visual C++ 5.0 Professional =
Edition</TD></TR>
<TR>
<TD class=3Dbullet>=E2=80=A2</TD>
<TD class=3Dtext>Microsoft Visual C++ 6.0 Professional =
Edition</TD></TR>
<TR>
<TD class=3Dbullet>=E2=80=A2</TD>
<TD class=3Dtext>Microsoft Visual C++, 32-bit Learning Edition =
6.0</TD></TR></TBODY></TABLE></DIV>
<P class=3DtopOfPage><A=20
=
href=3D"http://support.microsoft.com/default.aspx?scid=3Dhttp://support.m=
icrosoft.com:80/support/kb/articles/Q194/8/73.ASP&NoWebContent=3D1#to=
p"><IMG=20
alt=3D""=20
=
src=3D"http://support.microsoft.com/library/images/support/kbgraphics/pub=
lic/en-us/uparrow.gif">Back=20
to the top</A></P>
<DIV class=3Dkeywords>
<TABLE>
<TBODY>
<TR>
<TD class=3Dheader>
<H5>Keywords: </H5></TD>
<TD class=3Dtext>kbhowto kbinfo kbcode =
KB194873</TD></TR></TBODY></TABLE>
<P class=3DtopOfPage><A=20
=
href=3D"http://support.microsoft.com/default.aspx?scid=3Dhttp://support.m=
icrosoft.com:80/support/kb/articles/Q194/8/73.ASP&NoWebContent=3D1#to=
p"><IMG=20
alt=3D""=20
=
src=3D"http://support.microsoft.com/library/images/support/kbgraphics/pub=
lic/en-us/uparrow.gif">Back=20
to the top</A></P></DIV><!-- - -KB 3 end- - --></DIV></DIV><SPAN=20
class=3Dbookmark><A id=3Dsurvey name=3Dsurvey> </A></SPAN>
<DIV class=3Dwp id=3DfmsSurvey style=3D"DISPLAY: none">
<DIV class=3Dfms><IFRAME id=3Dsubmitframe=20
style=3D"DISPLAY: inline; WIDTH: 0px; HEIGHT: 0px" border=3D0=20
src=3D"about:blank" frameBorder=3D0></IFRAME><NOSCRIPT>
<DIV class=3DSURVEYNOSCRIPT></DIV></NOSCRIPT><INPUT =
id=3Dsurveyname=20
type=3Dhidden name=3Dsurveyname><INPUT id=3Dshowpage type=3Dhidden =
value=3D1=20
name=3Dshowpage>
<DIV id=3DSURVEYSECTIONCONTAINER>
<DIV class=3DSURVEYSECTION id=3DSURVEYSECTION_1 style=3D"DISPLAY: =
none"=20
xmlns:sw=3D"urn:schemas-microsoft-com/PSS/PSS_Survey01"><INPUT =
type=3Dhidden=20
value=3D1 name=3Did><INPUT type=3Dhidden =
name=3DCompoundingBranchRules><INPUT=20
type=3Dhidden name=3DNextSectionByTopicRef>
<DIV class=3DQUESTIONCONTAINER id=3DSURVEYQUESTION_7811><INPUT =
type=3Dhidden=20
value=3D7811 name=3Did><INPUT type=3Dhidden value=3DLABEL =
name=3Dtype><INPUT=20
type=3Dhidden value=3D0 name=3Drequired>
<DIV class=3DQUESTIONTEXT>
<H5>Provide feedback on this article</H5></DIV>
<DIV class=3DQUESTIONINSTRUCTION></DIV>
<DIV class=3DQUESTIONREQUIRED></DIV></DIV>
<DIV class=3DQUESTIONCONTAINER id=3DSURVEYQUESTION_7814><INPUT =
type=3Dhidden=20
value=3D7814 name=3Did><INPUT type=3Dhidden value=3DCHOICE =
name=3Dtype><INPUT=20
type=3Dhidden value=3D0 name=3Drequired>
<DIV class=3DQUESTIONTEXT>Did this article help you solve your=20
problem?</DIV>
<DIV class=3DQUESTIONINSTRUCTION></DIV>
<DIV class=3DQUESTIONREQUIRED></DIV><INPUT type=3Dhidden =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -