Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using VB ActiveX DLLs in MSVC++

Posted on 2000-03-19
15
224 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AVI wait icons for CAnimateCtrl in Visual Studio 2008 MFC 1 152
c++ reading data from file into two dimensional array 3 117
Outlook 13 43
I could not set window to top 4 21
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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