• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Using VB ActiveX DLLs in MSVC++

Anyone got some sample code to call functions etc from a DLL created from
VB ?
0
kechong
Asked:
kechong
  • 6
  • 5
  • 2
  • +2
1 Solution
 
nietodCommented:
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
 
kechongAuthor Commented:

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
 
nietodCommented:
>> 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kechongAuthor Commented:
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
 
nietodCommented:
>> 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
 
nietodCommented:
Search in the VC help for article Q118643
0
 
vishnuprasadc082099Commented:
You can try setting the project link options (Project Properties, Link Tab)
to the .LIB file.

This will add the LIB while linking.
0
 
nietodCommented:
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
 
pellepCommented:
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
 
pellepCommented:
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
 
kechongAuthor Commented:
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
 
nietodCommented:
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
 
kechongAuthor Commented:
Hehehehe, Sorry this is the first time I`ve used this site, I must of thought i was just replying !
0
 
kechongAuthor Commented:
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
 
JeromZCommented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now