.NET Components for Mobility

UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

Last post 05-12-2008 3:37 PM by alanjmcf. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 05-05-2008 2:29 PM

    UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    I am getting an UnauthorizedAccessException (see stack trace below) when I try to use BluetoothSecurity.RemoveDevice() to remove a pairing that I originally created using BluetoothSecurity.PairRequest().

    The PairRequest works fine, it's only the call to RemoveDevice that is giving me trouble.  My application is using the Sample Privileged Developer key that comes with the WM6 SDK, so I should have full administrator access to the device.  Also, I discovered that I can programatically remove the pairing from the registry with no problem.

    The WM device is an HTC Touch which uses WM6 and the Microsoft Bluetooth stack.  I am developing with Visual Studio 2005 with the WM6 SDK.

    I noticed that someone else posted a similar problem but there haven't been any suggestions posted yet.  Any thoughts would be appreciated!  Thanks.

    "at System.ThrowHelper.ThrowUnauthorizedAccessException()\r\nat Microsoft.Win32.RegistryKey.EnsureWriteable()\r\nat Microsoft.Win32.RegistryKey.DeleteSubKeyTree()\r\nat InTheHand.Net.Bluetooth.BluetoothSecurity.RemoveDevice()\r\nat BluetoothInterloper.MainForm.MainForm_Load()\r\nat System.Windows.Forms.Form.OnLoad()\r\nat System.Windows.Forms.Form._SetVisibleNotify()\r\nat System.Windows.Forms.Control.set_Visible()\r\nat System.Windows.Forms.Application.Run()\r\nat BluetoothInterloper.Program.Main()\r\n"

  • 05-08-2008 10:17 AM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    Hmm, I'm not the WindowsMobile expert, I'll see if Peter's around and ask if he can have a look.  Have you checked the contents of the Registry at the respective keys/values and checked if they exist, and if any permissions are applied (does WinCE have permissions in the Registry?).

    What do you mean by "Also, I discovered that I can programatically remove the pairing from the registry with no problem."  Using some other application/tool?

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 05-08-2008 10:39 AM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    Thanks for looking into it.  Yes, the keys do exist.  I can remove them using the methods in the .NET RegistryKey class.  It is my understanding that in order to be as lightweight as possible Windows Mobile does not use permissions on individual files or registry keys.  The application simply runs with total privileged rights or it doesn't, based on the certificate that you apply at compile time.  And I'm compiling with the Privileged Developer certificate, so to the best of my knowledge there shouldn't be any permission issues.

  • 05-08-2008 10:53 AM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    FYI, here is an exact copy of my MainForm.cs file.  It always fails on the RemoveDevice call even though the PairRequest succeeds.

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using InTheHand.Net;

    using InTheHand.Net.Sockets;

    using InTheHand.Net.Bluetooth;

     

    namespace BluetoothInterloper

    {

    public partial class MainForm : Form

    {

    private BluetoothAddress bluetoothAddress = new BluetoothAddress(

    new byte[ { 0x91, 0xfc, 0x2a, 0x17, 0x13, 0x00, 0x00, 0x00 });

    private string bluetoothPin = "0000";public MainForm()

    {

    InitializeComponent();

    }

    private void MainForm_Load(object sender, EventArgs e)

    {

    bool result = BluetoothSecurity.PairRequest(bluetoothAddress, bluetoothPin);if (result == false)

    {

    throw new ApplicationException("Pairing failed");

    }

    }

    private void MainForm_Closing(object sender, CancelEventArgs e)

    {

    BluetoothSecurity.RemoveDevice(bluetoothAddress);

    }

    }

    }

  • 05-11-2008 5:49 AM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    An important question comes to mind. :-)  Are you using the CF1 or CF2 version of the library?  The former, AIUI, uses locally constructed Registry classes, and the latter uses's MSFT's own.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 05-11-2008 6:03 AM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    Hmm.  Looking at the code and the API documentation, it seems that it should always fail.  We do

           RegistryKey rk = Registry.LocalMachine.OpenSubKey(NativeMethods.ceRegistryRoot + "Device");

    then

          rk.DeleteSubKeyTree(device.ToString());

    But the first operation according to MSDN does: "Retrieves a subkey as read-only."

    We need to call OpenSubKey("... ...", true)  Probably this isn't enforced by our own built-in Registry classes so it works in the CF1 library?

    Can you download the sources from codeplex?  Change the code in the RemoveDevice method to do that.  See if that solves it.  See http://www.codeplex.com/32feet/SourceControl/ListDownloadableCommits.aspx and then http://www.codeplex.com/32feet/SourceControl/FileView.aspx?itemId=431609&changeSetId=33193 -- obviously skipping the parts that say to skip building the NETCF libraries.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 05-12-2008 10:08 AM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    That was it!  Works fine now.  Thanks for the tip!

  • 05-12-2008 3:37 PM In reply to

    Re: UnauthorizedAccessException from BluetoothSecurity.RemoveDevice()

    I've fixed it in the sources too.  Thanks for your report and testing!

    (http://www.codeplex.com/32feet/WorkItem/View.aspx?WorkItemId=16554)

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
Page 1 of 1 (8 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.