Visual Basic 6 Black Book:Creating Code Components (OLE Automation)
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
Multithreading Code Components
You make in-process and out-of-process code components multithreaded by changing their threading model. Visual Basic uses its apartment-model threading to ensure thread safety when youre working with multiple threads. In apartment-model threading, each thread is like an apartmentall objects created on the thread work in an apartment, and they are not aware of the objects in other apartments. This threading model also eliminates conflicts when accessing global data from multiple threads by giving each apartment its own copy of that global data. You can use apartment-model threading without having to eliminate visual elements such as forms and controls, because all such Visual Basic objects are thread-safe.
You can specify the apartment threading model (instead of the single-threaded model) for in-process code components to make them multithreaded. To make out-of-process code components multithreaded, you have two options (both use the apartment thread model): thread pooling and the thread-per-object model. With thread pooling, you can specify how many threads you want available for your code components, and those threads are used in a round-robin way (that is, the first object is created on the first thread in the pool and thread allocation keeps going thread by thread, starting over with the first thread when the others in the pool are used). If you specify the thread-per-object model, each new object is created with its own thread.
SingleUse Code Components
You can also handle serialization conflicts by creating SingleUse code components, in which every new object of your code component class creates a new instance of the component. You do this by setting the classs Instancing property. The Instancing property indicates how you want your class to interact with client applications, if at all. Here are the possible values of that property:
PrivateOther applications arent allowed access to type library information about the class and cannot create instances of it. Private objects are only for use within your component.
PublicNotCreatableOther applications can use objects of this class only if your component creates the objects first. Other applications cannot use the CreateObject function or the New operator to create objects from the class.
MultiUseOther applications are allowed to create objects from the class. One instance of your component can provide any number of objects created in this fashion. An out-of-process component can supply multiple objects to multiple clients; an in-process component can supply multiple objects to the client and to any other components in its process.
GlobalMultiUseSimilar to MultiUse, with one addition: properties and methods of the class can be invoked as if they were simply global functions. Its not necessary to explicitly create an instance of the class first, because one will automatically be created.
SingleUseOther applications are allowed to create objects from the class, but every object of this class that a client creates starts a new instance of your component. Not allowed in ActiveX DLL projects.
GlobalSingleUseSimilar to SingleUse, except that properties and methods of the class can be invoked as if they were simply global functions. Not allowed in ActiveX DLL projects.
Overall, then, the reason you use code components is to make it easy to reuse your code in other applications, and well see how that works now in the Immediate Solutions section.
Immediate Solutions
Using A Code Component From A Client Application
The Testing Department is on the phone again. Wouldnt it be great to use Microsoft Excel as part of your new program? Maybe, you say. Its easy, they say, just treat Excel as a code component.
You can treat OLE automation servers like Excel as code components, adding them to your program and using them as you like. To do that, you add a reference to the code component to your program, create an object from that code component, then use the properties and methods of that object.
Lets see an example. Here, well use Microsoft Excel 7 in a Visual Basic program to add 2 and 2 and display the result in a text box. Start by adding a reference to Excel with the Project|References menu item, selecting the item labeled Microsoft Excel Object Library, and clicking on OK.
After youve added a reference to a code component, you can use the Object browser to take a look at the properties and methods in that component, as in Figure 27.1, where we are examining the Excel code library.
Figure 27.1 Using the Object browser to examine the Microsoft Excel code library.
Add a button, Command1, with the caption Add 2 + 2 using Microsoft Excel to the program now, as well as a text box, Text1, where we can display the results of the addition. When the user clicks the button, we start by creating an object, objExcel, from the Excel library (Excel will stay hidden throughout the program) using the Visual Basic CreateObject function (for other ways to create an object, see the next topic):
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
...
Now that we have our new object, we can use its properties like this, where we place 2 in both cells (1,1) and (2,1):
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Cells(1, 1).Value = "2"
objExcel.Cells(2, 1).Value = "2"
...
To add these values together, we place an Excel formula in the cell beneath these two, cell (3,1) to add those values:
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Cells(1, 1).Value = "2"
objExcel.Cells(2, 1).Value = "2"
objExcel.Cells(3, 1).Formula = "=R1C1 + R2C1"
...
Finally, we display the resulting sum in the text box in our program, Text1, and quit Excel like this:
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Cells(1, 1).Value = "2"
objExcel.Cells(2, 1).Value = "2"
objExcel.Cells(3, 1).Formula = "=R1C1 + R2C1"
Text1.Text = "Microsoft Excel says: 2 + 2 = " & objExcel.Cells(3, 1)
objExcel.Application.Quit
End Sub
Previous
Table of Contents
Next
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.
Wyszukiwarka
Podobne podstrony:
Brother Fax 910, 920, 921, 930, 931, 940, 945, MFC940 boe obietnice na co dzie bog ju dzi nas karmi i przemienia940 (2)więcej podobnych podstron