IsProcessInIsolatedWindowsEnvironment 函数 (isolatedwindowsenvironmentutils.h)

确定应用程序正在运行的执行环境 - 主机或独立环境。

Syntax

HRESULT IsProcessInIsolatedWindowsEnvironment(
  BOOL *isProcessInIsolatedWindowsEnvironment
);

参数

isProcessInIsolatedWindowsEnvironment

[out]

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

返回值

返回 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;
}

要求

Requirement 价值
Header isolatedwindowsenvironmentutils.h
Library IsolatedWindowsEnvironmentUtils.lib
DLL isolatedwindowsenvironmentutils.dll

另请参阅

Microsoft Defender 应用程序防护概述

IsolatedWindowsEnvironment

IsolatedWindowsEnvironmentHost