How to import IObjectSafety to the VB Control

Source: Internet
Author: User
From: http://support.microsoft.com/default.aspx? SCID = KB; en-US; 182598 How to Implement IObjectSafety in Visual Basic controlsview products that this Article applies.

Article ID : 182598
Last Review : August 10,200 6
Revision : 5.0
This article was previusly published under q182598summary

This article describes how to implementIObjectSafetyInterface in Visual Basic controls to mark the controls safe for scripting and initialization. By default, Visual Basic controls use component category entries in the Registry to mark the control safe for scripting and initialization. ImplementingIObjectSafetyInterface is the preferred method. This article contains all the code that is required to implement this interface in Visual Basic controls.

Please keep in mind that a control shoshould only be marked as safe if it is, in fact, safe. this article does not describe the details of marking controls as safe for scripting and initialization; it simply demonstrates how to do it in code. please refer to the Internet client software development kit (SDK) Documentation for a detailed description of this. see "Safe initialization and scripting for ActiveX controls" under the component development section.

Back to the top

More information

Warning: One or more of the following functions are discussed in this article: varptr, varptrarray, varptrstringarray, strptr, and objptr. these functions are not supported by Microsoft technical support. they are not supported ented in the Visual Basic documentation and are provided in this Knowledge Base Article "as is. "Microsoft does not guarantee that they will be available in future releases of Visual Basic. for additional information about these functions, click the article number below to view the article in the Microsoft Knowledge Base:

199824 Http://support.microsoft.com/kb/199824/EN-US) How to get the address of variables in Visual Basic

The following steps implements strate how to create a simple visual basic control and mark it safe for scripting and initialization.

1. Create a new folder where you can save all files that you create in this example.
2. Get the OLE Automation Type Library generator from the Visual Basic 6.0 CD-ROM. to do this, copy all four files from the \ common \ tools \ VB \ unsupprt \ typlib \ folder to your project folder.Note: Copy all the files from \ VB5.0 \ tools \ unsupprt \ typlib \ folder in Visual Basic 5.0.
3. Copy the following text into notepad, and save the file in the project folder as objsafe. odl:

[UUID (C67830E0-D11D-11cf-BD80-00AA00575603), helpstring ("VB IObjectSafety interface"), version (1.0)] library iobjectsafetytlb {importlib ("stdole2.tlb"); [UUID (CB5BDC81-93C1-11cf-8F20-00805F2CD064 ), helpstring ("IObjectSafety interface"), odl] interface IObjectSafety: iunknown {[helpstring ("unknown")] hresult getinterfacesafetyoptions ([in] Long riid, [in] Long * unknown, [in] Long * pdwenabledoptions); [helpstring ("setinterfacesafetyoptions")] hresult Merge ([in] Long riid, [in] Long dwoptionssetmask, [in] Long dwenabledoptions );}}

4. At a command prompt, useCD <path>Command to move to the project folder, and type the following command to generate a. TLB file:

Mktyplib objsafe. odl/TLB objsafe. TLB
5. From Visual Basic, create an ActiveX control project. InPropertiesList, change the name of the projectIobjsafetyAnd the name of the controlDemoctl. Put a commandbutton named test on the control. In the click event handler of the specified test, put a msgbox "test" statement.
6. OnProjectMenu, clickReferences, Browse to and add objsafe. TLB, which you created earlier.
7. Add a new module to your project with the following code, and name the module bassafectl:

Option explicit public const iid_idispatch = "{region}" Public const iid_ipersiststorage = _ "{region}" Public const iid_ipersiststream = _ "{region}" Public const region = _ "{region} "Public const principal = & H1 public const principal = & H2 public const e_nointerface = & h80004002 public const e_fail = & h80004005 public const max_guidlen = 40 public declare sub copymemory lib" Kernel32 "alias" rtlmovememory "_ (pdest as any, psource as any, byval bytelen as long) Public declare function stringfromguid2 lib "ole32.dll" (rguid AS _ Any, byval lpstrclsid as long, byval cbmax as integer) as long public type udtguid data1 as long data2 as integer data3 as integer data4 (7) as Byte end type public m_fsafeforscripting as Boolean public m_fsafeforinitializing as Boolean sub main () m_fsafeforscripting = true m_fsafeforinitializing = true end sub

8. From project properties, change the startup objectSub mainTo execute the sub main above. UseM_fsafeforscriptingAndM_fsafeforinitializingVariables to specify the values of safe for the scripting and/or initialization variables.
9. Open the code window of your control. Add the following line of code to the declaration section (right after option explicit or as the first ):

Implements IObjectSafety

10. Copy the following two procedures to your controls code:

Private sub partition (byval riid AS _ long, pdwsupportedoptions as long, pdwenabledoptions as long) dim RC as long dim rclsid as udtguid dim IID as string dim biid () as byte pdwsupportedoptions = partition or _ partition if (riid <> 0) Then copymemory rclsid, byval riid, Len (rclsid) biid = string $ (max_guidlen, 0) rc = stringfromguid2 (rclsid, varptr (biid (0), max_guidlen) rc = instr (1, biid, vbnullchar)-1 IID = left $ (ucase (biid), RC) select case IID case iid_idispatch pdwenabledoptions = IIF (m_fsafeforscripting, _ blank, 0) Exit sub case iid_ipersiststorage, optional, _ required pdwenabledoptions = IIF (optional, _ blank, 0) exit sub case else err. raise e_nointerface exit sub end select end if end sub private sub iobjectsafety_setinterfacesafetyoptions (byval riid AS _ long, byval dwoptionssetmask as long, byval dwenabledoptions as long) dim RC as long dim rclsid as udtguid dim IID as string dim biid () as byte if (riid <> 0) Then copymemory rclsid, byval riid, Len (rclsid) biid = string $ (max_guidlen, 0) rc = stringfromguid2 (rclsid, varptr (biid (0), max_guidlen) rc = instr (1, biid, vbnullchar) -1 IID = left $ (ucase (biid), RC) Select case IID case iid_idispatch if (dwenabledoptions and dwoptionssetmask) <> _ interfacesafe_for_untrusted_caller) Then err. raise e_fail exit sub else if not m_fsafeforscripting then err. raise e_fail end if exit sub end if case iid_ipersiststorage, iid_ipersiststream, _ iid_ipersistpropertybag if (dwenabledoptions and dwoptionssetmask) <> _ timeout) Then err. raise e_fail exit sub else if not m_fsafeforinitializing then err. raise e_fail end if exit sub end if case else err. raise e_nointerface exit sub end select end if end sub

11. OnFileMenu, save your project and files. Make an ocx file from your project. Your control now implementsIObjectSafetyInterface. to test it, insert the control in an. htm file.

Back to the top

References

Mktyplib.exe is an old tool that previusly shipped with the platform software development kit (SDK) that comes with Microsoft Visual Studio 6.0. for information on how to install the Platform SDK that comes with Visual Studio 6.0, see the following Microsoft Web site:

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/sdkintro/installing_the_platform_sdk_with_visual_studio.asp (Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/sdkintro/installing_the_platform_sdk_with_visual_studio.asp)

The current platform SDK does not come with the mktyplib.exe tool. For information about what Microsoft products ship the Type Library compiler (mktyplib.exe), see the following Microsoft DLL help database and search on the file name mktyplib.exe

Http://support.microsoft.com/dllhelp (Http://support.microsoft.com /? SCID = http % 3A % 2f % 2fsupport.microsoft.com % 2 fservicedesks % 2 ffileversion % 2fdllinfo. asp)

For information about how to invoke mktyplib, see the following Microsoft Web site:

Http://www.microsoft.com/msj/0297/visualprog/visualprog0297.aspx Http://www.microsoft.com/msj/0297/visualprog/visualprog0297.aspx)

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

161873 Http://support.microsoft.com/kb/161873/EN-US) How to mark MFC controls safe for scripting/initialization 143258 Http://support.microsoft.com/kb/143258/EN-US) How to Create constants and DLL declarations in a Type Library 131105 Http://support.microsoft.com/kb/131105/EN-US) Sample: typebld: How to Use icreatetypelib and icreatetypeinfo

For more information aboutIObjectSafetyInterface, see the following Microsoft Web site:

Http://msdn.microsoft.com/workshop/components/com/reference/ifaces/iobjectsafety/iobjectsafety.asp Http://msdn.microsoft.com/workshop/components/com/reference/ifaces/iobjectsafety/iobjectsafety.asp)

For more information about safe initialization and scripting for ActiveX controls, see the following Microsoft Web site:

Http://msdn.microsoft.com/workshop/components/activex/safety.asp Http://msdn.microsoft.com/workshop/components/activex/safety.asp)

For more information about developing web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Http://msdn.microsoft.com/ie/ Http://msdn.microsoft.com/ie)

Http://support.microsoft.com/iepHttp://support.microsoft.com/iep)

Back to the top

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.