1.UTF8字符串->UNICODE字符串->ANSI字符串
char szBuff[MAX_PATH];
int iLen = MultiByteToWideChar( CP_UTF8 , 0 , szBuff , -1 , NULL , 0 );
LPWSTR lpw = new WCHAR[iLen + 1];
ZeroMemory( lpw , sizeof(WCHAR) * (iLen+1) );
MultiByteToWideChar( CP_UTF8 , 0 , szBuff , -1 , lpw , iLen+1 );
int iLen2 = WideCharToMultiByte( CP_ACP , 0 , lpw , -1 , NULL , 0 , NULL , NULL );
char * lpc = new char[iLen2+1];ZeroMemory(lpc,sizeof(char)*(iLen2+1));
WideCharToMultiByte( CP_ACP , 0 , lpw , -1 , lpc , iLen2+1 , NULL , NULL);
ANSI转化为UNICODE字符串 使用StringCchPrintfW和L"%S"
// Allocate a (stack) buffer for the Unicode version of the pathname
SIZE_T cchSize = lstrlenA(pszLibFile) + 1; //取得ansi字符串长度
PWSTR pszLibFileW = (PWSTR)
_alloca(cchSize * sizeof(wchar_t)); //为wchar array分配空间
// Convert the ANSI pathname to its Unicode equivalent
StringCchPrintfW(pszLibFileW, cchSize, L"%S", pszLibFile); //将ansi字符串输出到刚刚分配的空间
2.构造没有重复时间的时间池,把冲突事件都增加1分钟
typedef set<CTime> TimePoolType;//<atltime.h>
TimePoolType allTime;
void IncreTimeUntilNoConfilicit(CTime & t)
{
CTimeSpan ts( 0L , 0 , 1 , 0 );
while( allTime.find(t) != allTime.end() )
t = t + ts;
}
void AddATime(CTime & t)
{
if ( allTime.find(t) != allTime.end() )
{
IncreTimeUntilNoConfilicit(t);
}
allTime.insert( t );
}
for_each( allTime.begin() , allTime.end() , [](const CTime & t)
{CString csFmt = t.Format(TEXT("%Y%m%d%H%M%S\n"));OutputDebugString( csFmt );} );
3.改变图片大小
#define SafeDelete(puk) if ( puk ){ delete puk; puk = NULL; }
BOOL ScaleImage( LPCTSTR lpImageFile, int iWidth, int iHight, bool bScale )//bScale是否按原宽高比例缩放
{
if ((lpImageFile == NULL) || !PathFileExists(lpImageFile))
{
return FALSE;
}
Gdiplus::Bitmap* pTemp = NULL;
{
Gdiplus::Bitmap bmp(lpImageFile);
float fOrgWidth = bmp.GetWidth();
float fOrgHight = bmp.GetHeight();
if (bScale)
{
float fScale = fOrgWidth / fOrgHight;
if (fScale > 1.0)
{
iWidth = (float)(iHight) * fScale;
}
else
{
iHight = (iWidth) / fScale;
}
}
pTemp = new Gdiplus::Bitmap(iWidth, iHight, bmp.GetPixelFormat());
if( pTemp )
{
Gdiplus::Graphics* myGraphics = Gdiplus::Graphics::FromImage(pTemp);
if( myGraphics )
{
myGraphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
RectF rcDest(0, 0, iWidth, iHight);
myGraphics->DrawImage(&bmp, rcDest, 0, 0, fOrgWidth, fOrgHight - 45, UnitPixel); // 去掉底部控制条
myGraphics->Flush();
delete myGraphics;
}
}
}
if (pTemp)
{
CLSID encoderClsid;
GetEncoderClsid(L"image/jpeg", &encoderClsid);
Status sta = pTemp->Save(lpImageFile, &encoderClsid);
SafeDelete(pTemp);
return TRUE;
}
return FALSE;
}
4.查找一个窗口的符合条件的子窗口
BOOL Fn(HWND hWnd)
{
TCHAR className[MAX_PATH];
::GetClassName( hWnd , className , MAX_PATH );
if (_tcscmp(className , TEXT("BFAxWindow")) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
HWND TraversalHWND(HWND hWnd , BOOL (*fn)(HWND))//hWnd为顶层窗口,当符合fn条件时,返回找到的第一个
{
if ( !hWnd ) return 0;
if ( fn(hWnd) )
{
return hWnd;
}
else
{
HWND child = ::GetFirstChild(hWnd);
while ( child )
{
HWND result = TraversalHWND(child,fn);
if ( result )
{
return result;
}
child = ::GetNextWindow(child , GW_HWNDNEXT);
}
}
return 0;
}
BOOL IsWndHasChild(HWND hwnd)
{
return(::GetFirstChild(hwnd) != NULL);
}
VOID TraversalHWNDAndGetAll(HWND hWnd , BOOL (*fn)(HWND) , list<HWND> & resultVec)//查找hWnd的子窗口里符合fn的所有窗口的窗体句柄
{
if ( !hWnd )
{
return;
}
list<HWND> queue;
queue.push_back(hWnd);
while( !queue.empty() )
{
HWND hCurrentWnd = queue.front();
queue.pop_front();
if ( fn(hCurrentWnd) )
{
resultVec.push_back(hCurrentWnd);
}
if ( IsWndHasChild(hCurrentWnd) )
{
HWND hChild = ::GetFirstChild(hCurrentWnd);
while(hChild)
{
queue.push_back(hChild);
hChild = ::GetWindow(hChild , GW_HWNDNEXT);
}
}
}
}
5.WTL目录包含的顺序
#define WIN32_LEAN_AND_MEAN
#define _WTL_USE_CSTRING
#include <atlbase.h> // 基本的ATL类
#include <atlapp.h> // 基本的WTL类
extern CAppModule _Module; // WTL 派生的CComModule版本
#include <atlwin.h> // ATL 窗口类
#include <atlframe.h> // WTL 主框架窗口类
#include <atlmisc.h> // WTL 实用工具类,例如:CString
#include <atlcrack.h> // WTL 增强的消息宏
6.解析json文件
std::ifstream ifs;
ifs.open(L"a.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if ( !reader.parse(ifs , root , false) )
{
return;
}
CString name = root["cid"].asCString();
CString url = root["url"].asCString();
OutputDebugString(name);
OutputDebugString(url);
OutputDebugString(TEXT("======================"));
int n = root["programs"].size();
CString csN;
csN.Format(TEXT("%d"),n);
OutputDebugString(csN);
for ( int i = 0 ; i < n ; ++i )
{
CString csTime = root["programs"][i]["time"].asCString();
__int64 i64 = _wtoi64( csTime );
OutputDebugString(csTime);
}
7.用Http协议下载某个文件(WinHttpClient)
bool Download(TCHAR * szJsFileUrl , TCHAR * szJsFilePath)
{
WinHttpClient downloadClient(szJsFileUrl);
downloadClient.SetTimeouts(RESOLVE_TIMEOUT , CONNECT_TIMEOUT , SEND_TIMEOUT , RECEIVE_TIMEOUT);//这几个宏设置超时时间 解析超时时间 连接超时时间 发送超时时间 接收超时时间
downloadClient.SetUserAgent(L"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; ...)");
BOOL bRet = downloadClient.SendHttpRequest();
if ( bRet )
{
return downloadClient.SaveResponseToFile( szJsFilePath );
}
return false;
}
8.用WinINet上载某个文件
使用http协议
void UploadFile(LPSTR lpFilePath)
{
LPCSTR rn="\r\n";
HINTERNET hSession=0;
HINTERNET hConnect=0;
HINTERNET hRequest=0;
DWORD dwNumberOfBytesWritten=0;
DWORD dwBytesSend=0;
INTERNET_BUFFERS BufferIn;
DWORD dwFlag;
LPCTSTR boundary=TEXT("---------------------------upload"); //随机字符串
LPCSTR aboundary= "---------------------------upload"; //ansi
HANDLE hFile;
hFile=CreateFileA(lpFilePath,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
DWORD dwFileSize=GetFileSize(hFile,0);
TCHAR content_type[128];
_stprintf_s(content_type,TEXT("Content-Type: multipart/form-data; boundary=%s"),boundary);
//LPTSTR referer=TEXT("Referer: http://127.0.0.1/upload/~upload"); //http报文额外的字段,根据需要进行添加
//LPTSTR accept=TEXT("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//LPTSTR accept_lan=TEXT("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
//LPTSTR accept_encoding=TEXT("Accept-Encoding: gzip, deflate");
LPTSTR user_agent=TEXT("User-Agent: Mozilla/4.0");
hSession=InternetOpen(_T("Mozilla/4.0"),
INTERNET_OPEN_TYPE_PRECONFIG,
0,
INTERNET_INVALID_PORT_NUMBER,
0);
if (0==hSession)
{
return;
}
hConnect=InternetConnect(hSession,
TEXT("www.someurlyoucanupload.com"),
80,
_T(""),
_T(""),
INTERNET_SERVICE_HTTP,
0,
0);
if (0==hConnect)
{
InternetCloseHandle(hSession);
return;
}
dwFlag=INTERNET_FLAG_NO_COOKIES;
hRequest=HttpOpenRequest(hConnect,
_T("POST"),
_T("/upload.php"),
HTTP_VERSION,
0, //Referrer
0, //AcceptTypes
dwFlag,
0);
if (0==hRequest)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return;
}
HttpAddRequestHeaders(hRequest,content_type,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //添加请求头
//HttpAddRequestHeaders(hRequest,referer,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
//HttpAddRequestHeaders(hRequest,accept,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
//HttpAddRequestHeaders(hRequest,accept_lan,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
//HttpAddRequestHeaders(hRequest,accept_encoding,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
BYTE* lpBuffer=(BYTE*)VirtualAlloc(0,dwFileSize,MEM_COMMIT,PAGE_READWRITE);
if (0==lpBuffer)
{
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return;
}
DWORD dwRead;
ReadFile(hFile,lpBuffer,dwFileSize,&dwRead,0);
CloseHandle(hFile);
char first_boundary[128];
char delimiter[128];
char end_boundary[128];
sprintf_s(first_boundary , "--%s\r\n" , aboundary); //form的开始边界
sprintf_s(delimiter , "\r\n--%s--\r\n" , aboundary ); //当有多个需要提交时,用delimiter分隔
sprintf_s(end_boundary , "\r\n--%s--\r\n" , aboundary ); //结束边界
LPSTR lpFileName = PathFindFileNameA( lpFilePath );
char content_dispos[MAX_PATH] = {0};
sprintf_s( content_dispos , "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" , lpFileName );
LPSTR content_type2="Content-Type: application/octet-stream\r\n\r\n";
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwFileSize
+strlen(first_boundary)
+strlen(content_dispos)
+strlen(content_type2)
+strlen(end_boundary); //Content-Length:
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;
if (!HttpSendRequestEx(hRequest,&BufferIn,0,0,0))
{
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return;
}
InternetWriteFile(hRequest,(byte*)first_boundary,strlen(first_boundary),&dwNumberOfBytesWritten); //first boundary
InternetWriteFile(hRequest,(byte*)content_dispos,strlen(content_dispos),&dwNumberOfBytesWritten);
InternetWriteFile(hRequest,(byte*)content_type2,strlen(content_type2),&dwNumberOfBytesWritten);
InternetWriteFile(hRequest,lpBuffer,dwFileSize,&dwNumberOfBytesWritten);
//如果还有其他文件
//InternetWriteFile(hRequest,(byte*)delimiter,strlen(delimiter),&dwNumberOfBytesWritten); //deimiter
//InternetWriteFile(hRequest,(byte*)content_dispos,strlen(content_dispos),&dwNumberOfBytesWritten);
//InternetWriteFile(hRequest,(byte*)content_type2,strlen(content_type2),&dwNumberOfBytesWritten);
//...
InternetWriteFile(hRequest,(byte*)end_boundary,strlen(end_boundary),&dwNumberOfBytesWritten);//last boundary
HttpEndRequest(hRequest,0,0,0);
BYTE bResponseData[MAX_PATH];
ZeroMemory( bResponseData , sizeof(bResponseData) );
DWORD dwNumberOfBytesRead = 0;
if ( InternetReadFile(hRequest , bResponseData , MAX_PATH , &dwNumberOfBytesRead) )
{
//如果读取返回页面成功
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
VirtualFree(lpBuffer,0,MEM_RELEASE);
}
9.枚举一个进程下所有加载的模块
hthSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId); //从dwProcessId进程创建快照,并找到pszLibFile相同名字的dll模块,从me.modBaseAddr获取基地址
if (hthSnapshot == INVALID_HANDLE_VALUE) __leave;
// Get the HMODULE of the desired library
MODULEENTRY32W me = { sizeof(me) };
BOOL bFound = FALSE;
BOOL bMoreMods = Module32FirstW(hthSnapshot, &me);
for (; bMoreMods; bMoreMods = Module32NextW(hthSnapshot, &me)) {
bFound = (_wcsicmp(me.szModule, pszLibFile) == 0) ||
(_wcsicmp(me.szExePath, pszLibFile) == 0);
if (bFound) break;
}