‘tirgul’ # 7 enterprise development using visual basic 6.0 autumn 2002 tirgul #7

27
‘Tirgul’ # 7 Enterprise Enterprise Development Development Using Visual Basic Using Visual Basic 6.0 6.0 Autumn 2002 Autumn 2002 Tirgul # Tirgul # 7 7

Upload: albert-goodman

Post on 12-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Enterprise Enterprise Development Development

Using Visual Basic 6.0 Using Visual Basic 6.0 Autumn 2002Autumn 2002

Tirgul #Tirgul #77

Page 2: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

OObjectivesbjectives

• Brief on COM

• COM and Classes

• Creating Objects – ActiveX DllActiveX Dll• Registering an Object

• Version Compatibility issues

Page 3: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

AbstractAbstract• In the world of the enterprise, employers want

that something more then just Forms and Controls

• That something is called COM– It's a way of organizing your code so it

can be used over and over again. – It's a method of helping create a bug-free

development environment. – Just after making your life a little harder,

makes it lot easier.

Page 4: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

What is COM?What is COM?

• you're already a COM programmer!– Every time you set the Text

property of a Text Box, you're using COM.

• COM is a method of communication

Page 5: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

COM is a method of communication COM is a method of communication

• Imagine a television remote control– You press button one and the television

tunes into Channel 1, 2… – You press the power button and the

television turns itself off. • You just press the button and it jumps into

action.

Page 6: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

COM is a method of communication COM is a method of communication

• That’s applies to the world of programming. • You don't really know what happens when you

change the Text property of your Text Box. • Under the covers, it's probably making two-dozen API

calls – but to the end user, it just displays a certain piece of text in the box.

Page 7: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

COM is a way of reusing code COM is a way of reusing code

• You can use Com Object again and again anywhere.– A COM widget could be accessed from any

program at any time. (Visual Basic, Excel C++ Program)

• So COM is all about creating 'widgets' that can be reused.

Page 8: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

COM is based around real-life objects COM is based around real-life objects

• After COM have been made, it’s easy to use.• Add a customer

– Instead of adding lots of data access code, validation algorithms, and weighty database DLLs to your application – wouldn't it be easier to run something like

Customer.AddCustomer.Add

Page 9: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Classes -> COMClasses -> COM

• All of our work will be surrounding the creation of a class – which could later be turned into a real life COM object

Page 10: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Classes check listClasses check list

• Variables

• Properties (Let/Get) (hopefully)..

• Subs, Functions, Parameters

• Collection

• What else?

– Enumeration

– Events – Not in the scope

Page 11: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

create a real-life COM object create a real-life COM object • ActiveX DLL – A .DLL program that contains classes.

• ActiveX EXE – A .EXE program that contains classes.

• ActiveX Control – A project that allows you to create controls in your toolbox.

• ActiveX Document EXE – A program that runs on a Web page, in .EXE format.

• ActiveX Document DLL – As above, but .DLL format.

Page 12: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

My first object!My first object!

• We have discussed objects as controls in VB (Textbox, ComboBox) - you should be remembering these by now!)

• When you make a control, it has a user interface (that means you can visually see something on the screen, such as the textbox itself

• Another kind of object is one that doesn't have a user interface, and we call this a DLL (or Dynamic Link Library if you want to impress your friends).

Page 13: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

ActiveX DllActiveX Dll

• Click File, New Project and select ActiveX DLL:

• make sure that the Instancing property is set to 5 – MultiUse,

• Allows us to compile the DLL and expose the class module to other programs (such as the object browser.

Page 14: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Build the DLLBuild the DLL

• Save the project, and a click File, Make Project1.DLL.

• You've just complied your first Dll object!

Page 15: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Testing..Testing..

• Add another project - File, Add Project, Standard EXE.

• Because a DLL is not a control, we need to make a reference to it (because we are not loading any kind of user interface)

Page 16: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Testing..Testing..

• Click File, References and scroll down and select Project1:

Page 17: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Browsing the ObjectBrowsing the Object

• press F2 to load up the object browser and select Projec1 from the combo box.

• Check the Methods..

Page 18: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

ExplanationExplanation

• An ActiveX DLL's code is executed within the main program's address space.

• It behaves as if the class was created within the main program's code.

• Because the code lies inside the program's address space, calling methods is very fast.

Page 19: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Registering a componentRegistering a component

• when you come to distribute your DLL to another machine and it doesn't work..

• Each DLL (or component) on your machine has to be registered (including controls). Once registered, an entry in to the registry is made.

Page 20: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Using Regsvr32 ToolUsing Regsvr32 Tool• Microsoft tool for registring DLL’s

and OCX’s• Simply Drag the Object into the

TextBox

Page 21: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Main subMain sub

• In the module you can add a main sub:

Sub main

‘Add code that will run when the ‘component is created

End Sub

Page 22: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

CompatibilityCompatibility

• Problem: – compile the ActiveX DLL,– then compiled a test program that used our

DLL. – Then we recompiled our DLL – something

you usually do after you make changes. • Suddenly, our test program no longer worked!

Page 23: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

CompatibilityCompatibility

• Open up your project• Click 'Project Properties' • Click the 'Component' tab

Page 24: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

Version CompatibilityVersion Compatibility

• No Compatibility – When compiled, your COM component gets assigned a new 'signature'. Programs looking for older 'signatures' (the previous version of the DLL) – simply flop

• Project Compatibility – When compiled, your COM component is assigned a new signature – and still, any using-applications still flop. The only change here is that 'big' differences between your current project and a previous DLL project are highlighted as you compile.

• Binary Compatibility – When compiled, your application attempts to keep the signature of a previously compiled DLL, thus ensuring any applications using it don't magically turn into the Blue Screen of Death. However if the differences between your previously compiled DLL and your to-be-compiled DLL are too great, a new signature must be assigned

Page 25: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

ActiveX DLL Creation SummaryActiveX DLL Creation Summary

1. Determine the features your component will provide.

2. Determine what objects are required to divide the functionality of the component in a logical fashion

When you create a new ActiveX DLL, the steps you’ll generally follow are these:

Page 26: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

ActiveX DLL Creation SummaryActiveX DLL Creation Summary

3. Design any forms your component will display.

4. Design the interface — that is, the properties, methods, and events — for each class provided by your component.

5. Create a project group consisting of your component project and a test project.

Page 27: ‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7

‘Tirgul’ # 7

ActiveX DLL Creation SummaryActiveX DLL Creation Summary

6. Implement the forms required by your component

7. Implement the interface of each class.8. As you add each interface element or

feature, add features to your test project to exercise the new functionality.

9. Compile your DLL and test it with all potential target applications.