Partager via


IsProcessInIsolatedWindowsEnvironment, fonction (isolatedwindowsenvironmentutils.h)

Détermine dans quel environnement d’exécution l’application s’exécute : un hôte ou un environnement isolé.

Syntaxe

HRESULT IsProcessInIsolatedWindowsEnvironment(
  BOOL *isProcessInIsolatedWindowsEnvironment
);

Paramètres

isProcessInIsolatedWindowsEnvironment

[out]

Pointeur vers une valeur booléenne qui reçoit le résultat de l’API. Ce paramètre sera true si le processus se trouve dans un environnement Windows isolé, false sinon.

Valeur retournée

Retourne S_OK si la fonction réussit. En cas d'échec, retourne un code d'erreur HRESULT.

Remarques

Toute application utilisant Protection d'application Microsoft Defender (MDAG) nécessite la possibilité de trouver l’environnement d’exécution dans lequel elle s’exécute. Cela est nécessaire pour que l’application puisse se comporter de manière appropriée pour protéger les données utilisateur/entreprise, l’identité de l’utilisateur et les intérêts métier de l’application.

Exemples

L’exemple suivant montre comment utiliser l’API IsProcessInIsolatedWindowsEnvironment pour déterminer l’environnement d’exécution de l’application.

#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;
}

Configuration requise

Condition requise Valeur
En-tête isolatedwindowsenvironmentutils.h
DLL isolatedwindowsenvironmentutils.dll

Voir aussi

Vue d’ensemble de Microsoft Defender Application Guard

IsolatedWindowsEnvironment

IsolatedWindowsEnvironmentHost