My use case is the following: I have a ready vhd image and need to mount it to a path f.e. D:\vhd\active. At the moment my solution is a combination of Powershell Hyper-V and diskpart commands but this seems very dirty and hacked:
Mount-VHD -Path D:\vhd-file.vhd -NoDriveLetter
diskpart> select vdisk file="D:\vhd-file.vhd"
diskpart> sel par 1
diskpart> assign mount="D:\vhd\active
As you can see I've used the "NoDriveLetter" because I don't want auto assign. The "sel par 1" I have to do because I found no easier way to select the right volume (other ideas were to check "lis vol" before the Mount-VHD and after it ... but yeah I always only have one partition so "sel par" ftw!)
I found the Microsoft Dev VHD Reference and could "Open" and "Attach" the VHD image via C# pinvoke code. I could even initialize it with a combination of CreateFile from kernel32.dll and DeviceIoControl with CREATE_DISK parameters. But I found no way to mount it to the folder path. I tried SetVolumeMountPoint and GetVolumeNameForVolumeMountPoint from kernel32.dll but the Guid of my VHD or the VirtualDiskIdentifier doesn't seem to be the right parameters for these functions.
My question is if someone can point me to the right functions/methods to achieve the same result like I have with the scripted solution but in code. In code means C/C++ or C# ... I am able to write my own C#-Wrappers if it is easier to do this task in plain C or C++.