Solved

Using VB ActiveX DLLs in MSVC++

Posted on 2000-03-19
15
222 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Better understanding on C++ Class serialization and formats 9 59
Exception thrown at 0x00007FFD5BC81F28 7 38
Create a path if not exists 7 68
Beginner to Unreal Engine 4 5 57
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

930 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

13 Experts available now in Live!

Get 1:1 Help Now