basquang™ on clouds

November 15, 2010

Develop a File IO ActiveX control in Microsoft .NET

Filed under: ActiveX,Digital Signature,Microsoft Technology — basquang @ 4:39 PM

Development

1. Use this guide to create your own ActiveX control

Writing an ActiveX Control in .NET

2. Design your ActiveX as picture bellow:

image

3. Use following code behind

namespace FileIOActiveXControl
{
    public partial class WriteToClientControl : UserControl
    {
        public WriteToClientControl()
        {
            InitializeComponent();
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            WriteLog(@"c:\temp\log.txt", txtUserInput.Text);
        }

        public void WriteLog(string fileName, string content)
        {
            try
            {
                StreamWriter log = new StreamWriter(fileName, true);
                log.WriteLine(content);
                log.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

3. Create TestActiveX.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<hr>
Please note that you MUST load this HTML document from your local IIS, or from a TRUSTED SITE or the control will not load properly on the page due to security policy.
<br>
To fix this:<br>
<li>copy the HTM file and the DLL into your c:\inetpub\wwwroot folder (or applicable IIS root directory)
<li>load the document in your browser with the url:  http://localhost/TestActiveX.htm
  <hr>
	 <OBJECT id="fileIOActiveX" name="fileIOActiveX" classid="http:FileIOActiveXControl.dll#FileIOActiveXControl.WriteToClientControl" width="450" height="100" VIEWASTEXT>
	 </OBJECT>
  <hr>
 </body>
</script>
</html>

Deployment

1. Copy FileIOActiveXControl.dll and TestActiveX.htm to the same folder

2. Go to IIS to create a new web site and point to that folder

3. Use another computer to test your control.

If you control cannot load to page like picture bellow.

image

a. Try to add the site to Trusted Zone in IE

b. Make sure .dll and .htm in the same folder

If your control is loaded correctly, you will see:

image

4. Enter some text to the textbox then click Write To File button. You may see:

“Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.”

image

To fix this. You have to configure Code Access Security for Microsoft .NET on client. Run following command to fix this issue.

“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -quiet -machine -chggroup Trusted_Zone FullTrust”

image

And  (MANDATORY if your client is running OS 64 bit)

“C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\caspol.exe -quiet -machine -chggroup Trusted_Zone FullTrust”

image

5. Stop IE –> reload the test page –> input some text –> click “Write To File” button again now. There will not have any error message appear. Check the log file from C:\temp\log.txt

Success.

Advertisements

Create a free website or blog at WordPress.com.