Jump to content
Sign in to follow this  
Dave Nottage

"Self-updating" terminal services DLL

Recommended Posts

I expect this is not going to be possible, however I thought I'd ask just in case someone has a brainwave of some kind.

 

I'm working on a DLL which implements a virtual channel for Windows terminal services, and a desired function is for it to be able to "self-update". It fetches a file which is the new version of the DLL, and is supposed to "move" the file in-place.

 

Needless to say I've tried to make it work, with the inevitable "access denied" errors when either attempting to rename the original file then replace it, or just copy over it.

Share this post


Link to post

It should be possible to rename a DLL while it is being used and replace it with a new one. I do this all the time in the pre build script of GExperts.

This of course requires the rights move the file in the first place.

  • Like 1

Share this post


Link to post
Guest
4 hours ago, Dave Nottage said:

I expect this is not going to be possible, however I thought I'd ask just in case someone has a brainwave of some kind.

When it comes to DLL loaded by system it need to be stopped first, so here two solution

1) The easy way, if there the required permission for the OS service loaded the DLL then you can restart the OS, but first use the RunOnce registry keys to copy the or replace .., 

https://docs.microsoft.com/en-us/windows-hardware/drivers/install/runonce-registry-key

 

2) The harder way make you DLL as EXE and DLL then let the new one unregister the old then register itself as DLL, but to do this you should also make sure that the OS service does has the required permission to run an EXE and install/un install the DLL, as the permission will be applied to the EXE.

 

I used both method successfully when wrote my own firewall to watch the traffic, using socket SPI https://docs.microsoft.com/en-us/windows/win32/winsock/transport-service-providers-2, but that was for Windows XP and Server 2003 and Server 2008, so i am not sure if your current OS will allow you to that, but as you said some brainwave might help.

 

 

26 minutes ago, dummzeuch said:

It should be possible to rename a DLL while it is being used and replace it with a new one. I do this all the time in the pre build script of GExperts.

I didn't know that does work with the IDE, spent hell of time in rebuilding, closing the IDE, copy the DLL over, then run the IDE !!!! 🤬

Share this post


Link to post
Guest

I forgot to extend the hard way into longer way, write your own service that does the (2) job, unregister, replace( or just reinstall with new name/path), then register again, give it the required permission, and you should be good to go, this should work on all Windows versions.

Share this post


Link to post

Depending on lib usage, you can split your lib into agent part that is rarely updated and functional part that could be unloaded and updated by agent part.

  • Like 3

Share this post


Link to post
4 hours ago, Kas Ob. said:

use the RunOnce registry keys to copy the or replace

MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT flag can do that already.

 

Virtual Channel client DLLs are loaded at boot so it's pointless to try to replace the file without a reboot.

That said, you could solve it with two DLLs:

  1. The Virtual Channel client DLL contains the auto-update functionality but forwards all the VirtualChannel* DLL calls to...
  2. The "other DLL" which implements the actual functionality.

Of course the Virtual Channel client DLL will need to load the "other DLL" dynamically or you will have the same problem with both DLLs.

When the Virtual Channel client DLL determines that it needs to update the "other DLL" it just unload the old DLL and load the new DLL.

If the Virtual Channel client DLL needs to update itself it can do so with MoveFileEx (or just unregister the old DLL and register the new) and a reboot. Maybe it's enough to restart the Remote Desktop Services (the documentation just states that the DLL is loaded during Remote Desktop Services initialization), but you will need a third file, an application, that does that for you. Give it a try.

 

Edit: I just realized that the above was basically what @Fr0sT.Brutal wrote.

Edited by Anders Melander
what he said
  • Like 3

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×