共用方式為


_dupenv_s_dbg _wdupenv_s_dbg

取得值,從目前的環境。版本的_dupenv_s _wdupenv_s所配置的記憶體與_malloc_dbg提供額外的偵錯資訊。

errno_t _dupenv_s_dbg(
   char **buffer,
   size_t *numberOfElements,
   const char *varname,
   int blockType,
   const char *filename,
   int linenumber
);
errno_t _wdupenv_s_dbg(
   wchar_t **buffer,
   size_t * numberOfElements,
   const wchar_t *varname,
   int blockType,
   const char *filename,
   int linenumber
);

參數

  • buffer
    若要儲存該變數的值的緩衝區。

  • numberOfElements
    Size of buffer.

  • varname
    環境變數的名稱。

  • blockType
    要求的記憶體區塊類型: _CLIENT_BLOCK或_NORMAL_BLOCK。

  • filename
    原始程式檔的名稱的指標或NULL。

  • linenumber
    行號,在原始程式檔或NULL。

傳回值

零都成功,失敗的錯誤代碼。

這些函式會驗證它們的參數。 如果buffer或varname是NULL,如所述,無效的參數處理常式被叫用參數驗證。如果執行,則允許繼續執行,該函式會設定errno到EINVAL ,並傳回EINVAL。

如果這些函式無法配置足夠的記憶體,它們會將buffer到NULL和numberOfElements為 0,而且傳回ENOMEM。

備註

_dupenv_s_dbg和_wdupenv_s_dbg函式是等於_dupenv_s和_wdupenv_s差異在於,當_DEBUG是定義,這些函式會使用偵錯版本的malloc_malloc_dbg、 配置記憶體給環境變數的值。如有關的偵錯功能_malloc_dbg,請參閱_malloc_dbg

您不需要明確地呼叫這些函式,在大多數情況下。相反地,您可以在其中定義這個旗標_CRTDBG_MAP_ALLOC。當_CRTDBG_MAP_ALLOC已定義,會呼叫_dupenv_s 和_wdupenv_s對應至 「 _dupenv_s_dbg和_wdupenv_s_dbg,分別與blockType設定為 [ _NORMAL_BLOCK。因此,您不需要明確地呼叫這些函式,除非您想要標示為堆積區塊_CLIENT_BLOCK。如需有關的區塊類型的詳細資訊,請參閱偵錯堆積上的區塊類型

泛用文字常式對應

TCHAR。H 常式

_UNICODE & 未定義的 _MBCS

定義的 _MBCS

定義 _unicode 之後

_tdupenv_s_dbg

_dupenv_s_dbg

_dupenv_s_dbg

_wdupenv_s_dbg

需求

常式

所需的標頭

_dupenv_s_dbg

<crtdbg.h>

_wdupenv_s_dbg

<crtdbg.h>

其他的相容性資訊,請參閱相容性在簡介中。

範例

// crt_dupenv_s_dbg.c
#include  <stdlib.h>
#include <crtdbg.h>

int main( void )
{
   char *pValue;
   size_t len;
   errno_t err = _dupenv_s_dbg( &pValue, &len, "pathext",
      _NORMAL_BLOCK, __FILE__, __LINE__ );
   if ( err ) return -1;
   printf( "pathext = %s\n", pValue );
   free( pValue );
   err = _dupenv_s_dbg( &pValue, &len, "nonexistentvariable",
      _NORMAL_BLOCK, __FILE__, __LINE__ );
   if ( err ) return -1;
   printf( "nonexistentvariable = %s\n", pValue );
   free( pValue ); // It's OK to call free with NULL
}

範例輸出

pathext = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.pl
nonexistentvariable = (null)

.NET Framework 對等用法

System::Environment::GetEnvironmentVariable

請參閱

參考

處理程序和環境控制

環境的常數

getenv_s _wgetenv_s

_putenv_s _wputenv_s