码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 杂项

时间:2019-11-02 09:19:16      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:amd   ptr   das   名称   _for   items   invalid   lib   lpcwstr   

#include <iostream>
#define Main main
#define COLOR_GREEN system("color 2");
#include <vector>
#include <list>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <LM.h>
#include <winnetwk.h>
#include "BitMap.h"
#include <wlanapi.h>
#include "ATBAudioEngine/ATBAudioEngine.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"Mpr.lib")
#pragma comment(lib,"netapi32.lib")
#pragma comment(lib,"Wlanapi.lib")
#include <Windows.h>

class disorderly
{
    int m_buf[10] = { 0 };
public:

    void initBuf()
    {
        srand(GetTickCount());
        int len = sizeof(m_buf) / sizeof(int);
        for (int i = 0; i < len; i++)
        {
            m_buf[i] = rand() % 100;
            printf("%d, ", m_buf[i]);
        }
    }

    //范围
    void initBuf(int range)
    {
        srand(GetTickCount());
        int len = sizeof(m_buf) / sizeof(int);
        for (int i = 0; i < len; i++)
        {
            m_buf[i] = rand() % range;
            printf("%d, ", m_buf[i]);
        }
    }

    int *getBuf() { return m_buf; }

    int getLen() { return sizeof(m_buf) / sizeof(int); }

    void print()
    {
        printf("\n");
        int len = sizeof(m_buf) / sizeof(int);
        for (int i = 0; i < len; i++)
        {
            printf("%d, ", m_buf[i]);
        }
        printf("\n");
    }

    void C(int *Arr, int Len)
    {
        int TempData = 0;
        int i = 0;
        int j = 0;
        for (i = 1; i < Len; i++)
        {
            j = i;
            TempData = *(Arr + i);
            //如果当前的数据小于当前数据的上一个
            //那么进行赋值, j--, 然后再判断j--数据是否小于上一个数据
            while (j > 0 && TempData < Arr[j - 1])
            {
                Arr[j] = Arr[j - 1];
                j--;
            }
            Arr[j] = TempData;
        }

        //for (int i = 0; i < 5; i++)
        //{
        //    std::thread th(C, i);
        //    th.join();
        //    //th.detch();
        //}
    }

    void M(int *Arr, int Len)
    {
        for (int i = 0; i < Len - 1; i++)
        {
            for (int j = i + 1; j < Len; j++)
            {
                if (Arr[i] > Arr[j])
                {
                    int temp = Arr[i];
                    Arr[i] = Arr[j];
                    Arr[j] = temp;
                }
            }
        }
    }

    void X(int *Arr, int Len)
    {
        int k = -1;
        for (int i = 0; i < Len - 1; i++)
        {
            k = i;
            for (int j = i + 1; j < Len; j++)
            {
                //第一波出来的必定是最小的
                if (Arr[k] > Arr[j])
                {
                    k = j;
                }
            }
            int temp = Arr[i];
            Arr[i] = Arr[k];
            Arr[k] = temp;
        }
    }

    void GetAllUserName(std::vector<std::wstring> & allUserName)
    {
        // See more: http://msdn.microsoft.com/en-us/library/windows/desktop/aa370652(v=vs.85).aspx
        LPUSER_INFO_1 pBuf = NULL;
        LPUSER_INFO_1 pTmpBuf;
        DWORD dwLevel = 1;
        DWORD dwEntriesRead = 0;
        DWORD dwTotalEntries = 0;
        DWORD dwResumeHandle = 0;
        DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
        NET_API_STATUS nStatus;
        LPTSTR pszServerName = NULL;

        do
        {
            nStatus = NetUserEnum((LPCWSTR)pszServerName,
                dwLevel,
                FILTER_NORMAL_ACCOUNT, // global users
                (LPBYTE*)&pBuf,
                dwPrefMaxLen,
                &dwEntriesRead,
                &dwTotalEntries,
                &dwResumeHandle);

            if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
            {
                if ((pTmpBuf = pBuf) != NULL)
                {
                    for (DWORD i = 0; i < dwEntriesRead; i++)
                    {
                        if (pTmpBuf == NULL)
                        {
                            fprintf(stderr, "An access violation has occurred\n");
                            break;
                        }
                        allUserName.push_back(pTmpBuf->usri1_name);
                        pTmpBuf++;
                    }
                }
            }
            else
                fprintf(stderr, "A system error has occurred: %d\n", nStatus);

            if (pBuf != NULL) {
                NetApiBufferFree(pBuf);
                pBuf = NULL;
            }
        } while (nStatus == ERROR_MORE_DATA); // end do

        // Check again for allocated memory.
        if (pBuf != NULL)
            NetApiBufferFree(pBuf);

        //std::vector<std::wstring> v;
        //GetAllUserName(v);
        //for (int i = 0; i < v.size(); i++)
        //{
        //    char buf[256] = { 0 };
        //    int j = 0;
        //    while (v[i].c_str()[j])
        //    {
        //        buf[j] = v[i].c_str()[j];
        //        j++;
        //    }
        //    printf(buf);
        //}
    }

    int getSystemPath()
    {
        HANDLE hFile;
        DWORD dwWrite;
        TCHAR path[MAX_PATH];
        GetSystemDirectory(path, MAX_PATH);
        hFile = CreateFile("syspath.txt", GENERIC_WRITE,
            0,
            NULL,
            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
            if (!(WriteFile(hFile, path, lstrlen(path), &dwWrite, NULL)))
            {
                return GetLastError();
            }
        }
        return CloseHandle(hFile);
    }

    //打印环境变量
    void printEV()
    {
        PVOID P = GetEnvironmentStringsA();
        LPSTR sz;
        for (sz = (LPSTR)P; *sz;)
        {
            printf("%s\n", sz);
            while (*sz++);
        }
        FreeEnvironmentStringsA((LPCH)P);
        P = NULL;

    }

    void vectors()
    {
        std::vector<int> arr;
        srand(GetTickCount());
        for (unsigned int i = 0; i < 10; i++)
        {
            arr.push_back(rand() % 100);
        }
        for (unsigned int i = 0; i < arr.size(); i++)
        {
            printf("%d, ", arr[i]);
        }
        printf("\n");
        std::cout << "sort last" << std::endl;
        std::cout << "======================" << std::endl;
        unsigned int j = 0;
        int data = 0;
        for (unsigned int i = 1; i < arr.size(); i++)
        {
            j = i;
            data = arr[i];
            while (j > 0 && data < arr[j - 1])
            {
                arr[j] = arr[j - 1];
                j--;
            }
            arr[j] = data;
        }
        for (unsigned int i = 0; i < arr.size(); i++)
        {
            printf("%d, ", arr[i]);
        }
        printf("\n");
        arr.clear();
    }

    void ComputerName_Ip()
    {
        char buf[256] = "";
        hostent *ph = NULL;//struct hostent *ph = NULL;
        WSADATA w;
        WSAStartup(0x0101, &w);//这一行必须在使用任何SOCKET函数前写!
        std::string hostName = buf;//此处获得本机名称
        gethostname(buf, sizeof(buf));
        ph = gethostbyname(buf);
        int i = 0;
        while (ph->h_addr_list[i] != NULL)
        {
            char *IP = inet_ntoa(*((struct in_addr *)ph->h_addr_list[i]));//此处获得本机IP
            std::cout << IP << std::endl;
            i++;
        }
        ph = gethostbyname("www.baidu.com");
        i = 0;
        while (ph->h_addr_list[i] != NULL)
        {
            char *IP = inet_ntoa(*((struct in_addr *)ph->h_addr_list[i]));//此处获得本机IP
            std::cout << IP << std::endl;
            i++;
        }
        WSACleanup();
    }

    void gethostsIP(char *hostsbuf)
    {

        WORD wVersion;
        WSADATA WSAData;
        wVersion = MAKEWORD(2, 2);
        WSAStartup(wVersion, &WSAData);
        gethostname(hostsbuf, 256);
        addrinfo hints;
        struct addrinfo *res, *cur;
        int ret;
        struct sockaddr_in *addr;
        char m_ipaddr[16];

        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_INET;     /* Allow IPv4 */
        hints.ai_flags = AI_PASSIVE;/* For wildcard IP address */
        hints.ai_protocol = 0;         /* Any protocol */
        hints.ai_socktype = SOCK_STREAM;

        ret = getaddrinfo(hostsbuf, NULL, &hints, &res);

        if (ret == -1)
        {
            perror("getaddrinfo");
            exit(1);
        }
        for (cur = res; cur != NULL; cur = cur->ai_next)
        {
            addr = (struct sockaddr_in *)cur->ai_addr;
            sprintf_s(m_ipaddr, "%d.%d.%d.%d",
                (*addr).sin_addr.S_un.S_un_b.s_b1,
                (*addr).sin_addr.S_un.S_un_b.s_b2,
                (*addr).sin_addr.S_un.S_un_b.s_b3,
                (*addr).sin_addr.S_un.S_un_b.s_b4);
            printf("%s\n", m_ipaddr);
        }
        freeaddrinfo(res);
        WSACleanup();
    }

    void gethostsIP(const char *hostsName)
    {

        WORD wVersion;
        WSADATA WSAData;
        wVersion = MAKEWORD(2, 2);
        WSAStartup(wVersion, &WSAData);
        addrinfo hints;
        struct addrinfo *res, *cur;
        int ret;
        struct sockaddr_in *addr;
        char m_ipaddr[16];

        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_INET;     /* Allow IPv4 */
        hints.ai_flags = AI_PASSIVE;/* For wildcard IP address */
        hints.ai_protocol = 0;         /* Any protocol */
        hints.ai_socktype = SOCK_STREAM;

        ret = getaddrinfo(hostsName, NULL, &hints, &res);

        if (ret == -1)
        {
            perror("getaddrinfo");
            exit(1);
        }
        for (cur = res; cur != NULL; cur = cur->ai_next)
        {
            addr = (struct sockaddr_in *)cur->ai_addr;
            sprintf_s(m_ipaddr, "%d.%d.%d.%d",
                (*addr).sin_addr.S_un.S_un_b.s_b1,
                (*addr).sin_addr.S_un.S_un_b.s_b2,
                (*addr).sin_addr.S_un.S_un_b.s_b3,
                (*addr).sin_addr.S_un.S_un_b.s_b4);
            printf("%s\n", m_ipaddr);
        }
        freeaddrinfo(res);
        res = NULL;
        WSACleanup();
    }

    //无线连接状态  
    int intlistenStatus()
    {
        HANDLE hClient = NULL;
        DWORD dwMaxClient = 2;
        DWORD dwCurVersion = 0;
        DWORD dwResult = 0;
        int iRet = 0;
        WCHAR GuidString[39] = { 0 };
        //ListenthestatusoftheAPyouconnected.  
        while (1)
        {
            Sleep(5000);
            PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
            PWLAN_INTERFACE_INFO pIfInfo = NULL;

            dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
            if (dwResult != ERROR_SUCCESS)
            {
                wprintf(L"WlanOpenHandlefailedwitherror:%u\n", dwResult);
                return 1;
            }
            //获取无线网卡列表  
            dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
            if (dwResult != ERROR_SUCCESS)
            {
                wprintf(L"WlanEnumInterfacesfailedwitherror:%u\n", dwResult);
                return 1;
            }
            else
            {
                wprintf(L"NumEntries:%lu\n", pIfList->dwNumberOfItems);
                wprintf(L"CurrentIndex:%lu\n\n", pIfList->dwIndex);
                int i;
                for (i = 0; i < (int)pIfList->dwNumberOfItems; i++)
                {
                    pIfInfo = (WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];
                    wprintf(L"InterfaceIndex[%u]:\t%lu\n", i, i);
                    iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
                        sizeof(GuidString) / sizeof(*GuidString));
                    if (iRet == 0)
                        wprintf(L"StringFromGUID2failed\n");
                    else
                    {
                        wprintf(L"InterfaceGUID[%d]:%S\n", i, GuidString);
                    }
                    wprintf(L"InterfaceDescription[%d]:%S", i,
                        pIfInfo->strInterfaceDescription);
                    wprintf(L"\n");
                    wprintf(L"InterfaceState[%d]:\t", i);
                    switch (pIfInfo->isState)
                    {
                    casewlan_interface_state_not_ready:
                        wprintf(L"Notready\n");
                        break;
                    casewlan_interface_state_connected:
                        wprintf(L"Connected\n");
                        break;
                    casewlan_interface_state_ad_hoc_network_formed:
                        wprintf(L"Firstnodeinaadhocnetwork\n");
                        break;
                    casewlan_interface_state_disconnecting:
                        wprintf(L"Disconnecting\n");
                        break;
                    casewlan_interface_state_disconnected:
                        wprintf(L"Notconnected\n");
                        break;
                    casewlan_interface_state_associating:
                        wprintf(L"Attemptingtoassociatewithanetwork\n");
                        break;
                    casewlan_interface_state_discovering:
                        wprintf(L"Autoconfigurationisdiscoveringsettingsforthenetwork\n");
                        break;
                    casewlan_interface_state_authenticating:
                        wprintf(L"Inprocessofauthenticating\n");
                        break;
                    default:
                        wprintf(L"Unknownstate%ld\n", pIfInfo->isState);
                        break;
                    }
                }
            }
        }
    }
};

#define FF FindFiles::getInstance()
class FindFiles
{
    string Path;
    string Type;
    HANDLE HFinder = NULL;
    WIN32_FIND_DATAA FindFileData;
    string CurPath;
    int num = -1;
public:
    static FindFiles *getInstance()
    {
        static FindFiles obj;
        return &obj;
    }
    //D:/Music_s
    void setFindPath(const char *Path)
    {
        if (Path)
        {
            this->Path = Path;
            CurPath = this->Path;
            if (this->Path == string("exit")) { exit(0); }
        }
        else
        {
            printf("input find path .......... \n");
        }
    }
    //D:/Music_s
    void setFindPath(string &Path)
    {
        if (!Path.empty())
        {
            this->Path = Path;
            CurPath = this->Path;
            if (this->Path == string("exit")) { exit(0); }
        }
        else
        {
            printf("input find path .......... \n");
        }
    }
    // mp3 mp4 ...
    void setFindType(const char *Type)
    {
        if (Type)
        {
            this->Type = Type;
            if (this->Type == string("exit")) { exit(0); }
        }
        else
        {
            printf("input find Type .......... \n");
        }
    }
    // mp3 mp4 ...
    void setFindType(string &Type)
    {
        if (!Type.empty())
        {
            this->Type = Type;
            if (this->Type == string("exit")) { exit(0); }

            FF->strcatFindFile();
        }
        else
        {
            printf("input find Type .......... \n");
        }
    }
    string getPath() const
    {
        return this->Path;
    }
    string getType() const
    {
        return this->Type;
    }

    ///=============================================
    void run()
    {
        system("color 2");
        char path[256] = { 0 };
        char type[256] = { 0 };
        printf("input path suppose <D:/Music>:");
        std::cin >> path;
        printf("input type or input 0 is any type suppose <bmp,mp3,mp4,0,cpp,h,c,zip,avi> :");
        std::cin >> type;
        FF->setFindPath(path);
        if (string(type) == "0")
        {
            FF->setFindType("*");
        }
        else
        {
            FF->setFindType(type);
        }
        FF->strcatFindFile();
        FF->findFirstFile(FF->Path.c_str());
        while (true)
        {
            if (!find())
            {
                break;
            }
        }
        printf("\n");
        printf("number =========== ::::::::  %d file \n", num);
    }
    void run_s()
    {
        while (true)
        {
            system("cls");
            run();
            system("pause");
        }
    }
    ///=============================================

    bool find()
    {
        string tmp = FindFileData.cFileName;
        if (tmp != "." && tmp != "..")
        {
            printf("%s , %d\n", FindFileData.cFileName, FindFileData.dwFileAttributes);
        }
        num++;
        int len = strlen(FindFileData.cFileName);
        char temp[2] = { 0 };
        temp[0] = FindFileData.cFileName[len - 1];
        if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
        {
            if (string(temp) != "" && string(temp) != "." && string(temp) != " " && string(temp) != "..")
            {
                string lastCurPath = this->CurPath;
                string lastCur = this->CurPath;
                lastCurPath += "/";
                lastCur += "/";
                lastCurPath += FindFileData.cFileName;
                lastCur += FindFileData.cFileName;
                lastCurPath += "/*.";
                if (this->Type == string("0"))
                {
                    lastCurPath += "*";
                }
                else
                {
                    lastCurPath += this->Type;
                }

                FF->ergodicFile(lastCurPath, lastCur);
            }
        }
        if (!FF->findNextFile())
        {
            return false;
        }
        return true;
    }
    bool ergodicFile(string path, string lastCur)
    {
        WIN32_FIND_DATAA temp;
        HANDLE HFinder = FindFirstFileA(path.c_str(), &temp);
        if (HFinder == INVALID_HANDLE_VALUE)
        {
            std::cout << "查找失败 !" << std::endl;
            return true;
        }

        while (true)
        {
            string tmp = temp.cFileName;
            if (tmp != "." && tmp != "..")
            {
                printf("%s , %d\n", temp.cFileName, temp.dwFileAttributes);
            }
            num++;
            int len = strlen(temp.cFileName);
            char strTemp[2] = { 0 };
            strTemp[0] = temp.cFileName[len - 1];
            if (temp.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
            {
                if (string(strTemp) != "" && string(strTemp) != "." && string(strTemp) != " " && string(strTemp) != "..")
                {
                    string CurPaths = lastCur;
                    string lastCurPath = CurPaths;
                    lastCurPath += "/";
                    CurPaths += "/";
                    lastCurPath += temp.cFileName;
                    CurPaths += temp.cFileName;
                    lastCurPath += "/*.";
                    if (this->Type == string("0"))
                    {
                        lastCurPath += "*";
                    }
                    else
                    {
                        lastCurPath += this->Type;
                    }
                    ergodicFile(lastCurPath, CurPaths);
                }
            }

            if (!(FindNextFileA(HFinder, &temp)))
            {
                return false;
            }
        }
        return true;
    }
    void findFirstFile(string path)
    {
        HFinder = FindFirstFileA(path.c_str(), &FindFileData);
        if (HFinder == INVALID_HANDLE_VALUE)
        {
            char buf[256] = { 0 };
            for (int i = path.length() - 1; i >= 0; i--)
            {
                if (path.c_str()[i] == .)
                {
                    int len = path.length() - i;
                    for (int j = 0; j <= len + 1; j++)
                    {
                        buf[j] = path.c_str()[j];
                    }
                }
            }
            string temp = buf;
            temp += "*";
            HFinder = FindFirstFileA(temp.c_str(), &FindFileData);
            if (HFinder == INVALID_HANDLE_VALUE)
            {
                std::cout << "查找失败 !" << std::endl;
            }
        }
    }
    void closeFind()
    {
        if (HFinder)
        {
            FindClose(HFinder);
            HFinder = NULL;
        }
    }
    bool findNextFile()
    {
        if (!(FindNextFileA(HFinder, &FindFileData)))
        {
            return false;
        }
        return true;
    }
private:
    FindFiles()
    {

    }
    ~FindFiles()
    {
        FF->closeFind();
    }
    //连接查找
    void strcatFindFile()
    {
        if (!Path.empty() && !Type.empty())
        {
            Path += "/*.";
            Path += Type.c_str();
            printf("find file path : %s\n", Path.c_str());
        }
    }
};


class Registry
{
public:
    void read_dword()//读取操作表,其类型为DWORD
    {
        HKEY hKEY;//定义有关的键,在查询结束时关闭
        //打开与路径data_Set相关的hKEY

        string data_set = "Software\\Chicony\\Lenovo1";

        //访问注册表,hKEY则保存此函数所打开的键的句柄
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set.c_str(), 0, KEY_READ, &hKEY))
        {
            DWORD dwValue;//长整型数据,如果是字符串数据用char数组
            DWORD dwSize = sizeof(DWORD);
            DWORD dwType = REG_DWORD;

            if (::RegQueryValueEx(hKEY, "123", 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
            {
                cout << "错误:无法查询有关的注册表信息" << endl;
            }

            cout << dwValue << endl;
        }
        ::RegCloseKey(hKEY);
    }

    void read_reg_sz()//读取操作表,其类型为REG_SZ
    {
        HKEY hkey;

        string data_set = "Software\\Chicony\\Lenovo1";
        //LPCWSTR lpwchar = &data_set;

        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set.c_str(), 0, KEY_READ, &hkey))
        {
            char dwValue[256];
            DWORD dwSzType = REG_SZ;
            DWORD dwSize = sizeof(dwValue);
            if (::RegQueryValueEx(hkey, "wangchong", 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
            {
                cout << "无法查询有关的注册表信息" << endl;
            }
            cout << dwValue << endl;
        }
        ::RegCloseKey(hkey);
    }

    void write_dword()//在\Software\\Chicony\\Lenovo1文件夹下写入一个test111的子键,设置其名称为Name,其值为6
    {
        HKEY hkey;//定义有关的hkey,在查询结束时要关闭
        HKEY hTempKey;

        DWORD dwValue = 6;
        DWORD dwSize = sizeof(DWORD);
        DWORD dwType = REG_DWORD;

        const char  *data_set = "Software\\Chicony\\Lenovo1";
        if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS == ::RegCreateKey(hkey, "test111", &hTempKey))
            {
                if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, "Name", 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)))
                {
                    cout << "写入注册表失败" << endl;
                }
            }
        }
        ::RegCloseKey(hkey);
    }

    void write_reg_sz()
    {
        HKEY hkey;
        HKEY hTempKey;
        char m_name_set[256] = "China";

        DWORD len = strlen(m_name_set) + 1;
        const char  *data_set = "Software\\Chicony\\Lenovo1";
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS == ::RegCreateKey(hkey, "test1112", &hTempKey))
            {
                if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, "Name", 0, REG_SZ, (const BYTE*)m_name_set, len))
                {
                    cout << "写入错误" << endl;
                }
            }
        }
        ::RegCloseKey(hkey);
    }

    void write_binary()
    {
        HKEY hkey;
        HKEY hTempKey;
        BYTE m_name[10];
        memset(m_name, 0, sizeof(m_name));
        m_name[0] = 0xff;
        m_name[1] = 0xac;
        m_name[2] = 0x05;
        m_name[3] = 0x4e;

        const char  *data_set = "Software\\Chicony\\Lenovo1";
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS == ::RegCreateKey(hkey, "test111", &hTempKey))
            {
                if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, "Name", 0, REG_BINARY, (unsigned char *)m_name, 5))
                {
                    cout << "写入错误" << endl;
                }
            }
        }
        ::RegCloseKey(hkey);
    }

    void delete_value()
    {
        HKEY hkey;
        const char  *data_set = "Software\\Chicony\\Lenovo1\\test1112";

        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS != ::RegDeleteValue(hkey, "Name"))
            {
                cout << "删除错误" << endl;
            }
        }
        ::RegCloseKey(hkey);
    }

    void delete_key()
    {
        HKEY hkey;
        const char  *data_set = "Software\\Chicony\\Lenovo1";

        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS != ::RegDeleteKey(hkey, "test1112"))
            {
                cout << "删除错误" << endl;
            }
        }
        ::RegCloseKey(hkey);
    }
};


#define DB DoubleBuffer::GetInstance()
class DoubleBuffer
{
    HANDLE m_hOutBuf = NULL;
    HANDLE m_hOutput = NULL;
    //双缓冲处理显示
    DWORD bytes = 0;
    COORD coord = { 0,0 };
public:
    static DoubleBuffer *GetInstance()
    {
        static DoubleBuffer db;
        return &db;
    }
    HANDLE GetOutBuf() { return m_hOutBuf; }
    HANDLE GetOutPut() { return m_hOutput; }
    void Begin(char **data, int W_Len)
    {
        for (int i = 0; i < W_Len; i++)
        {
            coord.Y = i;
            BOOL Over = WriteConsoleOutputCharacterA(m_hOutBuf, data[i], W_Len, coord, &bytes);
            int var = 0;
        }
        //设置新的缓冲区为活动显示缓冲
        BOOL Over = SetConsoleActiveScreenBuffer(m_hOutBuf);
    }

    void End(char **data, int W_Len)
    {
        for (int i = 0; i < W_Len; i++)
        {
            coord.Y = i;
            BOOL Over = WriteConsoleOutputCharacterA(m_hOutput, data[i], W_Len, coord, &bytes);
        }
        //设置新的缓冲区为活动显示缓冲
        BOOL Over = SetConsoleActiveScreenBuffer(m_hOutput);
    }

    /*
        system("color 2");
    char **buf = NULL;
    buf = new char*[40];
    for (int i = 0; i < 40; i++)
    {
        buf[i] = new char[40];
        for (int j = 0; j < 40; j++)
        {
            buf[i][j] = 0;
        }
    }
    srand(GetTickCount());
    while (true)
    {
        for (int i = 0; i < 40; i++)
        {
            for (int j = 0; j < 40 - 1; j++)
            {
                buf[i][j] = rand() % 48 + 32;
            }
        }
        DB->Begin(buf, 40);
        for (int i = 0; i < 40; i++)
        {
            for (int j = 0; j < 40 - 1; j++)
            {
                buf[i][j] = rand() % 48 + 32;
            }
        }
        DB->End(buf, 40);

        for (int i = 0; i < 40; i++)
        {
            for (int j = 0; j < 40 - 1; j++)
            {
                buf[i][j] = 0;
            }
        }
    }
    */

private:
    DoubleBuffer()
    {
        //创建新的控制台缓冲区
        m_hOutBuf = CreateConsoleScreenBuffer(
            GENERIC_WRITE,//定义进程可以往缓冲区写数据
            FILE_SHARE_WRITE,//定义缓冲区可共享写权限
            NULL,
            CONSOLE_TEXTMODE_BUFFER,
            NULL
        );
        m_hOutput = CreateConsoleScreenBuffer(
            GENERIC_WRITE,//定义进程可以往缓冲区写数据
            FILE_SHARE_WRITE,//定义缓冲区可共享写权限
            NULL,
            CONSOLE_TEXTMODE_BUFFER,
            NULL
        );

        //隐藏两个缓冲区的光标
        CONSOLE_CURSOR_INFO cci;
        cci.bVisible = 0;
        cci.dwSize = 1;
        SetConsoleCursorInfo(m_hOutput, &cci);
        SetConsoleCursorInfo(m_hOutBuf, &cci);
    }
    ~DoubleBuffer()
    {
        m_hOutBuf = NULL;
        m_hOutput = NULL;
    }
};

void MusicDemo()
{
    //https://github.com/zhmmmm/ANYTOOL-2.0.0.0.2Version
    system("color 2");
    //D:\Audio\YZFHKM-S.mp3
    string AudioName;
    std::cout << "Please Input Audio File Names :";
    std::cin >> AudioName;
    std::cout << "Player Music Name : " << AudioName << std::endl;
    ATA->LoadMusicsAndPlay(AudioName);

    const int H = 35;
    const int W = 100;
    char MusicName[256] = { 0 };
    strcat_s(MusicName, AudioName.c_str());
    char Buf[128];

    char **Map = NULL;
    Map = new char*[H];
    for (int i = 0; i < H; i++)
    {
        Map[i] = new char[W];
        for (int j = 0; j < W; j++)
        {
            Map[i][j] = 0;
        }
    }

    while (true)
    {
        ATA->GetAudioStreamData(ATA->GetCurPlayMusic(), Buf);
        for (int i = 0; i < H; i++)
        {
            if (Buf[i] <= 0)
            {
                for (int j = 0; j < W; j++)
                {
                    Map[i][j] =  ;
                }
            }
            else if (Buf[i] > W - 1)
            {
                int temp = Buf[i] % W - 1;
                for (int j = 0; j < temp; j++)
                {
                    Map[i][j] = @;
                }
            }
            else
            {
                for (int j = 0; j < Buf[i]; j++)
                {
                    Map[i][j] = @;
                }
            }
            Map[H - 1][W - 1] = 0;
        }
        DB->Begin(Map, W);
        ATA->GetAudioStreamData(ATA->GetCurPlayMusic(), Buf);
        for (int i = 0; i < H; i++)
        {
            if (Buf[i] <= 0)
            {
                for (int j = 0; j < W; j++)
                {
                    Map[i][j] =  ;
                }
            }
            else if (Buf[i] > W - 1)
            {
                int temp = Buf[i] % W - 1;
                for (int j = 0; j < temp; j++)
                {
                    Map[i][j] = @;
                }
            }
            else
            {
                for (int j = 0; j < Buf[i]; j++)
                {
                    Map[i][j] = @;
                }
            }
            Map[H - 1][W - 1] = 0;
        }
        DB->End(Map, W);
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                Map[i][j] = 0;
            }
        }

    }

    delete[] Map;
    Map = NULL;
}

class CodeChange
{
public:
    void wcharTochar(char *Buf, const wchar_t* wchar)
    {
        int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
        WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), Buf, len, NULL, NULL);
    }
    void charToWchar(wchar_t *buf, const char* cchar)
    {
        int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0);
        MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), buf, len);
    }
    void UTF_8ToWchar(char *buf, const char* UTF_8)
    {
        int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, UTF_8, strlen(UTF_8), NULL, 0);
        wchar_t wszString[1024] = { 0 };
        ::MultiByteToWideChar(CP_UTF8, NULL, UTF_8, strlen(UTF_8), wszString, wcsLen);
        wszString[wcsLen] = \0;
        wcharTochar(buf, wszString);
    }
    void UnicodeToUTF_8(char *buf, const wchar_t* unicode)
    {
        int len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
        WideCharToMultiByte(CP_UTF8, 0, unicode, -1, buf, len, NULL, NULL);
    }

};

void add(char *buf)
{
    int len = strlen(buf);
    for (int i = 0; i < len / 2; i++)
    {
        buf[i] -= 3;
        buf[len - i - 1] -= 3;
        char tmp = buf[i];
        buf[i] = buf[len - 1 - i];
        buf[len - 1 - i] = tmp;
    }
}
void sub(char *buf)
{
    int len = strlen(buf);
    for (int i = 0; i < len / 2; i++)
    {
        buf[i] += 3;
        buf[len - i - 1] += 3;
        char tmp = buf[i];
        buf[i] = buf[len - 1 - i];
        buf[len - 1 - i] = tmp;
    }
}

char Buf[61] = "                                   ?‏??‏?‌‎?‏?‌??‌‎???‎‍‌?‏";



using namespace std;
//char -> 16
void char_2_Hex(char* Char, char* Hex)
{
    int length = strlen(Char);
    for (size_t i = 0; i < length; i++)
    {
        int tmp = int(Char[i]);
        if (Char[i] < 0)
            tmp = (-1) * Char[i] + 128;
        int high = tmp / 16;
        int low = tmp % 16;
        char HIHG;
        char LOW;

        if (high >= 10)
            HIHG = char(high - 10 + 65);
        else
            HIHG = char(high + 48);

        if (low >= 10)
            LOW = char(low - 10 + 65);
        else
            LOW = char(low + 48);

        Hex[2 * i] = HIHG;
        Hex[2 * i + 1] = LOW;
    }
}
//16 -> char 
void Hex_2_char(char*Hex, char* Char)
{
    int length = strlen(Hex) / 2;
    for (size_t i = 0; i < length; i++)
    {
        int high;
        int low;
        if (int(Hex[2 * i]) >= 65)
            high = int(Hex[2 * i] - 65 + 10);
        else
            high = int(Hex[2 * i] - 48);

        if (int(Hex[2 * i + 1]) >= 65)
            low = int(Hex[2 * i + 1] - 65 + 10);
        else
            low = int(Hex[2 * i + 1] - 48);

        Char[i] = char(high * 16 + low);
    }
}
char* RC4(char* C, char* key)
{
    int S[256];
    int T[256];

    int  count = 0;
    count = strlen(key);

    for (int i = 0; i < 256; i++)
    {
        S[i] = i;
        int tmp = i % count;
        T[i] = key[tmp];
    }

    int j = 0;

    for (int i = 0; i < 256; i++)
    {
        j = (j + S[i] + T[i]) % 256;
        int tmp;
        tmp = S[j];
        S[j] = S[i];
        S[i] = tmp;
    }

    int length = 0;
    length = strlen(C);

    int i;
    i = 0, j = 0;

    for (int p = 0; p < length; p++)
    {

        i = (i + 1) % 256;
        j = (j + S[i]) % 256;
        int tmp;
        tmp = S[j];
        S[j] = S[i];
        S[i] = tmp;

        int k = S[(S[i] + S[j]) % 256];
        C[p] = C[p] ^ k;
    }

    return C;
}
//————————————————
//版权声明:本文为CSDN博主「彬彬逊」的原创文章,遵循 CC 4.0 BY - SA 版权协议,转载请附上原文出处链接及本声明。
//原文链接:https ://blog.csdn.net/qq_40481505/article/details/91182645

#include "Encryption.h"

/****************************************************************************
函数名称: str_to_hex
函数功能: 字符串转换为十六进制
输入参数: string 字符串 cbuf 十六进制 len 字符串的长度。
输出参数: 无
*****************************************************************************/
static int str_to_hex(char *string, unsigned char *cbuf, int len)
{
    BYTE high, low;
    int idx, ii = 0;
    for (idx = 0; idx < len; idx += 2)
    {
        high = string[idx];
        low = string[idx + 1];

        if (high >= 0 && high <= 9)
            high = high - 0;
        else if (high >= A && high <= F)
            high = high - A + 10;
        else if (high >= a && high <= f)
            high = high - a + 10;
        else
            return -1;

        if (low >= 0 && low <= 9)
            low = low - 0;
        else if (low >= A && low <= F)
            low = low - A + 10;
        else if (low >= a && low <= f)
            low = low - a + 10;
        else
            return -1;

        cbuf[ii++] = high << 4 | low;
    }
    return 0;
}

/****************************************************************************
函数名称: hex_to_str
函数功能: 十六进制转字符串
输入参数: ptr 字符串 buf 十六进制 len 十六进制字符串的长度。
输出参数: 无
*****************************************************************************/
static void hex_to_str(char *ptr, unsigned char *buf, int len)
{
    for (int i = 0; i < len; i++)
    {
        sprintf(ptr, "%02x", buf[i]);
        ptr += 2;
    }
}

int HexToStr(char *Hex)
{
    int _0 = 0;
    int _1 = 0;
    char buf[2] = { 0 };
    if (Hex[0] >= 0 && Hex[0] <= 9)
    {
        buf[0] = Hex[0];
        _0 = atoi(buf);
    }
    else
    {
        switch (Hex[0])
        {
        case A:
        {
            _0 = 10;
        }; break;
        case B:
        {
            _0 = 11;
        }; break;
        case C:
        {
            _0 = 12;
        }; break;
        case D:
        {
            _0 = 13;
        }; break;
        case E:
        {
            _0 = 14;
        }; break;
        case F:
        {
            _0 = 15;
        }; break;
        }

    }
    if (Hex[1] >= 0 && Hex[1] <= 9)
    {
        buf[0] = Hex[1];
        _1 = atoi(buf);
    }
    else
    {
        switch (Hex[1])
        {
        case A:
        {
            _1 = 10;
        }; break;
        case B:
        {
            _1 = 11;
        }; break;
        case C:
        {
            _1 = 12;
        }; break;
        case D:
        {
            _1 = 13;
        }; break;
        case E:
        {
            _1 = 14;
        }; break;
        case F:
        {
            _1 = 15;
        }; break;
        }

    }

    return _0 * 16 + _1;
}

void HexToString(unsigned char *buf,char *Hex)
{
    int len = (int)strlen(Hex);
    int index = 0;
    for (int i = 0; i < len; i++)
    {
        unsigned char var = HexToStr(Hex + index);
        buf[i] = var;
        index += 2;
    }
}

int Main()
{
    char data[] = "fkdsjfkldjsalfjldafad5f43a1f35da1f31ad";
    int datalen = sizeof(data);
    int ELen = Encryption::GetELen(datalen);
    int DLen = Encryption::GetDLen(datalen);
    char *Eout = new char[ELen];
    char *Dout = new char[DLen];
    memset(Eout, 0, ELen);
    memset(Dout, 0, DLen);
    //char *Eout = NULL;
    //char *Dout = NULL;

    char Hex[] = "9A06AD5C42ACD9FFE1D70A7FC9F9C1F61C0247605D745B5273C6E8AC5BA778979E0620ECF247B756";
    /*
    ?璡B嶙
    声瘤G`]t[Rs畦琜棡 祢G稸
    */

    unsigned char buf[256] = { 0 };
    HexToString(buf, Hex);

    std::cout << (unsigned char*)buf << std::endl;

    int ret = Encryption::Decryption_s((char*)buf, strlen((const char*)buf), Encryption::GetKey(), Dout, DLen);
    std::cout << Dout << std::endl;

    //int ret = Encryption::Encryption_s(data,datalen,Encryption::GetKey(),Eout,ELen);
    //std::cout << "非16进制数据 " << Eout << std::endl;
    //Encryption::print((unsigned char*)Eout);
    //ret = Encryption::Decryption_s(Eout, datalen, Encryption::GetKey(), Dout,DLen);
    //std::cout << Dout << std::endl;
    //Encryption::print((unsigned char*)Dout);

    if (Eout && Dout)
    {
        delete[] Eout;
        Eout = NULL;
        delete[] Dout;
        Dout = NULL;
    }
    system("pause");
    return 0;
}


//#include <iostream>
//#include <conio.h>
//using namespace std;
//
//
//void are7(int(&arr)[3][4]); //引用的方式
//void are6(int(*arr)[3][4]); //二维数组指针的方式
//void are5(int *arr, int, int); //一维指针方式
//void are4(int **arr, int, int); //动态分配
//void are3(int(*arr)[4], int); //传入高
//void are2(int arr[][4], int); //传入高
//void are1(int arr[][4]); //高不用填,但一定要一个列
//
//int main()
//{
//    //int arr2[3][4] = {
//    //    { 1, 2, 3 },
//    //    { 4, 5, 6, 7 },
//    //    { 8, 9, 10, 11 },
//    //};
//    //are1(arr2);//二维数组直接传递,退化为指针
//    //are2(arr2, 3);
//    //are3(arr2,3);
//    //int *p = &arr2[0][0];
//    //are4(&p, 3, 4);
//    //are5((int*)arr2,3,4);
//    //are5(arr2[0],3,4);
//    //are6(&arr2);
//    //are7(arr2);
//    system("pause");
//    return 0;
//}
//void are1(int arr[][4])
//{
//    for (int i = 0; i < 3; i++)
//    {
//        for (int j = 0; j < 4; j++)
//        {
//            cout << arr[i][j] << " ";
//        }
//        cout << endl;
//    }
//}
//void are2(int arr[][4], int len)
//{
//    for (int i = 0; i < len; i++)
//    {
//        for (int j = 0; j < 4; j++)
//        {
//            cout << arr[i][j] << " ";
//        }
//        cout << endl;
//    }
//}
//void are3(int(*arr)[4], int len)
//{
//    for (int i = 0; i < len; i++)
//    {
//        for (int j = 0; j < 4; j++)
//        {
//            std::cout << arr[i][j] << " ";
//        }
//        std::cout << std::endl;
//    }
//}
//void are4(int **arr, int len, int row)
//{
//    arr = new int*[len];
//    for (int i = 0; i < len; i++)
//    {
//        arr[i] = new int[row];
//    }
//    for (int i = 0; i < len; i++)
//    {
//        for (int j = 0; j < row; j++)
//        {
//            std::cout << arr[i][j] << " ";
//        }
//        std::cout << std::endl;
//    }
//}
//void are5(int *arr, int len, int row)
//{
//    for (int i = 0; i < len; i++)
//    {
//        for (int j = 0; j < row; j++)
//        {
//            std::cout << arr[j + i * row] << " ";
//        }
//        std::cout << std::endl;
//    }
//}
//void are6(int(*arr)[3][4])
//{
//    for (int i = 0; i < 3; i++)
//    {
//        for (int j = 0; j < 4; j++)
//        {
//            std::cout << (*arr)[i][j] << " ";
//        }
//        std::cout << std::endl;
//    }
//}
//void are7(int(&arr)[3][4])
//{
//    for (int i = 0; i < 3; i++)
//    {
//        for (int j = 0; j < 4; j++)
//        {
//            std::cout << arr[i][j] << " ";
//        }
//        std::cout << std::endl;
//    }
//}

 

C++ 杂项

标签:amd   ptr   das   名称   _for   items   invalid   lib   lpcwstr   

原文地址:https://www.cnblogs.com/YZFHKMS-X/p/11780514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!