环境变量(获取,暂无设置的代码)

1、参考网址: C_C++ 获取系统环境变量方法. - algorithmic - 博客园.html(https://www.cnblogs.com/algorithmic/archive/2012/09/23/2698604.html

// EnvConsole_VC.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include <stdlib.h>

// ZC: 取某个环境变量

//int main()
//{
//    printf("PATH : %s\n", getenv("PATH"));
//    printf("HOME : %s\n", getenv("HOME"));
//    printf("ROOT : %s\n", getenv("ROOT"));
//
//}

#include <stdio.h>
//#include <unistd.h>// Posix
#include <windows.h>

// ZC: 遍历所有环境变量

//extern char **environ;
//int main(int argc, char *argv[])
//{
//    char **p = environ;
//    while (*p != NULL)
//    {
//        printf("%s (%p)\n", *p, *p);
//        *p++;
//    }
//    system("pause");
//    return 0;
//}

#include <string>
#include <vector>
using namespace std;

void SplitStr_ZZ(vector<string>& _vtr, char* _pc, char* _pcPattern);
int WriteLog(char* _pcFullFileName, char* _pcWrite, int _iWriteLen, unsigned long * _pdwWritten);

void Env_All_1(vector<string>& _vtr);
void Env_All_2(vector<string>& _vtrOut, vector<string>& _vtrIn);

int main(int argc, char *argv[])
{
    vector<string> vtr1;
    vector<string> vtr2;
    Env_All_1(vtr1);
    Env_All_2(vtr2, vtr1);

    string str;
    for (int i = 0; i < vtr2.size(); i++)
        //printf("%s\n", vtr2.at(i).c_str());
        str += vtr2.at(i) + "\r\n";

    char buf[256] = { 0 };
    SYSTEMTIME sys;
    GetLocalTime(&sys);
    sprintf(buf, "D:\\HuanJingBianLiang.%02d%02d%02d.%d.txt", sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);

    DWORD dwWriteLen = 0;
    WriteLog(buf, (char*)str.c_str(), str.length(), &dwWriteLen);
}



void Env_All_1(vector<string>& _vtr)
{
    _vtr.clear();

    char **p = environ;
    while (*p != NULL)
    {
        _vtr.push_back(string(*p));
        //printf("%s (%p)\n", *p, *p);

        *p++;
    }
}

void Env_All_2(vector<string>& _vtrOut, vector<string>& _vtrIn)
{
    _vtrOut.clear();

    for (int i = 0; i < _vtrIn.size(); i++)
    {
        string str = _vtrIn.at(i);

        vector<string> vtr;
        SplitStr_ZZ(vtr, (char*)str.c_str(), (char*)"=");
        if (vtr.size() == 1)
            _vtrOut.push_back(vtr.at(0) + " :");
        else if (vtr.size() == 2)
        {
            _vtrOut.push_back(vtr.at(0) + " :");
            
            vector<string> vtr1;
            SplitStr_ZZ(vtr1, (char*)vtr.at(1).c_str(), (char*)";");
            for (int j = 0; j < vtr1.size(); j++)
                _vtrOut.push_back("\t" + vtr1.at(j));
        }
        else
            printf("Unhandled : %s\n", str.c_str());
    }
}


int WriteLog(char* _pcFullFileName, char* _pcWrite, int _iWriteLen, unsigned long * _pdwWritten)
{
    HANDLE hLogFile = CreateFileA(_pcFullFileName, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_ALWAYS/*CREATE_NEW*/, 0, 0);// ZC: 存在:打开;不存在:创建
    if (hLogFile == INVALID_HANDLE_VALUE)
        return GetLastError();

    if (INVALID_SET_FILE_POINTER == SetFilePointer(hLogFile, 0, 0, FILE_END))
    {
        int iErr = GetLastError();
        CloseHandle(hLogFile);
        return iErr;
    }

    BOOL B = WriteFile(hLogFile, _pcWrite, _iWriteLen, _pdwWritten, NULL);
    if (!B)
    {
        int iErr = GetLastError();
        CloseHandle(hLogFile);
        return iErr;
    }
    CloseHandle(hLogFile);
    return 0;
}

 

2、

void SplitStr_ZZ(vector<string>& _vtr, char* _pc, char* _pcPattern)
{
    _vtr.clear();
    if ((_pc == NULL) || (_pcPattern == NULL))
        return;

    char* pc = strtok(_pc, _pcPattern);
    while (pc != NULL)
    {
        _vtr.push_back(string(pc));

        pc = strtok(NULL, _pcPattern);
    }

}

 

3、

4、

5、

 

posted @ 2019-09-30 14:53  CppSkill  阅读(307)  评论(0编辑  收藏  举报