Wir empfehlen die GetEnvironmentVariable() der Windows-API. Die Standard-C Library beeinhaltet zwar eine Funktion getenv(), aber diese enthält einige Probleme, wenn sich die Umgebungsvariablen zur Laufzeit ändern.
Beispiel
Dieses Beispiel testet die Umgebungsvariablen und zeigt den Benutzernamen und Lizenzschlüssel an.
#include <windows.h> /* For declaration of GetEnvironmentVariable() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool ShowUserNameAndKey(void) {
char name[256]="", key[256]="", display[512];
/* Check the USERNAME; it should always be set if the program is protected */
if (!GetEnvironmentVariable("USERNAME", name, 255)) {
MessageBox(0, "Is the program protected?", "Error!", MB_OK|MB_ICONERROR);
return false;
};
if (!GetEnvironmentVariable("USERKEY", key, 255)) strcpy(key, "No key!");
if (!stricmp(name, "DEFAULT")) {
strcpy(display, "Program is using the default key; no username or key is available.");
} else {
sprintf(display, "Program is registered to %s (%s).", name, key);
};
MessageBox(0, display, "Information", MB_OK|MB_ICONINFORMATION);
return true;
}