Can anyone give me example code of _dupenv_s?

前端 未结 1 1338
名媛妹妹
名媛妹妹 2021-02-18 20:35

I\'m using getenv(\"TEMP\"), but I\'m getting a warning telling me to use _dupenv_s.

I can\'t find an example of _dupenv_s on the net.

相关标签:
1条回答
  • 2021-02-18 21:03

    _dupenv_s is a Microsoft function, designed as a more secure form of getenv.

    _dupenv_s allocates the buffer itself; you have to pass it a pointer to a pointer and it sets this to the address of the newly allocated buffer.

    For example,

    char* buf = nullptr;
    size_t sz = 0;
    if (_dupenv_s(&buf, &sz, "EnvVarName") == 0 && buf != nullptr)
    {
        printf("EnvVarName = %s\n", buf);
        free(buf);
    }
    

    Note that you're responsible for freeing the returned buffer.

    0 讨论(0)
提交回复
热议问题