Article ID: 194873
Article Last Modified on 6/2/2005
Public Function MyVBFunction(x As Integer) As Integer
MsgBox x
End Function
AfxOleInit();
void CAboutDlg::OnOK()
{
short st = 2;
short st1;
_Class1 p;
p.CreateDispatch("Project1.Class1");
st1 = p.MyVBFunction(&st);
CDialog::OnOK();
}
#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();
}
#include <initguid.h>
#include "test1.h"
BOOL CTst1App::InitInstance()
Find the line:
AfxEnableControlContainer();
Add the following line before it:
AfxOleInit();
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();
}
Keywords: kbhowto kbinfo kbcode KB194873