IsProcessInIsolatedWindowsEnvironment 函数 (isolatedwindowsenvironmentutils.h)

确定应用程序在哪个执行环境中运行 - 主机或独立环境。

语法

HRESULT IsProcessInIsolatedWindowsEnvironment(
  BOOL *isProcessInIsolatedWindowsEnvironment
);

参数

isProcessInIsolatedWindowsEnvironment

[out]

指向接收 API 结果的布尔值的指针。 如果进程位于独立 Windows 环境中,则此参数为 truefalse 否则为 。

返回值

S_OK如果函数成功,则返回 。 如果失败,则会返回 HRESULT 错误代码。

注解

使用 Microsoft Defender 应用程序防护 (MDAG) 的任何应用程序都需要能够查找运行它的执行环境。 需要这样做,以便应用可以采取适当的行为来保护应用的用户/企业数据、用户标识和业务利益。

示例

以下示例演示如何使用 IsProcessInIsolatedWindowsEnvironment API 来确定应用的执行环境。

#define PrintInfo wprintf

typedef HRESULT (*pIsProcessInIsolatedWindowsEnvironment)
                  (_Out_ BOOL *isProcessInIsolatedWindowsEnvironment);

int PrintError(unsigned int line, HRESULT hr)
{
  wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
  return hr;
}

HRESULT TakeActionAsPerExecutionEnvironment()
{
  //For instance the action could be saving changes to user settings for the app.
  //Lets assume the app has made a design decision to save change to user settings if
  //the app is running on the host, and discard the changes to user settings if they were
  //changed in an Isolated Environment.

  HMODULE dllInstance (LoadLibrary(L"IsolatedWindowsEnvironmentUtils.dll"));

  if (nullptr == dllInstance)
  {
    PrintInfo(L" Cannot load the library IsolatedWindowsEnvironmentUtils.dll \n");
    return E_FAIL;
  }

  auto pfn = reinterpret_cast<pIsProcessInIsolatedWindowsEnvironment>

  (GetProcAddress(dllInstance, "IsProcessInIsolatedWindowsEnvironment"));

  if (nullptr == pfn)
  {
    PrintInfo(L"Function definition IsProcessInIsolatedWindowsEnvironment() is not found.\n");
    FreeLibrary(dllInstance);
    return E_FAIL;
  }

  BOOL isInIsolatedWindowsEnvironment = FALSE;

  HRESULT hr = pfn(&isInIsolatedWindowsEnvironment);

  if (FAILED(hr))
  {
    FreeLibrary(dllInstance);
    return PrintError(__LINE__, hr);
  }

  if (isInIsolatedWindowsEnvironment == TRUE) //app is running in Isolated Environment
  {
    //do not save changes to the app’s user settings in this case
    PrintInfo(L"Discarding changes to app’s user settings.\n");

    //<TO-DO-Start>
    //Add app specific custom logic here
    //<TO-DO-End>
  }
  else
  {
    //Save changes to the app’s user settings in this case
    PrintInfo(L"Saving changes to app’s user settings.\n");

    //<TO-DO-Start>
    //Add app specific custom logic here
    //<TO-DO-End>
  }

  FreeLibrary(dllInstance);
  return S_OK;
}

要求

要求
Header isolatedwindowsenvironmentutils.h
DLL isolatedwindowsenvironmentutils.dll

另请参阅

Microsoft Defender 应用程序防护概述

IsolatedWindowsEnvironment

IsolatedWindowsEnvironmentHost