当前位置: 首页>>代码示例>>C++>>正文


C++ SetEnvironmentVariable函数代码示例

本文整理汇总了C++中SetEnvironmentVariable函数的典型用法代码示例。如果您正苦于以下问题:C++ SetEnvironmentVariable函数的具体用法?C++ SetEnvironmentVariable怎么用?C++ SetEnvironmentVariable使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SetEnvironmentVariable函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: defineSystemDirectories

// Obtain the right savegame paths for the platform
// XP is "%USERPROFILE%\My Documents\My Games"
// Vista and up : "%USERPROFILE%\Saved Games"
void defineSystemDirectories(const char * argv0) {
	
	ARX_UNUSED(argv0);
	
	std::string strPath;
	DWORD winver = GetVersion();
	
	// Vista and up
	if((DWORD)(LOBYTE(LOWORD(winver))) >= 6) {
		// Don't hardlink with SHGetKnownFolderPath to allow the game to start on XP too!
		typedef HRESULT (WINAPI * PSHGetKnownFolderPath)(const GUID &rfid, DWORD dwFlags,
		                                                 HANDLE hToken, PWSTR* ppszPath); 
		
		CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
		
		PSHGetKnownFolderPath GetKnownFolderPath = (PSHGetKnownFolderPath)GetProcAddress(GetModuleHandleA("shell32.dll"), "SHGetKnownFolderPath");
		const GUID FOLDERID_SavedGames = {0x4C5C32FF, 0xBB9D, 0x43b0, {0xB5, 0xB4, 0x2D, 0x72, 0xE5, 0x4E, 0xAA, 0xA4}};
		
		LPWSTR wszPath = NULL;
		HRESULT hr = GetKnownFolderPath(FOLDERID_SavedGames, kfFlagCreate | kfFlagNoAlias, NULL, &wszPath);
		
		if(SUCCEEDED(hr)) {
			strPath = ws2s(wszPath);
		}
		
		CoTaskMemFree(wszPath);
		CoUninitialize();
	} else if((DWORD)(LOBYTE(LOWORD(winver))) == 5) { // XP
		CHAR szPath[MAX_PATH];
		HRESULT hr = SHGetFolderPathA(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL,
		                              SHGFP_TYPE_CURRENT, szPath);
		
		if(SUCCEEDED(hr)) {
			strPath = szPath; 
			strPath += "\\My Games";
		}
	} else {
		arx_assert_msg(false, "Unsupported windows version (below WinXP)");
	}
	
	if(!strPath.empty()) {
		SetEnvironmentVariable("FOLDERID_SavedGames", strPath.c_str());
	}
}
开发者ID:temojin,项目名称:ArxLibertatis,代码行数:47,代码来源:Environment.cpp

示例2: main

int __cdecl main(int argc, char *argv[]) {

    /* Define some buffers needed for the function */
    WCHAR * pResultBuffer = NULL;
    WCHAR SomeEnvironmentVariable[] = {'P','A','L','T','E','S','T','\0'};
    WCHAR TheEnvironmentValue[] = {'T','E','S','T','\0'};
    int size;
  
    /*
     * Initialize the PAL and return FAILURE if this fails
     */

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
  
    SetEnvironmentVariable(SomeEnvironmentVariable,
                           TheEnvironmentValue);
  
  
    /* Normal case, PATH should fit into this buffer */
    size = GetEnvironmentVariable(convert("PALTEST"),   // Variable Name
                                  pResultBuffer,        // Buffer for Value
                                  0);                   // Buffer size
  
    pResultBuffer = malloc(size*sizeof(WCHAR));
  
    GetEnvironmentVariable(convert("PALTEST"),
                           pResultBuffer,
                           size);
  
    if(wcsncmp(pResultBuffer,convert("TEST"),wcslen(pResultBuffer) * 2) != 0) 
    {
        Fail("ERROR: The value in the buffer should have been 'TEST' but was "
             "really '%s'.",convertC(pResultBuffer));
    }
    
    free(pResultBuffer);  
    
    PAL_Terminate();
    return PASS;
}
开发者ID:0-wiz-0,项目名称:coreclr,代码行数:43,代码来源:test.c

示例3: SetEnvironmentVariable

/*
 * Class:     org_jdesktop_jdic_init_InitUtility
 * Method:    setEnv
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_org_jdesktop_jdic_init_InitUtility_setEnv
  (JNIEnv * env, jclass /*obj*/, jstring envVar, jstring envValue)
{
    const char* pEnvVar = env->GetStringUTFChars(envVar, JNI_FALSE);
    const char* pEnvValue = env->GetStringUTFChars(envValue, JNI_FALSE);
    if (NULL != pEnvVar) 
    {
        if (NULL != pEnvValue) 
        {
#ifdef WIN32
            SetEnvironmentVariable(pEnvVar, pEnvValue);
#else
            setenv(pEnvVar, pEnvValue, 1);
#endif
               env->ReleaseStringUTFChars(envValue, pEnvValue);
        }
        env->ReleaseStringUTFChars(envVar, pEnvVar);
    }
}
开发者ID:M6C,项目名称:com-parzing-ui-swing,代码行数:24,代码来源:InitUtility_1.cpp

示例4: usb_install_needs_restart_np

int usb_install_needs_restart_np(void)
{
  HDEVINFO dev_info;
  SP_DEVINFO_DATA dev_info_data;
  int dev_index = 0;
  SP_DEVINSTALL_PARAMS install_params;
  int ret = FALSE;

  dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
  dev_info = SetupDiGetClassDevs(NULL, NULL, NULL,
                                 DIGCF_ALLCLASSES | DIGCF_PRESENT);
  
  SetEnvironmentVariable("LIBUSB_NEEDS_REBOOT", "1");

  if(dev_info == INVALID_HANDLE_VALUE)
    {
      usb_error("usb_install_needs_restart_np(): getting "
                "device info set failed");
      return ret;
    }
  
  while(SetupDiEnumDeviceInfo(dev_info, dev_index, &dev_info_data))
    {
      memset(&install_params, 0, sizeof(SP_PROPCHANGE_PARAMS));
      install_params.cbSize = sizeof(SP_DEVINSTALL_PARAMS);

      if(SetupDiGetDeviceInstallParams(dev_info, &dev_info_data, 
                                       &install_params))
        {
          if(install_params.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))
            {
              usb_message("usb_install_needs_restart_np(): restart needed");
              ret = TRUE;
            }
        }
      
      dev_index++;
    }
  
  SetupDiDestroyDeviceInfoList(dev_info);

  return ret;
}
开发者ID:Habib2014,项目名称:tinyos-2.x-contrib,代码行数:43,代码来源:install.c

示例5: coin_setenv

SbBool
coin_setenv(const char * name, const char * value, int overwrite)
{
#ifdef HAVE_GETENVIRONMENTVARIABLE
/*
  The value is changed by the application, so we no longer need to
  guarantee old buffers' existence to the outside world.  We therefore
  free buffers we are bookkeeping here.  This code can be disabled
  without any consequence though.
*/
  struct envvar_data * envptr, * prevptr;
  envptr = envlist_head;
  prevptr = NULL;
  while ((envptr != NULL) && (strcmp(envptr->name, name) != 0)) {
    prevptr = envptr;
    envptr = envptr->next;
  }
  if (envptr) {
    /* unlink node */
    if (prevptr) prevptr->next = envptr->next;
    else envlist_head = envptr->next;
    if (envlist_tail == envptr) envlist_tail = prevptr;
    /* free node */
    free(envptr->name);
    free(envptr->val);
    free(envptr);
  }

  /* FIXME: This is from Win32s 1.3 Bug List - how should we handle it?
  and what's with the typo in the function name?  20030205 larsa
  ====================================================================
  SetEnvironmentVariables() does not handle an empty string, an equal
  sign (=), or foreign lowercase characters in the variable name.
  */

  if (overwrite || (GetEnvironmentVariable(name, NULL, 0) == 0))
    return SetEnvironmentVariable(name, value) ? TRUE : FALSE;
  else
    return TRUE;
#else /* !HAVE_GETENVIRONMENTVARIABLE */
  return (setenv(name,value,overwrite) == 0);
#endif /* !HAVE_GETENVIRONMENTVARIABLE */
}
开发者ID:Alexpux,项目名称:Coin3D,代码行数:43,代码来源:tidbits.cpp

示例6: prop_unexport

/// Make sure the specified property will not be exported to children.
/// This has no effect on the current setting of the property.
/// WARNING: must not be called from the auditor.
/// @param[in] prop     the specified property
/// @param[in] forever  boolean - if set, newly set values no longer exported
void
prop_unexport(prop_e prop, int forever)
{
    char buf[PROP_NAME_MAX];

    _prop_to_ev(prop, buf, charlen(buf));

#if defined(_WIN32)
    SetEnvironmentVariable(buf, NULL);
#else	/*_WIN32*/
    putil_unsetenv(buf);
#endif	/*_WIN32*/

    if (forever) {
	proptab[prop].pr_flags &= ~PROP_FLAG_EXPORT;
    }

    return;
}
开发者ID:denji,项目名称:audited-objects,代码行数:24,代码来源:prop.c

示例7: originalPathValue

xlw::PathUpdater::PathUpdater()
{
    MEMORY_BASIC_INFORMATION theInfo ;
    HMODULE theHandle = NULL;
    char theDLLPathChar [MAX_PATH + 1] = "";
    DWORD dwRet = 0;
    std::string originalPathValue(StringUtilities::getEnvironmentVariable("PATH"));
    bool ok(!originalPathValue.empty());

    dwRet = static_cast<DWORD>(VirtualQuery (((LPCVOID)this), &theInfo,(static_cast<DWORD> (sizeof (MEMORY_BASIC_INFORMATION)))));
    if (dwRet)
    {
        theHandle = ((HMODULE) (theInfo.AllocationBase));
        GetModuleFileName (theHandle, theDLLPathChar , MAX_PATH);
        xlw::XlfServices.StatusBar = theDLLPathChar;
    }
    else
    {
        ok = false;
        std::cerr << XLW__HERE__ <<" Could not attain path of DLL" << std::endl;
    }
    if(ok)
    {
        std::string theDLLPath(theDLLPathChar);
        std::string newPathValue(originalPathValue);
        std::string::size_type pos = theDLLPath.find_last_of("\\");
        newPathValue+= ";"+theDLLPath.substr(0,pos);

        if (!SetEnvironmentVariable("Path", newPathValue.c_str()))
        {
            std::cerr << XLW__HERE__ << " SetEnvironmentVariable failed to set PATH" << std::endl;
            ok = false;
        }
        else
        {
            std::cerr << XLW__HERE__ << " PATH set successfully " << std::endl;
        }
    }
    if(!ok)
    {
        std::cerr << XLW__HERE__ << " Warning: Unable to initialise PATH to directory of library " << std::endl;
    }
}
开发者ID:Laeeth,项目名称:d_excelsdk,代码行数:43,代码来源:PathUpdater.cpp

示例8: lw6sys_setenv

/**
 * lw6sys_setenv
 *
 * @keyword: the environment variable to set
 * @value: the value of the environment variable to set
 *
 * Sets the environment variable to a given value. If value
 * is NULL, variable is unset. Note that unlike lw6sys_getenv_prefixed,
 * this function does not transform the keyword into "LW6_..."
 * before setting the value, so it's your responsability to
 * call "lw6sys_keyword_as_env" if needed.
 *
 * Return value: 1 if success, 0 if failed
 */
int
lw6sys_setenv (lw6sys_context_t * sys_context, const char *keyword, const char *value)
{
  int ret = 0;

#ifdef LW6_MS_WINDOWS
  {
    /*
     * For some reason (tired of getting into MS specifics...) it's
     * required to use putenv *and* SetEnvironmentVariable. Putenv
     * is required for instance when setting GUILE_LOAD_PATH. In doubt,
     * we do both, the last one being the MS dedicated function,
     * which should overwrite the previous in case of conflict and/or
     * handle special characters such as '=' in a more consistent way.
     */
    char *putenv_str = NULL;

    putenv_str = lw6sys_new_sprintf ("%s=%s", keyword, value);
    if (putenv_str)
      {
	putenv (putenv_str);
	LW6SYS_FREE (sys_context, putenv_str);
      }
  }
  ret = SetEnvironmentVariable (keyword, value) ? 1 : 0;
  if (!ret)
    {
      lw6sys_log (sys_context, LW6SYS_LOG_WARNING, _x_ ("SetEnvironmentVariable failed"));
    }
#else
  if (value)
    {
      ret = setenv (keyword, value, 1) ? 0 : 1;
    }
  else
    {
      ret = unsetenv (keyword);
    }
#endif

  return ret;
}
开发者ID:lijiaqigreat,项目名称:liquidwar-web,代码行数:56,代码来源:sys-env.c

示例9: acl_putenv

int acl_putenv(char *str)
{
#ifdef ACL_WINDOWS
	const char *myname = "acl_putenv";
	ACL_ARGV *argv = acl_argv_split(str, "=");

	if (argv->argc != 2) {
		acl_msg_error("%s(%d): input(%s) invalid", myname, __LINE__, str);
		return (-1);
	}
	if (!SetEnvironmentVariable(argv->argv[0], argv->argv[1])) {
		acl_msg_error("%s(%d): putenv(%s, %s) error(%s)",
			myname, __LINE__, argv->argv[0], argv->argv[1], acl_last_serror());
		return (-1);
	}
	return (0);
#else
	return (putenv(str));
#endif
}
开发者ID:angel16364,项目名称:acl,代码行数:20,代码来源:acl_env.c

示例10: PrependPathToEnv

void PrependPathToEnv(const TCHAR* path)
{
    DWORD oldPathLen = GetEnvironmentVariable(TEXT("PATH"), NULL, 0)+1;
    DWORD addPathLen = _tcslen(path)+1;
    DWORD newPathLen = addPathLen + oldPathLen;    // one NULL turns into semicolon

    TCHAR* newPath = (TCHAR*)_alloca(sizeof(TCHAR)*newPathLen);
    if (newPath)
    {
        StringCchPrintf(newPath, newPathLen, TEXT("%s;"), path);
        for(UINT iC= 0; iC<addPathLen; iC++)
        {
            if(newPath[iC] == TEXT('\\'))
                newPath[iC] = '/';
        }

        GetEnvironmentVariable(TEXT("PATH"), newPath+addPathLen, newPathLen-addPathLen  );

        SetEnvironmentVariable(TEXT("PATH"), newPath);
    }
}
开发者ID:steadyfield,项目名称:SourceEngine2007,代码行数:21,代码来源:MayaManager.cpp

示例11: __kmp_env_unset

void
__kmp_env_unset( char const * name ) {

    #if KMP_OS_UNIX
        unsetenv( name );
    #elif KMP_OS_WINDOWS
        BOOL rc = SetEnvironmentVariable( name, NULL );
        if ( ! rc ) {
            DWORD error = GetLastError();
            __kmp_msg(
                kmp_ms_fatal,
                KMP_MSG( CantSetEnvVar, name ),
                KMP_ERR( error ),
                __kmp_msg_null
            );
        }; // if
    #else
        #error Unknown or unsupported OS.
    #endif

} // func __kmp_env_unset
开发者ID:haraldservat,项目名称:LLVM-openmp,代码行数:21,代码来源:kmp_environment.c

示例12: OpSetEnv

BOOL OpSetEnv(LPVOID* p)
{
   LPTSTR Name = GetString(p);
   LPTSTR Value = GetString(p);
   LPTSTR ExpandedValue;
   ExpandPath(&ExpandedValue, Value);
   DEBUG("SetEnv(%s, %s)", Name, ExpandedValue);

   BOOL Result = FALSE;
   if (!SetEnvironmentVariable(Name, ExpandedValue))
   {
      FATAL("Failed to set environment variable (error %lu).", GetLastError());
      Result = FALSE;
   }
   else
   {
      Result = TRUE;
   }
   LocalFree(ExpandedValue);
   return Result;
}
开发者ID:Epictetus,项目名称:ocra,代码行数:21,代码来源:stub.c

示例13: main

int main(int argc, char *argv[]) {
    void *p;
    HINSTANCE hRtsDll, hProgDll;
    LPTSTR oldPath;

    StgClosure *main_p;
    RtsConfig rts_config;
    hs_main_t hs_main_p;

    // MSDN says: An environment variable has a maximum size limit of
    // 32,767 characters, including the null-terminating character.
    oldPath = malloc(32767);
    if (oldPath == NULL) {
        die("Mallocing 32767 for oldPath failed");
    }

    if (!GetEnvironmentVariable(TEXT("PATH"), oldPath, 32767)) {
        if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
            oldPath[0] = '\0';
        }
        else {
            die("Looking up PATH env var failed");
        }
    }
    setPath();
    hProgDll = loadDll(progDll);
    if (! SetEnvironmentVariable(TEXT("PATH"), oldPath)) {
        printf("SetEnvironmentVariable failed (%d)\n", GetLastError());
    }
    free(oldPath);

    hRtsDll = GetNonNullModuleHandle(rtsDll);

    hs_main_p    = GetNonNullProcAddress(hRtsDll,  "hs_main");
    main_p       = GetNonNullProcAddress(hProgDll, "ZCMain_main_static_closure");
    rts_config.rts_opts_enabled = rtsOpts;
    rts_config.rts_opts = NULL;

    return hs_main_p(argc, argv, main_p, rts_config);
}
开发者ID:Chobbes,项目名称:ghc,代码行数:40,代码来源:dynwrapper.c

示例14: up2date_createProcess

DWORD
up2date_createProcess(LPCTSTR commandLine) {
    LPWSTR *argv;
    int argc;
    DWORD error;

    argv = CommandLineToArgvW(commandLine, &argc);
    if (argv) {
        switch (argc) {
        case 2:
            if (!SetEnvironmentVariable(
                        TEXT("SIP_COMMUNICATOR_AUTOUPDATE_INSTALLDIR"),
                        *(argv + 1))) {
                error = GetLastError();
                break;
            }
        case 1: {
            STARTUPINFO si;
            PROCESS_INFORMATION pi;

            ZeroMemory(&si, sizeof(si));
            si.cb = sizeof(si);

            error
                = CreateProcess(NULL, *argv, NULL, NULL, FALSE, 0, NULL,
                                NULL, &si, &pi)
                  ? 0
                  : GetLastError();
        }
        break;
        default:
            error = 0;
            break;
        }

        LocalFree((HLOCAL) argv);
    } else
        error = GetLastError();
    return error;
}
开发者ID:xmppjingle,项目名称:sip-comm-jn,代码行数:40,代码来源:up2date.c

示例15: SetEnv

int SetEnv( const char *key, const char *value)
{
	assert(key);
	assert(value);
#ifdef WIN32
	if ( !SetEnvironmentVariable(key, value) ) {
		dprintf(D_ALWAYS,
			"SetEnv(%s, %s): SetEnvironmentVariable failed, "
			"errno=%d\n", key, value, GetLastError());
		return FALSE;
	}
#else
	char *buf;
	buf = new char[strlen(key) + strlen(value) + 2];
	sprintf(buf, "%s=%s", key, value);
	if( putenv(buf) != 0 )
	{
		dprintf(D_ALWAYS, "putenv failed: %s (errno=%d)\n",
				strerror(errno), errno);
		delete[] buf;
		return FALSE;
	}

	char *hashed_var=0;
	if ( EnvVars.lookup( HashKey( key ), hashed_var ) == 0 ) {
			// found old one
			// remove old one
		EnvVars.remove( HashKey( key ) );
			// delete old one
		delete [] hashed_var;
			// insert new one
		EnvVars.insert( HashKey( key ), buf );
	} else {
			// no old one
			// add new one
		EnvVars.insert( HashKey( key ), buf );
	}
#endif
	return TRUE;
}
开发者ID:AlainRoy,项目名称:htcondor,代码行数:40,代码来源:setenv.cpp


注:本文中的SetEnvironmentVariable函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。