作者: admin

  • VC WIN32程序中通过__argc和__wargv获取命令行参数

    VC WIN32 程序中通过__argc__wargv获取命令行参数

    wstring send_qq, send_content;
    if (__argc < 3)
            return -2;
    
    send_qq = __wargv[1];
    send_content = __wargv[2];

    如果是多字节版本的工程,就用 __argv

  • VC模拟鼠标点击

    VC模拟鼠标点击

    SetCursorPos(472, 356);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    Sleep(1000);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

  • PBK拨号文件路径

    PBK拨号文件路径

    WIN7:  

    C:\Users\All Users\Application Data\Microsoft\Network\Connections\Pbk

    XP:    

    C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk

  • VS2013 VC中添加自定义资源

    VS2013 VC中添加自定义资源:

    先添加自定义类型,然后再打开添加资源对话框,点导入,导入的时候选刚才添加的资源类型即可。

  • MFC通过wininet下载文件

    MFC通过wininet下载文件

    // down.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include <windows.h> 
    #include <wininet.h>   
    #include <string> 
    #include <iostream>
    #include <vector>
    using namespace std; 
    #pragma comment(lib, "wininet.lib") 
    
    
    
    
     
    //下载 
    #define  DOWNHELPER_AGENTNAME         "down" 
    #define  LEN_OF_BUFFER_FOR_QUERYINFO  128 
    #define  DOWNLOAD_BUF_SIZE            (10*1024)  //10KB 
    #define  MAX_DOWNLOAD_REQUEST_TIME    10   
    #define  MAX_DOWNLOAD_BYTESIZE        (1000*1024*1024) //1000MB 
     
     
    BOOL _TryHttpSendRequest(LPVOID hRequest, int nMaxTryTimes); //多次发送请求函数 
     
    //HTTP下载函数,通过先请求HEAD的方式然后GET,可以通过HEAD对下载的文件类型和大小做限制 
    BOOL DownloadUrl(std::string strUrl, std::string strFileName) 
    { 
        BOOL bRet = FALSE; 
        if (strUrl == "" || strFileName == "") 
            return FALSE; 
     
        //定义变量 
        HINTERNET hInet = NULL; //打开internet连接handle 
        HINTERNET hConnect = NULL; //HTTP连接 
        HINTERNET hRequestHead = NULL; //HTTP Request 
        HINTERNET hRequestGet = NULL; //HTTP Request 
        HANDLE hFileWrite = NULL; //写文件的句柄 
        char* pBuf = NULL; //缓冲区 
        DWORD dwRequestTryTimes = MAX_DOWNLOAD_REQUEST_TIME; //尝试请求的次数 
        DWORD dwDownBytes = 0; //每次下载的大小 
        DWORD dwDownFileTotalBytes = 0; //下载的文件总大小 
        DWORD dwWriteBytes = 0; //写入文件的大小 
        char bufQueryInfo[LEN_OF_BUFFER_FOR_QUERYINFO] = {0}; //用来查询信息的buffer 
        DWORD dwBufQueryInfoSize = sizeof(bufQueryInfo); 
        DWORD dwStatusCode = 0; 
        DWORD dwContentLen = 0; 
        DWORD dwSizeDW = sizeof(DWORD); 
     
        //分割URL 
        CHAR pszHostName[INTERNET_MAX_HOST_NAME_LENGTH] = {0}; 
        CHAR pszUserName[INTERNET_MAX_USER_NAME_LENGTH] = {0}; 
        CHAR pszPassword[INTERNET_MAX_PASSWORD_LENGTH] = {0}; 
        CHAR pszURLPath[INTERNET_MAX_URL_LENGTH] = {0}; 
        CHAR szURL[INTERNET_MAX_URL_LENGTH] = {0}; 
        URL_COMPONENTSA urlComponents = {0}; 
        urlComponents.dwStructSize = sizeof(URL_COMPONENTSA); 
        urlComponents.lpszHostName = pszHostName; 
        urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH; 
        urlComponents.lpszUserName = pszUserName; 
        urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH; 
        urlComponents.lpszPassword = pszPassword; 
        urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH; 
        urlComponents.lpszUrlPath = pszURLPath; 
        urlComponents.dwUrlPathLength = INTERNET_MAX_URL_LENGTH; 
     
        bRet = InternetCrackUrlA(strUrl.c_str(), 0, NULL, &urlComponents); 
        bRet = (bRet && urlComponents.nScheme == INTERNET_SERVICE_HTTP); 
        if (!bRet) 
        { 
            goto _END_OF_DOWNLOADURL; 
        } 
         
        //打开一个internet连接 
        hInet = InternetOpenA(DOWNHELPER_AGENTNAME, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); 
        if (!hInet) 
        { 
            bRet = FALSE; 
            goto _END_OF_DOWNLOADURL; 
        } 
         
        //打开HTTP连接 
        hConnect = InternetConnectA(hInet, pszHostName, urlComponents.nPort, pszUserName, pszPassword, INTERNET_SERVICE_HTTP, 0, NULL); 
        if (!hConnect) 
        { 
            bRet = FALSE; 
            goto _END_OF_DOWNLOADURL; 
        } 
         
        //创建HTTP request句柄 
        if (urlComponents.dwUrlPathLength !=  0) 
            strcpy(szURL, urlComponents.lpszUrlPath); 
        else 
            strcpy(szURL, "/"); 
         
        //请求HEAD,通过HEAD获得文件大小及类型进行校验 
        hRequestHead = HttpOpenRequestA(hConnect, "HEAD", szURL, "HTTP/1.1", "", NULL, INTERNET_FLAG_RELOAD, 0); 
        bRet = _TryHttpSendRequest(hRequestHead, dwRequestTryTimes); 
        if (!bRet) 
        { 
            goto _END_OF_DOWNLOADURL; //请求HEAD失败 
        } 
        
        //查询content-length大小 
        dwContentLen = 0; 
        dwSizeDW = sizeof(DWORD); 
        bRet = HttpQueryInfo(hRequestHead, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &dwContentLen, &dwSizeDW, NULL); 
        if (bRet) 
        { 
            //检查是否文件过大 
            if (dwContentLen > MAX_DOWNLOAD_BYTESIZE) 
            { 
                bRet = FALSE; 
                goto _END_OF_DOWNLOADURL; 
            } 
        } 
     
        //校验完成后再请求GET,下载文件 
        hRequestGet = HttpOpenRequestA(hConnect, "GET", szURL, "HTTP/1.1", "", NULL, INTERNET_FLAG_RELOAD, 0); 
        bRet = _TryHttpSendRequest(hRequestGet, dwRequestTryTimes); 
        if (!bRet) 
        { 
            goto _END_OF_DOWNLOADURL; //请求HEAD失败 
        } 
     
        //创建文件 
        hFileWrite = CreateFileA(strFileName.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
        if (INVALID_HANDLE_VALUE == hFileWrite) 
        { 
            bRet = FALSE; 
            goto _END_OF_DOWNLOADURL; 
        } 
     
        //分配缓冲 
        pBuf = new char[DOWNLOAD_BUF_SIZE]; //分配内存 
        if (!pBuf) 
        { 
            bRet = FALSE; 
            goto _END_OF_DOWNLOADURL; 
        } 
     
        //多次尝试下载文件 
        dwDownFileTotalBytes = 0; 
        while (1) 
        { 
            dwDownBytes = 0; 
            memset(pBuf, 0, DOWNLOAD_BUF_SIZE*sizeof(char)); 
            bRet = InternetReadFile(hRequestGet, pBuf, DOWNLOAD_BUF_SIZE, &dwDownBytes); 
            if (bRet) 
            { 
                if (dwDownBytes > 0) 
                { 
                    dwDownFileTotalBytes += dwDownBytes; 
                    bRet = WriteFile(hFileWrite, pBuf, dwDownBytes, &dwWriteBytes, NULL); //写入文件 
                    if (!bRet) 
                    { 
                        goto _END_OF_DOWNLOADURL; 
                    } 
                } 
                else if (0 == dwDownBytes) 
                { 
                    bRet = TRUE; 
                    break; //下载成功完成 
                } 
            } 
        } 
         
        //清理 
    _END_OF_DOWNLOADURL: 
        if (INVALID_HANDLE_VALUE != hFileWrite) 
            CloseHandle(hFileWrite); 
        if (pBuf) 
            delete [] pBuf; 
        if (hRequestGet) 
            InternetCloseHandle(hRequestGet); 
        if (hRequestHead) 
            InternetCloseHandle(hRequestHead); 
        if (hConnect) 
            InternetCloseHandle(hConnect); 
        if (hInet) 
            InternetCloseHandle(hInet); 
         
        return bRet; 
    } 
     
    //多次发送请求函数 
    BOOL _TryHttpSendRequest(LPVOID hRequest, int nMaxTryTimes) 
    { 
        BOOL bRet = FALSE; 
        DWORD dwStatusCode = 0; 
        DWORD dwSizeDW = sizeof(DWORD); 
        while (hRequest && (nMaxTryTimes-- > 0)) //多次尝试发送请求 
        { 
            //发送请求 
            bRet = HttpSendRequestA(hRequest, NULL, 0, NULL, 0); 
            if (!bRet) 
            { 
                continue; 
            } 
            else 
            { 
                //判断HTTP返回的状态码 
                dwStatusCode = 0; 
                dwSizeDW = sizeof(DWORD); 
                bRet = HttpQueryInfo(hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatusCode, &dwSizeDW, NULL); 
                if (bRet) 
                { 
                    //检查状态码 
                    if (HTTP_STATUS_OK == dwStatusCode) //200 OK 
                    { 
                        break; 
                    } 
                    else 
                    { 
                        bRet = FALSE; 
                        continue; 
                    } 
                } 
            } 
        } 
     
        return bRet; 
    } 
     
    
    //////////////////////////////////////////////////////////////////////////
    
    BOOL HttpGet(string url, string &result)
    {
         vector<char> v; 
        const CHAR * szUrl = url.c_str(); 
        CHAR szAgent[] = ""; 
        HINTERNET hInternet1 =  
            InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL); 
        if (NULL == hInternet1) 
         { 
            InternetCloseHandle(hInternet1); 
            return FALSE; 
         } 
        HINTERNET hInternet2 =  
            InternetOpenUrl(hInternet1,szUrl,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL); 
        if (NULL == hInternet2) 
         { 
            InternetCloseHandle(hInternet2); 
            InternetCloseHandle(hInternet1); 
            return FALSE; 
         } 
        DWORD dwMaxDataLength = 500; 
        PBYTE pBuf = (PBYTE)malloc(dwMaxDataLength*sizeof(TCHAR)); 
        if (NULL == pBuf) 
         { 
            InternetCloseHandle(hInternet2); 
            InternetCloseHandle(hInternet1); 
            return FALSE; 
         } 
        DWORD dwReadDataLength = NULL; 
        BOOL bRet = TRUE; 
        do  
        { 
            ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR)); 
            bRet = InternetReadFile(hInternet2,pBuf,dwMaxDataLength,&dwReadDataLength); 
            for (DWORD dw = 0;dw < dwReadDataLength;dw++) 
              { 
                v.push_back(pBuf[dw]); 
              } 
         } while (NULL != dwReadDataLength); 
    
        vector<char>::iterator i; 
        for(i=v.begin(); i!=v.end(); i++) 
            result += *i;
    
         return TRUE;
    }
    
    //////////////////////////////////////////////////////////////////////////
     
    /*
    int main(int argc, char* argv[]) 
    { 
        cout << "正在下载..."; 
        BOOL bR = DownloadUrl("http://42.duote.com.cn/office2007.zip", "test.zip"); 
        if (bR) 
            cout << "完成" << endl; 
        else 
            cout << "失败" << endl; 
        return 0; 
    }
    */
    
    
    string trim(const string& str)
    {
         string::size_type pos = str.find_first_not_of(' ');
         if (pos == string::npos)
         {
              return str;
         }
         string::size_type pos2 = str.find_last_not_of(' ');
         if (pos2 != string::npos)
         {
              return str.substr(pos, pos2 - pos + 1);
         }
         return str.substr(pos);
    }
    
    int split(const string& str, vector<string>& ret_, string sep = ",")
    {
         if (str.empty())
         {
              return 0;
         }
        
         string tmp;
         string::size_type pos_begin = str.find_first_not_of(sep);
         string::size_type comma_pos = 0;
        
         while (pos_begin != string::npos)
         {
              comma_pos = str.find(sep, pos_begin);
              if (comma_pos != string::npos)
              {
                   tmp = str.substr(pos_begin, comma_pos - pos_begin);
                   pos_begin = comma_pos + sep.length();
              }
              else
              {
                   tmp = str.substr(pos_begin);
                   pos_begin = comma_pos;
              }
             
              if (!tmp.empty())
              {
                   ret_.push_back(tmp);
                   tmp = "";
              }
         }
         return 0;
    }
    
    string replace(const string& str, const string& src, const string& dest)
    {
         string ret;
        
         string::size_type pos_begin = 0;
         string::size_type pos       = str.find(src);
         while (pos != string::npos)
         {
              cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";
              ret.append(str.data() + pos_begin, pos - pos_begin);
              ret += dest;
              pos_begin = pos + 1;
              pos       = str.find(src, pos_begin);
         }
         if (pos_begin < str.length())
         {
              ret.append(str.begin() + pos_begin, str.end());
         }
         return ret;
    }
    
    void Debug(string str)
    {    
         MessageBox(NULL, str.c_str(), "DEBUG", MB_OK);
    }
    
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
         string html;
         if (!HttpGet("http://42.96.190.81:81/list.txt", html))
         {
              MessageBox(NULL, "列表获取失败!", "ERROR", MB_OK);
              return 0;
         }
        
    
         vector<string> urls;
         split(html, urls, "\r\n");
         for(int i=0; i<urls.size(); i++)
         {
              size_t pos = urls[i].rfind('/', urls[i].size());
              string fileName = urls[i].substr(pos+1, urls[i].size()-pos-1);
    
              if (DownloadUrl(urls[i], fileName))
              {
                   Debug(fileName + "下载成功");
    
              }
    
         }
         Debug("完成");
    
    
    
         return 0;
    }
  • C# 遍历文件下所有文件

    C# 遍历文件下所有文件

    string[] files = Directory .GetFiles(Application.StartupPath, "*.mdb", SearchOption .AllDirectories);

    如果要遍历所有的,把 *.mdb 改成*即可。

  • C#二维数组的定义

    C#二维数组的定义

    string[,] items = new string[_totalCount, 32];

    string[][] = new string[32][]; 是有区别的。

  • notepad++ viewhex插件不准,太坑了,用winhex可正常查看

    notepad++ viewhex插件不准,太坑了,用winhex可正常查看。

    notepad++ viewhex插件
    winhex

    notepad++已经完全弃用了,时不时夹点政治立场,恶心的一批!

  • C#读取十六进制文件

    C#读取十六进制文件

    BinaryReader br = new BinaryReader( File.OpenRead(_fileName));
    br.BaseStream.Position = 0xF; 
    string first_player = br.ReadByte().ToString("X2"); //ToString("X2") 为C#中的字符串格式控制符。X表示十六进制,2为每次都是两位数
    br.Close();
    MessageBox.Show(first_player);
  • Hyper-v XP 不识别网卡的问题

    Hyper-v XP 不识别网卡的问题。

    解决办法:

    把自动生成的网卡删除,重新添加 “旧版网络适配器“ ,然后开机就可以上网了