Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Saving a Toolbar State to Registry?

Saving a Toolbar State to Registry?

Scheduled Pinned Locked Moved C#
helpwindows-admindebuggingjsonperformance
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    redfish34
    wrote on last edited by
    #1

    I am trying to save a toolbar state to registry for later restore. But i seem unable to work with the Win32 API correctly. Can someone help? The problem is with the TBSAVEPARAMS struct. It wants a handle to the registry key (HKEY_LOCAL_MACHINE in this case) but where do i get this? I have tried passing the HKEY_LOCAL_MACHINE const 0x80000002 but i get a memory cannot be read error when i execute SendMessage TB_SAVERESTORE. I have tried the NET Registry.LocalMachine but i am unable to turn this into a pointer needed for the struct. So what options are available? Is this impossible in NET? Below is the code i am using. [StructLayout(LayoutKind.Sequential)] public struct TBSAVEPARAMS { public UIntPtr hkr; public string pszSubKey; public string pszValueName; } [DllImport("user32.dll")] public static extern int SendMessage( IntPtr hWnd, uint msg, bool wParam, Win32Struct.TBSAVEPARAMS lParam); // open registrykey // create key if needed RegistryKey key = null; string regKey = @"SOFTWARE\TEST"; key = Registry.LocalMachine.OpenSubKey(regKey, true); // check errors if (key == null) { return; } // close registry key key.Close(); // get toolbar handle IntPtr hWndToolBar = TrayIcons.GetIconTrayWnd(); // set up restore struct Win32Struct.TBSAVEPARAMS tbSaveParams = new Win32Struct.TBSAVEPARAMS(); tbSaveParams.hkr = (UIntPtr)Win32Const.HKEY_LOCAL_MACHINE; tbSaveParams.pszSubKey = regKey; tbSaveParams.pszValueName = "Toolbar"; // save tray icon state int result = Win32Windows.SendMessage(hWndToolBar, (uint)Win32Const.TB_SAVERESTOREA, true, tbSaveParams); Debug.WriteLine("result: " + result);

    R 1 Reply Last reply
    0
    • R redfish34

      I am trying to save a toolbar state to registry for later restore. But i seem unable to work with the Win32 API correctly. Can someone help? The problem is with the TBSAVEPARAMS struct. It wants a handle to the registry key (HKEY_LOCAL_MACHINE in this case) but where do i get this? I have tried passing the HKEY_LOCAL_MACHINE const 0x80000002 but i get a memory cannot be read error when i execute SendMessage TB_SAVERESTORE. I have tried the NET Registry.LocalMachine but i am unable to turn this into a pointer needed for the struct. So what options are available? Is this impossible in NET? Below is the code i am using. [StructLayout(LayoutKind.Sequential)] public struct TBSAVEPARAMS { public UIntPtr hkr; public string pszSubKey; public string pszValueName; } [DllImport("user32.dll")] public static extern int SendMessage( IntPtr hWnd, uint msg, bool wParam, Win32Struct.TBSAVEPARAMS lParam); // open registrykey // create key if needed RegistryKey key = null; string regKey = @"SOFTWARE\TEST"; key = Registry.LocalMachine.OpenSubKey(regKey, true); // check errors if (key == null) { return; } // close registry key key.Close(); // get toolbar handle IntPtr hWndToolBar = TrayIcons.GetIconTrayWnd(); // set up restore struct Win32Struct.TBSAVEPARAMS tbSaveParams = new Win32Struct.TBSAVEPARAMS(); tbSaveParams.hkr = (UIntPtr)Win32Const.HKEY_LOCAL_MACHINE; tbSaveParams.pszSubKey = regKey; tbSaveParams.pszValueName = "Toolbar"; // save tray icon state int result = Win32Windows.SendMessage(hWndToolBar, (uint)Win32Const.TB_SAVERESTOREA, true, tbSaveParams); Debug.WriteLine("result: " + result);

      R Offline
      R Offline
      redfish34
      wrote on last edited by
      #2

      I found that one has to us process shared memory when communicating with windows OS toolbars; I thought this only applied when reading back structs in a SendMessage. Below is update of my code. I do not get any errors this time, but nothing happens. No registry key value is created, nothing. I probably need someone with PInvoke experience to OK my code ot NOT. That would help. // get toolbar handle IntPtr hWndToolBar = TrayIcons.GetIconTrayWnd(); // get handle to other process // open process for all access or get access denied error int processID = 0; int threadID = Win32ProcessThread.GetWindowThreadProcessId( hWndToolBar, ref processID); IntPtr hProcess = Win32ProcessThread.OpenProcess( Win32Const.PROCESS_ALL_ACCESS, false, processID); // set up shared memory for struct // make it big enough to hold dynamic data int bufferSize = 16000; IntPtr hGlobalBuffer = Win32Memory.VirtualAllocEx( hProcess, IntPtr.Zero, bufferSize, Win32Const.MEM_RESERVE | Win32Const.MEM_COMMIT, Win32Const.PAGE_READWRITE); // allocate local memory to hold struct IntPtr hLocalBuffer = Marshal.AllocHGlobal(bufferSize); // set up save struct Win32Struct.TBSAVEPARAMS tbSaveParams = new Win32Struct.TBSAVEPARAMS(); string regKey = @"SOFTWARE\TEST"; tbSaveParams.hkr = (UIntPtr)Win32Const.HKEY_LOCAL_MACHINE; tbSaveParams.pszSubKey = regKey; tbSaveParams.pszValueName = "IconsBackup"; // convert struct to pointer Marshal.StructureToPtr(tbSaveParams, hLocalBuffer, false); // put struct in place Win32Memory.WriteProcessMemory(hProcess, hGlobalBuffer, hLocalBuffer, bufferSize, 0); // save tray icon state // no return value Win32Windows.SendMessage(hWndToolBar, (uint)Win32Const.TB_SAVERESTOREA, true, hLocalBuffer); // clean up Win32Memory.VirtualFreeEx(hProcess, hGlobalBuffer, bufferSize, Win32Const.MEM_RELEASE); Marshal.FreeHGlobal(hLocalBuffer); Win32ProcessThread.CloseHandle(hProcess);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups