
Calling VB ActiveX DLL from MFC applications(Newbie question)
Assume you have a VB COM (ActiveX) DLL project called "MyProject" that
exports two variables (let's say, Var1 as Long and Var2 as String) and one
method (MyMethod), and the VB class is called MyVBClass.
So, in VB you would access this class and its members by using syntax
similar to
Sub Main
Dim objMyClass as MyVBClass
Set objMyClass = new MyVBClass
objMyClass.MyMethod
Debug.Print objMyClass.Var1, objMyClass.Var2
End Sub
The same functionality can be had in VC++ by using what are known as "smart
pointers". Example:
#import "MyProject.dll" no_namespace
void main()
{
char buf[64];
CoInitialize(NULL);
_MyClassPtr clsptr("MyProject.MyClass");
clsptr->MyMethod();
long var1 = clsptr->Var1;
_bstr_t var2 = cldptr->Var2;
sprintf(buf, "%d %s\n", var1, (char *)var2);
OutputDebugString(buf);
CoUninitialize();
Quote:
}
Using the #import directive tells VC++ to look up the type library contained
in the specified file (in this case, your DLL), and create C++ include
headers from it. These headers are automatically included in when this
source file is combined. They also declare things like the _MyClassPtr type,
and helper methods that make accessing the object and its properties more
like VB than C++. See the help that comes with Visual Studio for more
information (search on "smart pointers" and "import"). The calls to
CoInitialize and CoUninitialize make the COM libraries available for calling
by non-COM programs. OutputDebugString simply writes a string to the debug
window (much like VB's Debug.Print).
If you are new to VC++ and/or programming in general and this just confused
you more, I would suggest taking a more formal course of education specific
to C++, then VC++, because if the first thing you learned to program was VB,
then you have some programming habits to unlearn as well as a whole 'nother
understanding of compiled languages to learn. :)
Hope this helps,
Greg
Quote:
>HI
>I need to call a an ActiveX DLL (written in VB5)
>from an MFC application.
>I am an absolute newbie to VC++ and haven't a clue
>on how to do it.I have tried inserting into project and using
>Classwizard and similar stuff but the call doesn't work.
>Could you pls. help me out??
>TIA