首页
社区
课程
招聘
关于在dll中限制post数据次数的问题?
发表于: 2014-7-5 13:40 3260

关于在dll中限制post数据次数的问题?

2014-7-5 13:40
3260
post是用的libcurl,
情况1:放在单独一个exe中测试

int WriteToLog(char* str);
void  postdata(int flag)
{
    GetMacAddress(g_strMacAdress);
    USES_CONVERSION;
    char *pMac=T2A(g_strMacAdress);
    char fullAdress[MAX_PATH]={0};
    if(flag==1)
    {
        strcat(fullAdress,"&name=钟欣桐");
    }
    else if(flag==2)
    {
        strcat(fullAdress,"&name=张柏芝");
    }
    else if(flag==3)
    {
        strcat(fullAdress,"&name=陈文媛");
    }
    else if(flag==4)
    {
        strcat(fullAdress,"&name=颜颖思");
    }
    else if(flag==5)
    {
        strcat(fullAdress,"&name=章子怡");
    }
    else
    {
        strcat(fullAdress,“&name=陈冠希”);
    }
    CURL *curl;   
    CURLcode res;   
    //FILE *fptr;   
    struct curl_slist *http_header = NULL;   
 
    //if ((fptr = fopen(FILENAME, "w")) == NULL) {   
    //    fprintf(stderr, "fopen file error: %s\n", FILENAME);   
    //   exit(1);   
    //}   
    curl = curl_easy_init();  
    if(flag==0)
    {
        curl_easy_setopt(curl, CURLOPT_URL, POSTURL2);   
    }
    else
    {
        curl_easy_setopt(curl, CURLOPT_URL, POSTURL);   
    }
    WriteToLog("post函数内部");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fullAdress);   
    curl_easy_setopt(curl, CURLOPT_POST, 1);   
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);   
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);   
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);   
    res = curl_easy_perform(curl);   
    curl_easy_cleanup(curl);   
    WriteToLog("post函数退出");
}
#define LOGFILE "D:\\log.txt"
 
int WriteToLog(char* str)
{
    FILE* log;
    log = fopen(LOGFILE, "a+");
    if (log == NULL){
        OutputDebugString(L"Log file open failed.");
        return -1;
    }
    fprintf(log, "%s\n", str);
    fclose(log);
    return 0;
}
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////post部分限制///////////////////////////
//软件是否初次运行增加注册表项
BOOL IsFirstRun()
{
    HKEY hKey;
    LPCTSTR lpRun=_T("Software\\Microsoft\\Windows\\CurrentVersion\\test");
    long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_WRITE, &hKey);
    if(lRet==ERROR_SUCCESS)
    {
        //g_firstrun=false;
        return false;
    }
    else
    {
        //第一次运行
        if(ERROR_SUCCESS==RegCreateKey(HKEY_CURRENT_USER,_T("Software\\Microsoft\\Windows\\CurrentVersion\\test"),&hKey))
        {
            DWORD nCount=1;
            RegSetValueEx(hKey,_T("tj"),0,REG_DWORD,(BYTE*)&nCount,sizeof(DWORD));
            //创建当前时间、日期
            TCHAR szCurrentDataTime[MAX_PATH]={0};
            SYSTEMTIME systm;
            GetLocalTime(&systm);
            wsprintf(szCurrentDataTime,_T("%4d年%d月%d日"),systm.wYear,systm.wMonth,systm.wDay);
            lRet=RegSetValueEx(hKey,L"data",0,REG_SZ,(BYTE*)szCurrentDataTime,/*lstrlen(szCurrentDataTime)*sizeof(TCHAR)*/MAX_PATH);
            if(lRet==ERROR_SUCCESS)
            {
                WriteToLog("成功");
            }
            else
            {
                WriteToLog("失败");
            }
            RegCloseKey(hKey);
            return true;
        }
        else
        {
            return false;
        }
    }
}
 
BOOL tjLimited()
{
    IsFirstRun();
    //创建当前时间、日期
    TCHAR szCurrentDataTime[MAX_PATH]={0};
    SYSTEMTIME systm;
    GetLocalTime(&systm);
    wsprintf(szCurrentDataTime,_T("%4d年%d月%d日"),systm.wYear,systm.wMonth,systm.wDay);
    HKEY hKey;
    TCHAR szData[MAX_PATH]={0};
    DWORD nCount=0;
    DWORD dType=REG_SZ;
    DWORD dTypecount=REG_DWORD;
    DWORD dReturn=MAX_PATH;
    LPCTSTR lpRun=_T("Software\\Microsoft\\Windows\\CurrentVersion\\test");
    long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_WRITE|KEY_READ, &hKey);
    if(lRet==ERROR_SUCCESS)
    {
        //继续下层
        lRet=RegQueryValueEx(hKey,L"tj",NULL,(LPDWORD)(&dTypecount),(LPBYTE)&nCount,&dReturn);
        if(lRet==ERROR_SUCCESS)
        {
            dReturn=MAX_PATH;
            lRet=RegQueryValueEx(hKey,L"data",NULL,&dType,(LPBYTE)szData,&dReturn);
            DWORD n=GetLastError();
            if(lRet==ERROR_SUCCESS)
            {
                //WriteToLog("chenggong");
            }
            else
            {
 
                //WriteToLog("shibai");
            }
            //自增
            ++nCount;
            RegSetValueEx(hKey,L"tj",0,REG_DWORD,(BYTE*)&nCount,sizeof(DWORD));
            if(lstrcmp(szData,szCurrentDataTime))
            {
                WriteToLog("位置2");
                lstrcpy(szData,szCurrentDataTime);
                nCount=0;
                //更新注册表
                DWORD count=2;
                RegSetValueEx(hKey,L"data",0,REG_SZ,(BYTE*)szCurrentDataTime,lstrlen(szCurrentDataTime)*sizeof(TCHAR*));
                RegSetValueEx(hKey,L"tj",0,REG_DWORD,(BYTE*)&count,sizeof(DWORD));
                return true;
            }
            else
            {
                //只判断点击次数
                if(nCount>2)
                {
                    WriteToLog("超过次数啦");
                    return false;
                }
                else
                {
                    ///////////////////////////////////日志打印////////////
                    //char log[10]={0};
                    //sprintf(log,"%d",nCount);
                    //char fulllog[50]={0};
                    //strcpy(fulllog,"post数据");
                    //strcat(fulllog,log);
                    //WriteToLog(fulllog);
                    ////////////////////////////////////////////////////////
                    return true;
                }
            }
 
        }
        else
        {
            //没有内容,进行添加
            //DWORD n=GetLastError();
            WriteToLog("位置1");
            DWORD count=1;
            RegSetValueEx(hKey,L"data",0,REG_SZ,(BYTE*)szCurrentDataTime,lstrlen(szCurrentDataTime)*sizeof(TCHAR*));
            RegSetValueEx(hKey,L"tj",0,REG_DWORD,(BYTE*)&count,sizeof(DWORD));
            return true;
        }
    }
}
 
int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE, PTSTR pszCmdLine, int) {
    while(1)
    {
        if(tjLimited())
        {
            WriteToLog("post开始了");
            postdata(0);
        }
        Sleep(10000);
    }
    return(0);
}

单独放一个工程里面运行,发现web那边就只会受到1次post数据,也是正确的。
情况2:
BOOL WINAPI DllMain(                HINSTANCE hinstDLL,
               DWORD fdwReason,
                   LPVOID lpvReserved
)
{
 switch( fdwReason ) 
    { 
        case DLL_PROCESS_ATTACH:
            {
if(tjLimited())
                    {
                        WriteToLog("post开始啦");
                        postdata(0);
                    }

情况2的dll是一个全局钩子,也就是会被所有进程加载,代码都是一摸一样的,在dll对应的exe模块中也是一个while循环。

但是现在的问题是运行情况2时候的exe(+dll),web端会收到不止一条post的数据,隔阵子web收到一条,有时候1分钟内收到2条等等,而且时间也是随机的,请问各位大大这是啥原因导致的呢,谢谢

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 128
活跃值: (2788)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
艳照门?
2014-7-5 16:07
0
游客
登录 | 注册 方可回帖
返回
//