Calling VB ActiveX DLL from MFC applications(Newbie question) 
Author Message
 Calling VB ActiveX DLL from MFC applications(Newbie question)

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



Sun, 26 Aug 2001 03:00:00 GMT  
 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



Sun, 26 Aug 2001 03:00:00 GMT  
 Calling VB ActiveX DLL from MFC applications(Newbie question)
Thanks for a very nice and clear answer.
I now ask for one more help - hope you could help me out

I have a COM DLL say Test.DLL in which it has class cMain
and method 'MainMethod()'

I used ClassWizard to generate an ActiveX Event and it created
_cMain.CPP & _cMain.h.I suppose this doesn't requite the use of #import
directive.

Now can I call this by instantiating _cMain class.

Say I have this call :

{
 _cMain cObj;
 cObj.MainMethod();

Quote:
}

It doesn't seem to work.If I do:

{
 _cMain cObj("Test.cMain");
 cObj.MainMethod();

Quote:
}

The compiler complains.So I am not sure about the call syntax.
Could you  pls. help out.
I really appreciate your help and advise.

Thanks
Neil

Quote:

>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();
>}

>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


>>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



Mon, 27 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. newbie question - VB ActiveX DLL calling

2. MFC app calling VB ActiveX DLL

3. Calling a VB ActiveX dll from MFC

4. C++ MFC ActiveX / VB Newbie question

5. Erratic behaviour of VB application while calling MFC dll

6. Creating c dll calling vb ActiveX dll

7. Show modeless form in VB ActiveX DLL from C++ MFC DLL

8. MFC DLL needs to be called from Non-MFC VB.

9. Bad DLL calling convention in my MFC (for VB) DLL

10. Calling VB ActiveX Control from MFC

11. Atl NT service calling VB ActiveX dll had problem when clean-up

 

 
Powered by phpBB® Forum Software