Solved

Using VB ActiveX DLLs in MSVC++

Posted on 2000-03-19
15
221 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
Comment Utility
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
Comment Utility

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

Author Comment

by:kechong
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Search in the VC help for article Q118643
0
 

Accepted Solution

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

This will add the LIB while linking.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now