How to create a library

Hi!

This is mostly a question for C++ programmers that have wroked with VB... (I guess)

In VB you can add a reference to a library (.tlb, .olb, .dll... whatever)
These libraries seem to be (to me at least) an ordinary class with member functions and variables (properties and methods)
Supose I'd like to have an object called CObjHello, and some properties like CObjHello.MyName... and methods like CObjHello.ShowMsgBoxHello().... (of course I don't want to do this, but it's just and example)

How to create such a library in C++ to use with VB?... Do I need MFC, or is this possible without MFC?
What should I do or what are the steps?

Thanks in advance
LVL 2
trilloAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
andlaConnect With a Mentor Commented:
This text i found in an tutorial "From the Project menu, choose the Add to Project menu item, and then choose the Components and Controls submenu item.
In the Components and Controls Gallery dialog box, double-click the Developer Studio Components folder.
After the Components and Controls Gallery dialog box is refreshed with data, double-click the ATL Proxy Generator icon"

Yes. Generally you choose you new object.

I'm going to bed now and i'm back in a couple of ours.
0
 
nil_dibCommented:
1. you don't need MFC to do this.

2. >These libraries seem to be (to me at least) an ordinary class with member functions and variables
those libraries can hold a couple of classes or even plain non OO code!
 
3. there is no special way to write a library, it depends on your compiler settings whether you want to compiple your code as a dll, tlb, ocx ...

e.g.
if your writing a dll, you have to export the methods/functions which should be accessible from VB:

// CObjHello.h
// the class you want to export methods from
class CObjHello
{
   void ShowMsgBoxHello() { //show something };
}


#include "CObjHello.h"

CObjHello  gObjHello;  // the CObjHello object

//  Function DllMain
BOOL WINAPI DllMain(HANDLE hModule,
                        DWORD  ulReason,
                          LPVOID lpReserved)
{
      switch(ulReason)
      {
            case DLL_PROCESS_ATTACH:
                  break;
            case DLL_THREAD_ATTACH:
            case DLL_THREAD_DETACH:
            case DLL_PROCESS_DETACH:
                  break;
      }
      return TRUE;
}


// EXPORT
__declspec(dllexport) void ShowMsgBoxHello( )
{
     gObjHello.ShowMsgBoxHello( );
}



nil_dib








0
 
yodanCommented:
That is only a small corner of the point. A .tlb, .olb, library file is  what is named a TypeLib for a COM (Ms Common Object Model) interface.
If you really want to export classes you may be interested to learn about COM.
Other ways can be: using C dlls and declaring functions in VB as is done for the Windows API; exporting entire C++ classes or only methods (but I believe you can't easily import)

You can create those COM objects in VB too. 1)define interface, 2) define server classe(s)

3) use interface in client while creating object with the implementation name:
'
Dim Var As MyCOMInterface
Set Var = New MyImplementServer
'
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
trilloAuthor Commented:
Sorry, but I already knew ho to build those DLL's and I actually use them all the time. What I really want is having a DLL or a template library with OBJECTS, and NOT ONLY FUNCTIONS.
I'd like to use THE OBJECTS within VB
0
 
yodanCommented:
Right, if you want objects one shortcut is using COM/ActiveX, something I believe you know (from your answer, trillo).
The little I know is that you must restrict the .idl file to some rules. I guess the rules imply: the data types recognizable by VB COM engine and the methods return the correct type (I think  only one type is available).
Research in the MSDN and if you have access to, the book: "Programming Distributed Applications with COM and Microsoft Visual Basic 6.0"
MSPress, Ted Pattison.

There they explain some of restrictions for VB. I could search there and give you an answer, but now no time. If you can have an answer  at monday, ask me, I'll find the topics and present them to you.
Meantime, good luck!
0
 
trilloAuthor Commented:
Thanks a lot
0
 
andlaCommented:
If i may help you i can tell you how to build an component using ATL without MFC. You can access method from VB or VBScript or Javascript. But you must tell me first what version of MVC you are using. Because you must have version from 4.2 and above.
0
 
trilloAuthor Commented:
Very nice!
I'm using VC++ 5.0. I realize that this question needs more points than 80, so if you do this for me, you'll get more points, and even more, I'll be very thankful.
0
 
andlaCommented:
Ok points are not so important. But i would be grateful if you help me sometime :-)

When you start a new project you probebly have a application wizard called ATL COM appwizard. If you use the defaults of this wizard witch is probely the same as VC6.0 you will have the code for a basic ATL project. If you are not familiar with ATL it's stepping in MFC for the first time. You don't start knowing everthing just watch for the methods where the action are.

Now when you compile this program it doesn't do much but notice that the compiler tells you that the component are registered. This is the common registry used by window. A piece of UUID or CLSID (not sure) are entered in the registry that identifies you control and tells other program where to find it.

Next step is to use 'insert new atl object' You insert a new 'control' give it a name and if there is any option to support connectionpoint please use it. You can play with the other option doesent matter so much the program still works when you compile.

When you compiled this far you will have to do something that differs from VC6.0. In 6.0 you can implement connectionpoint just by rightclick the class. In 5.0 you must use 'project->Add to project-> components and control' There you must find a tool to implement connection point i'm not sure what the name was, but if you can't find it please give me a comment and i help you search for it.

When you come this far i hold for a moment until you tell how everything went.

Good luck!
0
 
trilloAuthor Commented:
everithing OK except last step:

> In 5.0 you must use 'project->Add to project-> components and control' There you must find a tool to implement connection point i'm not sure what the name was, but if you can't find it please give me a comment and i help you search for it.

The only thing I found was my own object I just created... is that what I should select?
0
 
trilloAuthor Commented:
Adjusted points to 110
0
 
trilloAuthor Commented:
Well, I think I already got it... I saw the "Polygon" tutorial in the VC++ documentation and I guess that´s where you were going, right?
I must say that ATL is much more complex than creating an ordinary DLL.
I created the "Polygon" example successfully, (although I must say that even it worked in VB, my VB environment crashes every time the object gets destroyed... there must be some kind of bug when destroying the object)

I'll keep finding out by my own about ATL... I was lost and din't even know where to start, but what I needed was that big push you gave me, Thanks a lot!! I increased the points, hoping it will reward your effort.
0
 
andlaCommented:
:-)

I was thinking like you but this ATL is important if you want to make the components small. If you include MFC it will increase in size dramatically. A very powerful tool that aid you when programming especially with the wizard is a textdiff tool like windiff or csdiff (i use that). You can see exactly what the wizard did and draw your knowledge from there. When you for example add a message handler to you project you can se that the code is updated in three places. One change in the IDL file for the interface to the outside world, one in the class .h file and one in the .cpp file of that class.

Oops i'm not sure if the connection points was nessesary. I think it's nessesary for events.

What you must do now is to add a properties. What you do is to highlight the interface and select a property. If you want to tranfere string between VB and VC++ you can use BSTR. You probely se that two checkboxes marking GET and PUT are marked in the wizard. Use them to transfere the string between the control and the VB.

I have not done this in VB myself but i think it should not be a big problem. Both VB and VC++ has wizard to implement activex components. If you watch you projectfiles you will probebly (i'm not sure if this was implemented in VC6.0) se a .htm file with the control. If you do not se that you can use the OLE view tool and create a .htm code there for the object.

If you created the property with the GET and PUT properties you actually can communicate between a scripting host and the control. But the BSTR is very tricky. It doesn't support direct convert between char[] and BSTR therefor you must implement two functions that i found. But first i will let you catch up and se if something went wrong.

Good luck!
0
 
trilloAuthor Commented:
The most of what you said was on the tutorial (connection points, Get and Put properties, a HTML test script...) and I already know how to handle BSTR strings because I use them with ordinary DLLs (passing strings, returning strings, passing string buffers to be filled, ...).

Since the difficult part is understanding interaction between all interface objects and maps, I need to gain experience and this is done only with one way: programming and more programming!

Cheers and thank you again!
0
 
andlaCommented:
The hardest thing is to connect what you read with coding knowledge. What you do (i think) is using IDispatch to use the automation feature to manipulate scripts. I'm not sure if this is what you want? But you can transfer you own class instead of the BSTR i showed you. If you want to use it with ASP you must encapsulate it with the VARIANT.

I hope this helps. :-)
0
All Courses

From novice to tech pro — start learning today.