System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q194873
This article represents an introduction to creating Visual
C++ clients for Visual Basic ActiveX components. If you are already proficient
with Visual C++, you may wish to refer to the reference section for a list of
more advanced topics.
Create a Visual Basic ActiveX DLL project. Class1 is
created by default.
Add the following code to Class1:
Public Function MyVBFunction(x As Integer) As Integer
MsgBox x
End Function
Compile the DLL as c:\Project1.dll and exit Visual Basic.
Method 1 - CreateDispatch:
Start Visual C++ and select New on the File menu. Choose
MFC AppWizard (Exe) and a project name, and Click OK. When the MFC AppWizard
dialog box appears, click Finish. Click OK on the next dialog box.
Select ClassWizard on the View menu, pick Ctst1App in the
Class Name box, and double-click InitInstance in the Messages box. Click Edit
Code to bring up the code for BOOL CTst1App::InitInstance(), find the line
AfxEnableControlContainer();, and add the following line before it:
AfxOleInit();
Select ClassWizard from the View menu and click the
Automation tab. Click AddClass and select "from a TypeLibrary". Specify
Project1.dll, the Visual Basic DLL which was created in step 3. When the
Confirm Classes dialog box appears, click OK. Click OK again to close the MFC
ClassWizard dialog box.
Open your <App Name>.cpp file and add the line
#include "Project1.h". You need to include Project1.h wherever you have code
that accesses project1.dll.
Open the ClassWizard again. On the Message Maps tab, select
CAboutDlg in the Class Name box and IDOK in the Object IDs box, and then
double-click BN_CLICKED. Click OK in response to the dialog box and OK again to
close the ClassWizard.
Open <App Name>.cpp, scroll to the bottom to
theCAboutDlg::OnOK(), and replace it with the following code:
void CAboutDlg::OnOK()
{
short st = 2;
short st1;
_Class1 p;
p.CreateDispatch("Project1.Class1");
st1 = p.MyVBFunction(&st);
CDialog::OnOK();
}
Compile your .exe file (F7).
Run the .exe file and select About on the Help menu. Click
OK on the About box and the message box that was specified in project1.dll
appears. Click the Close button to dismiss the dialog box.
Method 2 - #IMPORT:
Start Visual C++ 6.0 and create a Win32 Console
Application. Select "An Empty Project" and click Finish.
Point to Add to Project on the Project menu and click New
to add a new C++ source file to the project. Paste the following code in the
new source file and save it:
#include <stdio.h>
// This is the path for your DLL.
// Make sure that you specify the exact path.
#import "c:\project1.dll" no_namespace
void main()
{
BSTR bstrDesc;
try
{
CoInitialize(NULL);
short st = 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 = ptr->MyVBFunction(&st);
}
catch(_com_error &e)
{
bstrDesc = e.Description();
}
CoUninitialize();
}
Compile and run your project. The message box from
Project1.DLL should appear.
The #import method can be used in a Win32 Application, a
Console Application, or in MFC as well.
Method 3 - Pure COM
Interface
Start Visual C++ and select New from the File menu. Choose
MFC AppWizard (Exe), name the project tst1, and click OK. When the MFC
Appwizard dialog box appears, select Dialog Based and click Finish. Click OK
when the next dialog box appears.
The Resource Editor is started by default. Delete all the
controls on the dialog box and add a Command button on it, retaining the
default caption "Button1".
Double-click Button1 to display the Add Member Function
dialog box. Click OK to accept the name OnButton1.
Click OLE/COM Object Viewer on the Tools menu. Select View
Typelib from the File menu and choose the Project1.dll you created earlier.
Click Open to display the ITypeLib Viewer, which contains the .idl file for
your DLL.
Copy the contents of your .idl file (contents of the right
pane) to the Clipboard. Hold the SHIFT key down while paging or scrolling from
the first character to the end of the text in the pane. Press Ctrl+C to copy
the marked text to the Clipboard.
Click New on Visual C++ File menu. Select Text File on the
New dialog box, name the file test1.idl, and click OK.
A blank text file appears. Paste the data from the
Clipboard into it and save the file.
Select Settings from the Project menu, expand the tst1 and
Source Files nodes of the tree view, and select test1.idl. Click the MIDL tab,
enter test1.h in the "Output header file name" box, and click OK.
Open tst1Dlg.cpp and add the following files to the
includes section:
#include <initguid.h>
#include "test1.h"
Click the ClassWizard on the View menu, select Ctst1App in
the Class Name box, and double-click InitInstance in the Messages box. Click
Edit Code to bring up the code for:
BOOL CTst1App::InitInstance()
Find the line:
AfxEnableControlContainer();
Add the following line before it:
AfxOleInit();
Open the ClassWizard again. On the Message Maps tab select
CTst1Dlg in the Class Name box and IDC_BUTTON1 in the Object IDs box.
Double-click BN_CLICKED in the Messages box, and click Edit Code to bring up
the code for void CTst1Dlg::OnButton1(). Replace the OnButton1() function with
the following code:
void CTst1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here.
_Class1 *pClass = NULL;
IUnknown *pUnk = NULL;
// HRESULT hr = 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 = CoCreateInstance(CLSID_Class1,NULL,CLSCTX_INPROC_SERVER,
IID_IUnknown,(void **)&pUnk);
hr = 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 = 2;
short st1;
hr = pClass->MyVBFunction(&st,&st1);
pClass->Release();
}
Compile your .exe file (F7) and run your application (F5).
Click Button1 in the dialog box. The message box from the Visual Basic DLL
appears.