Thanks to @f6a4 answer. I took a reverse way to accomplish my goal. Here are my procedure:
I find an answer to find the DLL behind Get-AppxPacage
cmdlet in Powershell. With this command (Get-Command Get-AppxPackage).dll
, Powershell shows the DLL file path as follows:
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Windows.Appx.PackageManager.Commands\v4.0_10.0.0.0__31bf3856ad364e35\Microsoft.Windows.Appx.PackageManager.Commands.dll
Go to that path in File Explorer and open Microsoft.Windows.Appx.PackageManager.Commands.dll
file in any .NET decompiler. Here I used dnSpy. The Get-AppxManifest
command section has this C# code:
protected override void ProcessRecord()
{
AppxPackage appxPackage = this.packageManager.FindPackage(this.Package);
if (appxPackage != null)
{
string str;
if (appxPackage.IsBundle)
{
str = "\\AppxMetadata\\AppxBundleManifest.xml";
}
else
{
str = "\\AppxManifest.xml";
}
using (FileStream fileStream = new FileStream(appxPackage.InstallLocation + str, FileMode.Open, FileAccess.Read))
{
using (XmlReader xmlReader = XmlReader.Create(fileStream, new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Ignore
}))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlReader);
base.WriteObject(xmlDocument);
}
}
}
}
I converted that code a similar C code with Windows API. Here is the code snippet:
ExpandEnvironmentStringsW(L"%ProgramFiles%\\WindowsApps", Buffer, MAX_PATH);
swprintf(FirstFile, MAX_PATH, L"%ls\\*", Buffer);
hFile = FindFirstFileW(FirstFile, &fileInfo);
if (hFile != INVALID_HANDLE_VALUE) {
do {
if (wcsstr(fileInfo.cFileName, AppxName) != 0) {
memcpy(PackageName, fileInfo.cFileName, MAX_PATH);
}
} while (FindNextFileW(hFile, &fileInfo) != 0);
}