title: Open, attach and assign a VHD
body: <p>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:</p>
  
  <pre><code>Mount-VHD -Path D:\vhd-file.vhd -NoDriveLetter 
  diskpart&gt; select vdisk file="D:\vhd-file.vhd"
  diskpart&gt; sel par 1
  diskpart&gt; assign mount="D:\vhd\active
  </code></pre>
  
  <p>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!)</p>
  
  <p>I found the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd323700%28v=vs.85%29.aspx" rel="nofollow">Microsoft Dev VHD Reference</a> 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.</p>
  
  <p>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++.</p>