To clarify some details: I'm building a Windows Setup app that can be used with Windows-To-Go USB sticks, to make it able to install Windows straight from that session onto a local storage unit, like Linux's Live CD's.
I automatically had these classes made using the mgmtclassgen.exe
Now i'm trying to create a partition on a disk, and make it return a MSFT_Partition class object through the OUT parameters.
This is the code for the app:
private async Task<(Partition Boot, Partition Data)> PrepareDisk(Disk disk)
{
lbStatus.Text = "Clearing disk";
var cleared = await Task.Run(() => disk.Clear(
RemoveData: true,
RemoveOEM: true,
RunAsJob: true,
Sanitize: false,
ZeroOutEntireDisk: false,
out ManagementBaseObject statusClear));
await Task.Run(() => disk.ConvertStyle(2, out ManagementBaseObject status));
lbStatus.Text = "Creating boot partition";
var ptBoot = new Partition();
var ptData = new Partition();
await Task.Run(() =>
{
disk.CreatePartition(
Alignment: 0,
AssignDriveLetter: true,
DriveLetter: char.MinValue,
GptType: "System Partition",
IsActive: true,
IsHidden: true,
MbrType: 6,
Offset: 0,
Size: Convert.ToUint64(128 * 1048576),
UseMaximumSize: false,
out ManagementBaseObject partBoot,
out ManagementBaseObject statusBoot);
Task.WaitAll();
ptBoot = new Partition(partBoot);
});
lbStatus.Text = "Creating data partition";
await Task.Run(() =>
{
disk.CreatePartition(
Alignment: 0,
AssignDriveLetter: true,
DriveLetter: default,
GptType: "Basic data",
IsActive: true,
IsHidden: true,
MbrType: 7,
Offset: Convert.ToUInt64(128 * 1048576),
Size: disk.LargestFreeExtent,
UseMaximumSize: false,
out ManagementBaseObject partData,
out ManagementBaseObject statusData);
Task.WaitAll();
ptData = new Partition(partData);
});
return (ptBoot, ptData);
}
This is the code that was generated through mgmtclassgen.exe:
public uint CreatePartition(uint Alignment, bool AssignDriveLetter, char DriveLetter, string GptType, bool IsActive, bool IsHidden, ushort MbrType, ulong Offset, ulong Size, bool UseMaximumSize, out System.Management.ManagementBaseObject CreatedPartition, out System.Management.ManagementBaseObject ExtendedStatus)
{
if ((isEmbedded == false))
{
System.Management.ManagementBaseObject inParams = null;
inParams = PrivateLateBoundObject.GetMethodParameters("CreatePartition");
inParams["Alignment"] = ((uint)(Alignment));
inParams["AssignDriveLetter"] = ((bool)(AssignDriveLetter));
inParams["DriveLetter"] = ((char)(DriveLetter));
inParams["GptType"] = ((string)(GptType));
inParams["IsActive"] = ((bool)(IsActive));
inParams["IsHidden"] = ((bool)(IsHidden));
inParams["MbrType"] = ((ushort)(MbrType));
inParams["Offset"] = ((ulong)(Offset));
inParams["Size"] = ((ulong)(Size));
inParams["UseMaximumSize"] = ((bool)(UseMaximumSize));
System.Management.ManagementBaseObject outParams = PrivateLateBoundObject.InvokeMethod("CreatePartition", inParams, null);
CreatedPartition = ((System.Management.ManagementBaseObject)(outParams.Properties["CreatedPartition"].Value));
ExtendedStatus = ((System.Management.ManagementBaseObject)(outParams.Properties["ExtendedStatus"].Value));
return System.Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
}
else
{
CreatedPartition = null;
ExtendedStatus = null;
return System.Convert.ToUInt32(0);
}
}
Now I found out that AssignDriveLetter, and DriveLetter don't work in coallition with each other. If I assign both at the same time, I get a 'Access Path Not Valid' error.
My question is: how can I NOT assign (or use/specify) the DriveLetter property in the parameters used for CreatePartition?