Уверен что вставится коряво, но все же попробую... Knowledge Base Articles HOWTO: Access a Visual Basic ActiveX DLL from Visual C++ Q194873 -------------------------------------------------------------------------------- The information in this article applies to: Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0 Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0 Microsoft Visual C++, 32-bit Learning Edition, version 6.0 -------------------------------------------------------------------------------- SUMMARY This article shows three ways to access a Visual Basic ActiveX DLL from a Visual C++ 6.0 executable. MORE INFORMATION 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. Steps to Create the Visual Basic Server 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 .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 .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 // 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
Ответить
|