|Last Review||:||September 1, 2005|
Please keep in mind that a control should 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.
The following steps illustrate 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 CD-ROM. To do this, copy all four files from the CommonToolsVBUnsupprtTyplib folder to your project folder.|
|3.||Copy the following text into Notepad, and save the file in the project folder as Objsafe.odl:
|4.||At a command prompt, use the CD <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. In the Properties list, change the name of the project to IObjSafety and the name of the control to DemoCtl. Put a CommandButton named cmdTest on the control. In the Click event handler of the cmdTest, put a MsgBox "Test" statement.|
|6.||On the Project menu, click References, 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:
|8.||From Project Properties, change the Startup Object to Sub Main to execute the Sub Main above. Use the m_fSafeForScripting and m_fSafeForInitializing variables 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):
|10.||Copy the following two procedures to your controls code:
Private Sub IObjectSafety_GetInterfaceSafetyOptions(ByVal riid As _
|11.||On the File menu, save your project and files. Make an OCX file from your project. Your control now implements the IObjectSafety interface. To test it, insert the control in an .htm file.|