![]() |
WGX library Architecture - Reference Manual - Guides |
|
00001 /* 00002 * WGX - Windows GUI Extended Library. 00003 * Copyright (c) 2007-2010 by Dmitri Arkhangelski (dmitriar@gmail.com). 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00027 #define WIN32_NO_STATUS 00028 #include <windows.h> 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <time.h> 00032 00033 #include "wgx.h" 00034 00035 #define MAX_GA_REQUEST_LENGTH 1024 00036 char ga_request[MAX_GA_REQUEST_LENGTH]; 00037 00038 typedef HRESULT (__stdcall *URLMON_PROCEDURE)( 00039 /* LPUNKNOWN */ void *lpUnkcaller, 00040 LPCSTR szURL, 00041 LPTSTR szFileName, 00042 DWORD cchFileName, 00043 DWORD dwReserved, 00044 /*IBindStatusCallback*/ void *pBSC 00045 ); 00046 URLMON_PROCEDURE pURLDownloadToCacheFile; 00047 00048 void DbgDisplayLastError(char *caption); 00049 DWORD WINAPI SendWebAnalyticsRequestThreadProc(LPVOID lpParameter); 00050 00053 static BOOL __stdcall SendWebAnalyticsRequest(char *url) 00054 { 00055 HMODULE hUrlmonDLL = NULL; 00056 HRESULT result; 00057 char path[MAX_PATH + 1]; 00058 00059 if(url == NULL){ 00060 OutputDebugString("SendWebAnalyticsRequest: invalid URL (NULL)!\n"); 00061 return FALSE; 00062 } 00063 00064 OutputDebugString("SendWebAnalyticsRequest: URL = "); 00065 OutputDebugString(url); 00066 OutputDebugString("\n"); 00067 00068 /* load urlmon.dll library */ 00069 hUrlmonDLL = LoadLibrary("urlmon.dll"); 00070 if(hUrlmonDLL == NULL){ 00071 DbgDisplayLastError("SendWebAnalyticsRequest: LoadLibrary(urlmon.dll) failed! "); 00072 return FALSE; 00073 } 00074 00075 /* get an address of procedure downloading a file */ 00076 pURLDownloadToCacheFile = (URLMON_PROCEDURE)GetProcAddress(hUrlmonDLL,"URLDownloadToCacheFileA"); 00077 if(pURLDownloadToCacheFile == NULL){ 00078 DbgDisplayLastError("SendWebAnalyticsRequest: URLDownloadToCacheFile not found in urlmon.dll! "); 00079 return FALSE; 00080 } 00081 00082 /* download a file */ 00083 result = pURLDownloadToCacheFile(NULL,url,path,MAX_PATH,0,NULL); 00084 path[MAX_PATH] = 0; 00085 if(result != S_OK){ 00086 if(result == E_OUTOFMEMORY) OutputDebugString("Not enough memory for SendWebAnalyticsRequest!"); 00087 else OutputDebugString("SendWebAnalyticsRequest: URLDownloadToCacheFile failed!"); 00088 OutputDebugString("\n"); 00089 return FALSE; 00090 } 00091 00092 /* remove cached data, otherwise it may not be loaded next time */ 00093 (void)remove(path); 00094 return TRUE; 00095 } 00096 00099 DWORD WINAPI SendWebAnalyticsRequestThreadProc(LPVOID lpParameter) 00100 { 00101 (void)SendWebAnalyticsRequest((char *)lpParameter); 00102 return 0; 00103 } 00104 00107 static void __stdcall SendWebAnalyticsRequestAsynch(char *url) 00108 { 00109 HANDLE h; 00110 DWORD id; 00111 00112 h = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SendWebAnalyticsRequestThreadProc,(void *)url,0,&id); 00113 if(h == NULL) 00114 DbgDisplayLastError("Cannot create thread for SendWebAnalyticsRequestAsynch!" ); 00115 if(h) CloseHandle(h); 00116 } 00117 00120 void DbgDisplayLastError(char *caption) 00121 { 00122 LPVOID lpMsgBuf; 00123 char buffer[128]; 00124 DWORD error = GetLastError(); 00125 00126 OutputDebugString(caption); 00127 if(!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 00128 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 00129 NULL,error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 00130 (LPTSTR)&lpMsgBuf,0,NULL)){ 00131 (void)_snprintf(buffer,sizeof(buffer), 00132 "Error code = 0x%x",(UINT)error); 00133 buffer[sizeof(buffer) - 1] = 0; 00134 OutputDebugString(buffer); 00135 return; 00136 } else { 00137 OutputDebugString((LPCTSTR)lpMsgBuf); 00138 LocalFree(lpMsgBuf); 00139 } 00140 OutputDebugString("\n"); 00141 } 00142 00147 static char * __stdcall build_ga_request(char *hostname,char *path,char *account) 00148 { 00149 int utmn, utmhid, cookie, random; 00150 __int64 today; 00151 00152 srand((unsigned int)time(NULL)); 00153 utmn = (rand() << 16) + rand(); 00154 utmhid = (rand() << 16) + rand(); 00155 cookie = (rand() << 16) + rand(); 00156 random = (rand() << 16) + rand(); 00157 today = (__int64)time(NULL); 00158 00159 (void)_snprintf(ga_request,MAX_GA_REQUEST_LENGTH, 00160 "http://www.google-analytics.com/__utm.gif?utmwv=4.6.5" 00161 "&utmn=%u" 00162 "&utmhn=%s" 00163 "&utmhid=%u" 00164 "&utmr=-" 00165 "&utmp=%s" 00166 "&utmac=%s" 00167 "&utmcc=__utma%%3D%u.%u.%I64u.%I64u.%I64u.50%%3B%%2B__utmz%%3D%u.%I64u.27.2.utmcsr%%3Dgoogle.com%%7Cutmccn%%3D(referral)%%7Cutmcmd%%3Dreferral%%7Cutmcct%%3D%%2F%3B", 00168 utmn,hostname,utmhid,path,account, 00169 cookie,random,today,today,today,cookie,today 00170 ); 00171 ga_request[MAX_GA_REQUEST_LENGTH - 1] = 0; 00172 return ga_request; 00173 } 00174 00218 BOOL __stdcall IncreaseGoogleAnalyticsCounter(char *hostname,char *path,char *account) 00219 { 00220 return SendWebAnalyticsRequest(build_ga_request(hostname,path,account)); 00221 } 00222 00228 void __stdcall IncreaseGoogleAnalyticsCounterAsynch(char *hostname,char *path,char *account) 00229 { 00230 SendWebAnalyticsRequestAsynch(build_ga_request(hostname,path,account)); 00231 } 00232