Libvirt on Windows Part II

I have successfully started developing a VM Mamangement tool on windows utilizing C#. So far, it is progressing better than I expected — the libvirt library is really fantastic! I am modeling the start of the tool after the VM Manager in linux.

You can check out the solution here https://github.com/smasherprog/VM_Manager

Libvirt on windows

I am experimenting with libvirt, and my favorite IDE is visual studio so I looked for an easy method to build the libvirt library on windows and found that the state of cross-platform libraries like Mingw are outdated or difficult to use correctly. I know there is a way, but assembling the right libraries and commands was just too much. So, I found that the libvirt dll’s are actually a part of most distros of linux and can be downloaded and extracted. I just needed to find out what dlls were needed, then search the repos for those files.

After the search, I have begun work on a powershell script to get the latest libvirt builds from fedora, download and extract them. After that, you can go to town building libraries against the libvirt api. Check out the script here: https://github.com/smasherprog/Libvirt_Windows_Dll

Anyone who links against the libvirt library would be creating a management tool of some sort so a language like C# so after I create this tool, I will write some code to generate the C# code files for the DLLImport calls. The Libvirt header thousands of lines long so I will write some code to extract the calls/structs and generate the PInvoke calls for everything at once.

SystemParametersInfo Error

While working on the desktop sharing program, I came across a very interesting bug in the SystemParametersInfoW function while trying to get the current users wallpaper.

THE FIX IS TO USE SystemParametersInfoA — this works as expected: no junk data added and no buffer overrun.

Here is the code to reproduce the error — THERE IS NO OTHER CODE

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int main()

{
wchar_t OldWallpaper[MAX_PATH * 4];
for (auto i = 0; i < 5; i++){
SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, OldWallpaper, 0);
}
return 0;
}

I put this into a loop so I could see the data changing in the debugger by inserting a break point at the end of each for loop iteration. What I found was that the variable OldWallpaper was being filled with all sorts of junk data, sometimes it had something about ‘TRS\\USERS’, another time it was ‘\\.\DISPLAY6′. The upside is that the first character was always a null character if there was no wallpaper selected, so the function is partially correct. In cases where a wallpaper did exist, junk data was inserted after the path.

Now, I could live with this, but the function occasionally overan the buffer by a few characters at random times. It was causing heap corruption and crashing the program because the function was overrunning the buffer. My recommendation is to use twice the buffer to give it space to grow when needed.

If you look up SystemParametersInfo with SPI_GETDESKWALLPAPER, the function expects the size of the buffer in CHARACTERS, not bytes, as stated below for SPI_GETDESKWALLPAPER:

Retrieves the full path of the bitmap file for the desktop wallpaper. The pvParam parameter must point to a buffer to receive the null-terminated path string. Set the uiParam parameter to the size, in characters, of thepvParam buffer. The returned string will not exceedMAX_PATH characters. If there is no desktop wallpaper, the returned string is empty.

In any case, the junk data being returned is problematic even if the function should take the number of bytes instead of the number of characters.

You can view the bug report here which contains the images of the bug.

https://connect.microsoft.com/VisualStudio/feedback/details/1101370/systemparametersinfo-buffer-overrun

 

Desktop Viewer status

I currently have a working, stable VNC-like remote viewing desktop application at https://github.com/smasherprog/RemoteDesktop/. In all of my tests, it outperforms VNC on network traffic by 2-4X, and performance is the same.

All of the functionality is there, copy/paste between client and server; drag and drop file transfer. If you install the server as a service, which you can do by running the server exe with the -install command, it will allow you to view the Login Screen, Windows Locked Screen, Switch Users screen and the server will persist across reboots.

Next on the list

  • Support for more than 1 monitor
  • add option for server to remove itself upon command from viewer (this is most helpful when you want to connect to assist someone, then completely remove the application) — the code is done, I just need to tie it in.
  • Create a Proxy Server which will allow the server and client to be behind a NAT device and still connect to each other. It acts as a bridge between internal networks.

code postings

I have stopped work on my c# VNC like application. You can view the code on Github at https://github.com/smasherprog/Desktop_Sharing  The application is stable, but has lag spikes that occur at random times. I suspect that the problem is the garbage collector, but I dont want to go into tracking this problem down because this would be going against the language. Others might say that it is natural to have to optimize code, but the type of optimization necessary is like helping a blind man see.

So, i decided to re-write it in c++! You can check it out at https://github.com/smasherprog/RemoteDesktop The code is stable and will capture UAC, Logon log off, and regular windows operations. It is better than vnc in all of my performance/networking testing by wide margins. Additionally, it is about 10X faster than the C# equivalent code and does not have fluctuations in its running as my c# application did.

The only issue is when switching users, this causes the program to get “confused.” Once the user logs back into the desktop everything resets.  This was designed for Vista and forward.. It might work on XP, but I havent tried and dont plan on supporting an old OS.

Encryption is in place, but I have to hook it in. Within the next few days I guess. . . .

GitHub Code

I just added a new project to github; its a desktop sharing program — like VNC. So far everything is progressing nicely! There are two main projects: the viewer and the server.

To get started:

Open the project DesktopSharing_Viewer, make sure the project DesktopSharing_Viewer is set to the project starup. Then, build it. There is a text box for the IP address of the machine the server is running on to connect to.

Open the project DesktopSharing_Server, make sure the project DesktopSharingService is set to the project starup. Then, build it. There is a project called DesktopSharing_Server, which is where all of the code is, but the server is designed to be a service to run in the background so the project is set up in this way.

Run the Server on a different computer, then run the viewer on your computer and connect up.

 

backup program

I just released the start of a backup program which query’s an NTFS journal for changes. The ntfs journal is very fast for determining changes, searches and creating a file structure of the drive. With the Master File Table, an entire file count, folder count along with a complete mapping of the drive structure can be completed in less than a second.

This is the first release, and its not set up to actually do backups yet. Current, I build the file structure of a drive from the Master File Table. Next, I will query the journal to get changes which will allow me to build a list of files that need to be backed up.

Its written in C#, check it out   https://github.com/smasherprog/Backup

 

vnc proxy with websocket support

I just released a very early version of the websocket support for the VNC proxy server. It is very very unoptimized and a bit messy, but it works!. I am releasing this as a proof of concept and will clean up the code within the next few days. Check it out at https://github.com/smasherprog/VNC_Proxy

 

vnc proxy

Adding support for web sockets so the proxy can be used for regular vnc clients and web based novnc clients. Expect this within the week. . .

More code

I have released more code on github https://github.com/smasherprog

Block_SizeReporter: This will scan the directory get the file sizes and report back the best block size that should be used for the given files. When formatting a drive, sometimes you want to know what the best blocksize is to use given a certain filestructure. This is a short tool for that.

VNC_Proxy: While playing with UltraVNC, i found their repeater (aka, proxy) code to be …. not very neat. So, I wrote their repeater in c#.