How to Implement the IObjectSafety interface of ActiveX controls in VB

Source: Internet
Author: User

How to Implement the IObjectSafety interface of ActiveX controls in VB

--------------------------------------------------------------------------------

Overview

This article describes how to implement the IObjectSafety interface of the control in VB to indicate that the control is secure in script and initialization. The default Processing Method of the VB Control is to register the component class in the Registry to identify its security, but implementing the IObjectSafety interface is a better method. This statement includes allCode.

Note that the control can be identified as "secure" only when it is actually secure ". This article does not discuss how to ensure control security. For more information, see "Safe initialization and scripting for ActiveX controls" in Internet client software development kit (SDK ", it is in the component development column.

Related information:

<A warning that may not matter is omitted here>

The following example shows how to create a simple VB Control and identify it as Script Security and initialization security step by step.

Create a folder to store the files generated in this example.

Obtain the production tool of OLE Automation class library from VB CD-ROM. Copy all the content in the \ common \ tools \ VB \ unsupprt \ typlib \ directory of the VB installation disc to the project folder created earlier.

Copy the following content to notepad and save it to the preceding folder named 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 ("getinterfacesafetyoptions")]

Hresult getinterfacesafetyoptions (

[In] Long riid,

[In] Long * pdwsupportedoptions,

[In] Long * pdwenabledoptions );

[Helpstring ("setinterfacesafetyoptions")]

Hresult setinterfacesafetyoptions (

[In] Long riid,

[In] Long dwoptionssetmask,

[In] Long dwenabledoptions );

}

}

Switch to the project folder at the command line prompt, and enter the following command to create a. TLB file:

Mktyplib objsafe. odl/TLB objsafe. TLB

Create an ActiveX control project in VB. Modify properties, name the project iobjsafety, and name the control democtl. Place a button on the control and name it "test". Add a code msgbox "test" to its click event ".

Open "Project-> reference" in the menu, click "Browse", find the created objsafe. TLB, and add it to reference.

Add a new module named bassafectl and add the following code to it:

Option explicit

Public const iid_idispatch = "{00020400-0000-0000-c000-000000000046 }"

Public const iid_ipersiststorage = _

"{0000010a-0000-0000-c000-000000000046 }"

Public const iid_ipersiststream = _

"{00000109-0000-0000-c000-000000000046 }"

Public const iid_ipersistpropertybag = _

"{37d84f60-42cb-11ce-8135-00aa004bb851 }"

Public const interfacesafe_for_untrusted_caller = & H1

Public const interfacesafe_for_untrusted_data = & 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 _

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

Change the startup object to sub main in the project properties to ensure that the above code is executed. The values of m_fsafeforscripting and m_fsafeforinitializing variables specify the Script Security and initialization Security values respectively.

Open the control code window and add the following code in the Declaration section (if an option explicit statement exists, ensure that the Code is placed after it ):

Implements IObjectSafety

Copy the following two process codes to the control code:

Private sub iobjectsafety_getinterfacesafetyoptions (byval riid _

Long, pdwsupportedoptions as long, pdwenabledoptions as long)

Dim RC as long

Dim rclsid as udtguid

Dim IID as string

Dim biid () as byte

Pdwsupportedoptions = interfacesafe_for_untrusted_caller or _

Interfacesafe_for_untrusted_data

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 ,_

Interfacesafe_for_untrusted_caller, 0)

Exit sub

Case iid_ipersiststorage, iid_ipersiststream ,_

Iid_ipersistpropertybag

Pdwenabledoptions = IIF (m_fsafeforinitializing ,_

Interfacesafe_for_untrusted_data, 0)

Exit sub

Case else

Err. Raise e_nointerface

Exit sub

End select

End if

End sub

Private sub iobjectsafety_setinterfacesafetyoptions (byval riid _

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) <> _

Interfacesafe_for_untrusted_data) 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

after saving, compile the project into an ocx file. Now the control has implemented the IObjectSafety interface. Add this control to .htm and try it.

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.