Solved

Using VB ActiveX DLLs in MSVC++

Posted on 2000-03-19
15
226 Views
Last Modified: 2011-09-20
Anyone got some sample code to call functions etc from a DLL created from
VB ?
0
Comment
Question by:kechong
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2633291
There isn't much to it.  Just use __declspec(dllimport) to declare the function as imported and declare the function as extern "C" to tell the compiler/linker that the function name is not decorated, like

_declspec(dllimport) extern "C" int Increment(int i);

Do you have an example function you want to import?
0
 

Author Comment

by:kechong
ID: 2633905

Thanks for you reply :-)

I've created a ActiveX DLl with VB6 and it has a function

Function Add2DB(Byval X as string, Byval Y as string)
.....
End Function

All I want to do is some how call that code, by passing some strings to it in MSVC and letting the DLL do the rest.

_declspec(dllimport) extern "C" int Increment(int i);

How does it know which DLL to get code from ?

I was playing anout with the Class wizard thing, and it allows u to import DLLs and creates a .cpp and .h files which has all the function names and arguments etc, but when I create instances of it, then call it, nothing seems to happen.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2634067
>> How does it know which DLL to get code from ?
When you create the DLL in VB you should be creating an import library.  This is a static library (.lib) that has the same name as the DLL.  Do you have such a file?  If so, you just link your C++ application to this .lib file.   (Add the .lib to the project using the "add files to project" option.)   Then you don't have to worry about which DLL to use, it is taken care of automatically.

If you don't have an import library (.lib) you need to link explicitly to the DLL, for this you will need to use LoadLibrary() to load the dLL and then use GetProcAddress() to find the procedure in the DLL.  But this is more work and should be avoided if possible, so look for the import library.

Note that C++ can't manipulate VB strings directly.   I believe you have to use a type called BSTR for this.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kechong
ID: 2634349
I found the .Lib file and added it to the project. It doesnt seem to make any difference.

_declspec(dllimport) extern "C" int add2DB(BSTR x, BSTR y);

Do I need to use the class wizard ?

Is it normal for the app, to continue ( no err msg ) if the call didnt work ?

Do u know of an example anywhere ?

Thanks
K-D
0
 
LVL 22

Expert Comment

by:nietod
ID: 2634504
>> Do I need to use the class wizard ?
No.

>> Is it normal for the app, to continue
>> ( no err msg ) if the call didnt work ?
No.  If the app isn';t crashing, the call is being made.  It just might not be doing what you expect.

>>  It doesnt seem to make any difference.
It what sense?  What is it doing wrong?

If you didn't have a .lib before, then the program couldn't have linked, so you couldn't have even run the program before.  With the lib you should be able to get the code to compile and link.  You should be able to run it.  That should be a difference.  Did you get that difference?

>> Do u know of an example anywhere ?
This largely IS the example.  Its not like it is that hard to do.  However, you do need to handle parameters correctly, and I do not know how you handle VB strings in C++.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2634516
Search in the VC help for article Q118643
0
 

Accepted Solution

by:
vishnuprasadc082099 earned 30 total points
ID: 2635162
You can try setting the project link options (Project Properties, Link Tab)
to the .LIB file.

This will add the LIB while linking.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2635852
vishnuprasadc, do you really think that is an answer?  I already explained how to link to the .lib file and he has done so and Kechong rejected my answer anyways.  
0
 
LVL 4

Expert Comment

by:pellep
ID: 2636500
A DLL created in VB is not a 'normal' windows dll, but an ActiveX dll (COM-object). The easiest way to access this dll, if your using msvc++6.0, is via the typelib. Using the class-wizard in msvc++, choose 'add class' and then 'add class from typelib'.
VB includes the typelib in the .dll file, so when you're asked to point out the typelib, select your vb-dll (the same goes if your making ActiveX exe). Class wizard will generate a wrapper class that contains the functions you defined as public in the .class-file in your vb-dll. It will do this using the IDispath interface that all COM - automation (VB) objects expose.
0
 
LVL 4

Expert Comment

by:pellep
ID: 2636534
i forgot one thing.
You will have to instanciate your ActiveX-dll before using it.

Use CLSIDFromProgID() to get the CLSID
and then CreateDispatch() to create the object instance.

CLSID clsid;
COleException e;
m_wrapper = new _wrapper;//_wrapper would be the wrapper class
if (CLSIDFromProgID((OLESTR("loggerapi.db"), &clsid) != S_OK)
{
MyErrExit("Could not retreive CLSID for loggerdbapi.db");
return FALSE;
}
if (!m_database->CreateDispatch(clsid, &e))
{
MyErrExit("CreateDispatch() failed to connect to loggerdbapi.db");      return FALSE;
}
0
 

Author Comment

by:kechong
ID: 2636562
Hi Pellep,

I 've done what you suggested and as I`ve said above somewhere, it creates the .cpp and .h files which is really cool ! But when I create instances of the class and call the method, nothing happens.
e.g.

_Class1 x;
x.add2DB("hjg","hjghj");

It compile fine but when I run it, it does not crash or work ?

In VB I`ve referenced the dll i`ve created and it works fine, its just getting it to work in msvc.

----

nietod, Sorry if it sounds like I "rejected" your help, but I do appreciate it!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2637268
But you did reject it.  Then you gave vishnuprasadc the points for an "answer" to an problem that I had already told you how to solve.  
0
 

Author Comment

by:kechong
ID: 2637704
Hehehehe, Sorry this is the first time I`ve used this site, I must of thought i was just replying !
0
 

Author Comment

by:kechong
ID: 2637749
If anyone else with the same problem still didnt "get it" look at:

Q194873
 
http://support.microsoft.com/support/kb/articles/q194/8/73.asp
0
 

Expert Comment

by:JeromZ
ID: 2637849
well...know of any books which covers this...cos the example given is simple and i am not familiar with VC++....
got an access violation error when i have MAPI in my VB dll..wonder why?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Starting a function in a c++ application with a TCP command from C# 4 83
Embarcadero C++ Builder XE2 TDateTime 8 80
C++ mouse_event mouse look 7 111
Gaming Software 1 29
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question