首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 经验分享,常用代码共享 [已结帖,结帖人:zyx040404]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 结帖率:
    发表于:2008-11-16 16:45:25 楼主
    以下代码是我多年工作中总结的常用代码,其中部分代码是其他人写的代码
    另外也可以参考我发的另外几个关于经验分享的帖子

    经验分享,如何写DLL
    http://topic.csdn.net/u/20080822/00/17bbae38-9936-42b6-ac76-806911e7830a.html

    经验分享,如何写线程
    http://topic.csdn.net/u/20080906/01/1e9aa0c3-6167-4abb-a2b3-91083716edfd.html

    经验分享,如何使用try,catch, throw之一
    http://topic.csdn.net/u/20080823/19/75373f80-7709-44b3-856f-31651910b6e6.html

    经验分享,如何使用try,catch, throw之二
    http://topic.csdn.net/u/20080824/12/abeadfe6-cc1b-4f6d-ab04-93b82a5eab54.html

    经验分享,如何设计代码架构
    http://topic.csdn.net/u/20080830/00/2be80d62-5340-4f6f-a067-368a6215972e.html
    0  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fanlehai
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 16:54:001楼 得分:0
    哈哈,up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • b271737818
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 16:59:412楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Non_Recursive
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 17:03:293楼 得分:0
    up~~~
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 17:20:354楼 得分:0
    我将常用代码写成一个DLL,使用DOT.NET 2003,如果要使用该DLL,就必须要修改项目的C++的默认属性,否则会有内存异常
    请参考http://msdn.microsoft.com/en-us/library/ms235460(VS.80).aspx, 里面有讲具体原因
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 17:22:275楼 得分:0
    当然如果你喜欢可以直接使用原代码
    有些代码写的不一定很好,仅仅当做参考一下
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 17:39:556楼 得分:0
    以下是头文件Util.h

    #pragma   once

    #ifdef UTIL_EXPORTS
    #define UTIL_API __declspec(dllexport)
    #else
    #define UTIL_API __declspec(dllimport)
    #endif

    #include <WINDOWS.H>
    #include <NTSECAPI.H>
    #include "CommonDataDefine.h"

    //-----------------------------------------------------------------------------------------------------
    // classes
    //-----------------------------------------------------------------------------------------------------
    UTIL_API void * CreateILogger();
    UTIL_API void * CreateIRegistryKnob();
    UTIL_API void * CreateIStringMatchService();
    UTIL_API void * CreateIXMLParser();
    UTIL_API void * CreateIScanFile();
    UTIL_API void * CreateIResource();
    UTIL_API void * CreateIDBGW();
    UTIL_API void * CreateIMD5();

    //-----------------------------------------------------------------------------------------------------
    // string functions
    //-----------------------------------------------------------------------------------------------------
    UTIL_API int CompareStringWithoutCase(const TSTRING & s1, const TSTRING & s2);
    UTIL_API size_t FindStringWithoutCase(const TSTRING & s1, const TSTRING & s2);
    UTIL_API TSTRING stoupper(const TSTRING & s);
    UTIL_API TSTRING stolower(const TSTRING & s);
    UTIL_API TSTRING srtrim(TSTRING & s);
    UTIL_API TSTRING sltrim(TSTRING & s);
    UTIL_API TSTRING strim(TSTRING & s);
    UTIL_API void GetRidOfDoubleQuotationMarks(TSTRING & s);
    UTIL_API char * Wstring2Char(const wstring wstr, char* tch);
    UTIL_API TSTRING Bstr2String(const BSTR bstr, TSTRING & sString);
    UTIL_API BSTR String2Bstr(const TSTRING & sString, BSTR bstr);
    UTIL_API TSTRING Int2String(const int iValue);
    UTIL_API TSTRING Int2String(const int iValue, TSTRING & sString);
    UTIL_API void Replace(TSTRING & sSource, const TSTRING & sOld, const TSTRING & sNew);
    UTIL_API TSTRING GetPathFromEnviroment(const TSTRING & sEnvNameWithPercent);
    UTIL_API TSTRING ReplaceEnvToRealValue(const TSTRING &sTempLine);
    UTIL_API int GetStringBytesCount(const unsigned char *str);
    UTIL_API int GetStringCharCount(const unsigned char *str);
    UTIL_API void InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String);
    UTIL_API void CommaSeperatedStrToStrVector(TSTRING& cms, VECSTRING& strvector);


    //-----------------------------------------------------------------------------------------------------
    // time functions
    //-----------------------------------------------------------------------------------------------------
    UTIL_API TSTRING GetTime();
    UTIL_API TSTRING GetIntervalTime(const TSTRING& sBeginTime, const TSTRING& sEndTime);


    //-----------------------------------------------------------------------------------------------------
    // file functions
    //-----------------------------------------------------------------------------------------------------
    UTIL_API BOOL CreateDir(const TSTRING & sPath);
    UTIL_API BOOL CreateFileByPath(const TSTRING & sFile);
    UTIL_API BOOL IsFileExist(const TSTRING & sFile);
    UTIL_API BOOL IsFileValid(const TSTRING & sFile);
    UTIL_API BOOL IsPathValid(const TSTRING & sPath);
    UTIL_API BOOL CreatePathByFullFileName(const TSTRING & sFile);
    UTIL_API void EmptyDir(const TSTRING & sPath);
    UTIL_API void DeleteFiles(const TSTRING & sPath);
    UTIL_API void AddBackslashToPathIfNotPresent(TSTRING & sPath);
    UTIL_API __int64 GetDiskKSizeUsed(const TSTRING & sDrive);
    UTIL_API __int64 GetFileSize(const TSTRING & sFullFileName);
    UTIL_API __int64 GetFolderSize(const TSTRING & sFolder);
    UTIL_API BOOL MoveFile(LPCTSTR src, LPCTSTR dst, BOOL dwflags);
    UTIL_API BOOL CopyDir(const TCHAR* srcDir, const TCHAR* dstDir);
    UTIL_API int RunExeFile(const TSTRING & sCommandLine, BOOL bWait = FALSE, DWORD dwFlags = 0);
    UTIL_API TSTRING GetRunPath();
    UTIL_API TSTRING GetLongPathNameFromShortName(const TSTRING & sShortFileName);
    UTIL_API BOOL IsEmptyFolder(const TCHAR* dir);
    UTIL_API DWORD GetOrignalPathName(LPTSTR pszCurrentPathName, LPTSTR pszOrignalPathName, DWORD cbBuffer);
    UTIL_API size_t GetNewFileIndex(const TSTRING & sFileName, const TCHAR tchFlag = _T('('));


    //-----------------------------------------------------------------------------------------------------
    // OS functions
    //-----------------------------------------------------------------------------------------------------
    //##ModelId=46D462B103A9
    enum OS_RESTART_TYPE
    {
    //##ModelId=46D462B103C8
    UNKNOW_RESTART_TYPE = 0,
    //##ModelId=46D462B103D8
    SHUTDOWN_AND_REBOOT = 1,
    //##ModelId=46D462B20000
    RESTART_DIALOG = 2
    };

    UTIL_API void RestartWindows(const OS_RESTART_TYPE & restartType);
    UTIL_API void SystemShutDownAndReboot();
    UTIL_API void PreventSystemHibernation();

    UTIL_API void CriticalDelay(LONG delayTime);

    UTIL_API void SimpleBeginThread(unsigned ( __stdcall *start_address )( void * ), void * arglist = NULL);

    UTIL_API string GetCPUVenderID();
    UTIL_API string GetCPUSerial();

    //-----------------------------------------------------------------------------------------------------
    // Resource functions
    //-----------------------------------------------------------------------------------------------------
    UTIL_API int GetResString(int uStrID, TSTRING& tsText, LPRECT lpRc = NULL);
    UTIL_API int GetResControl(int eView, int uCtrlID, TSTRING& tsText, LPRECT lpRc = NULL);
    UTIL_API int GetResControl(int eView, int uCtrlID, LPRECT lpRc);
    UTIL_API int LoadNLS(const TSTRING& tsResFile);
    UTIL_API int GetMsgString(int uStrID, TSTRING& tsText);
    UTIL_API int GetResFont(int uFontID, LPLOGFONT lpFt, int eView = -1);

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wuming0404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:22:047楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:22:298楼 得分:0
    以下是CommonDataDefine.h

    #pragma   once


    #include <TCHAR.H>
    #include <shlwapi.h>
    #include <assert.h>
    #include <string>
    #include <vector>
    #include <list>
    #include <map>
    #include <sstream>
    #include <algorithm>
    using namespace std;

    //******************************************************************************************************
    // common data define
    //******************************************************************************************************
    #ifdef _UNICODE
    //##ModelId=46D34E58032E
    typedef wstring TSTRING;

    //##ModelId=46D34E58035B
    typedef wstringstream STRINGSTREAM;
    //##ModelId=46D34E58036C
    typedef wifstream IFSTREAM;
    //##ModelId=46D34E58038A
    typedef wofstream OFSTREAM;
    #else
    typedef string TSTRING;

    typedef stringstream STRINGSTREAM;
    typedef ifstream IFSTREAM;
    typedef ofstream OFSTREAM;
    #endif

    //##ModelId=46D34E58039A
    typedef vector <TSTRING> VECSTRING;
    //##ModelId=46D34E5803B9
    typedef VECSTRING::iterator VECSTRING_IT;
    //##ModelId=46D890D802AF
    typedef VECSTRING::const_iterator VECSTRING_CONST_IT;

    #define NPOS -1

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:31:289楼 得分:0
    #include "stdafx.h"
    #include <algorithm>
    #include <Shlobj.h>
    #include <ShObjIdl.h>
    #include <PROCESS.H>

    #include "Util.h"
    #include "UtilLog/ILogger.h"
    #include "UtilLog/LoggerImpl.h"
    #include "UtilRegularString/IStringMatchService.h"
    #include "UtilRegularString/StringMatchServiceImpl.h"
    #include "UtilRegistry/IRegistryKnob.h"
    #include "UtilRegistry/RegistryKnobImpl.h"
    #include "UtilXML/IXMLParser.h"
    #include "UtilXML/XMLParserImpl.h"
    #include "UtilScanFile/IScanFile.h"
    #include "UtilScanFile/ScanFileImpl.h"
    #include "UtilResource/ResourceImpl.h"
    #include "dbgw/IDBGW.h"
    #include "dbgw/DBGWImpl.h"
    #include "MD5/md5.h"

    using namespace std;

    static CResourceImpl l_Resource;

    #define MAX_PATH_IN_BYTES (MAX_PATH * 2)

    //------------------------------------------------------------------------------------------------------
    // FileSizeK
    //------------------------------------------------------------------------------------------------------
    __int64 FileSizeK(DWORD high, DWORD low)
    {
    __int64 k = 0;
    if (high != 0)
    {
    k = high;
    k < <= 22; // shift left 32 since high order, then right 10 to divide by 1024
    }

    k += (low / 1024);

    return k;
    }

    //------------------------------------------------------------------------------------------------------
    // GetFileSize
    //------------------------------------------------------------------------------------------------------
    __int64 GetFileSize(const TSTRING & sFullFileName, WIN32_FIND_DATA & win32FindData)
    {
    __int64 i64FileSize = 0;

    if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)
    {
    __int64 i64ActualSize = 0;
    DWORD dwActualSizeLow = 0;
    DWORD dwActualSizeHigh = 0;

    dwActualSizeLow =  GetCompressedFileSize(sFullFileName.c_str(), &dwActualSizeHigh);
    if (dwActualSizeHigh > 0)
    {
    i64ActualSize = dwActualSizeHigh;
    i64ActualSize < <= 32;
    }

    i64ActualSize += dwActualSizeLow;
    i64FileSize = (i64ActualSize / 1024);
    }
    else
    i64FileSize = max(FileSizeK(win32FindData.nFileSizeHigh, win32FindData.nFileSizeLow), 1);

    return i64FileSize;
    }


    BOOL APIENTRY DllMain( HANDLE hModule,
      DWORD  ul_reason_for_call,
      LPVOID lpReserved
      )
    {
    return TRUE;
    }

    //******************************************************************************************************
    // structs definition
    //******************************************************************************************************
    //##ModelId=46D34E59000F
    enum SCAN_FILE_ERROR
    {
    //##ModelId=46D34E59006D
    HF_error,
    //##ModelId=46D34E590128
    HF_continue,
    //##ModelId=46D34E590186
    HF_done
    };

    //******************************************************************************************************
    // classes
    //******************************************************************************************************
    //------------------------------------------------------------------------------------------------------
    // CreateILogger
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateILogger()
    {
    CLoggerImpl * pRK = new CLoggerImpl();
    return static_cast <void *> (pRK);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIRegistryKnob
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIRegistryKnob()
    {
    CRegistryKnobImpl* pRK = new CRegistryKnobImpl();
    return static_cast <void *> (pRK);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIStringMatchService
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIStringMatchService()
    {
    CStringMatchServiceImpl* pSMS = new CStringMatchServiceImpl();
    return static_cast <void *> (pSMS);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIXMLParser
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIXMLParser()
    {
    CXMLParser* pXMLParser = new CXMLParser();
    return static_cast <void *> (pXMLParser);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIScanFile
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIScanFile()
    {
    CScanFileImpl* pScanFile = new CScanFileImpl();
    return static_cast <void *> (pScanFile);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIResource
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIResource()
    {
    CResourceImpl* pResource = new CResourceImpl();
    return static_cast <void *> (pResource);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIDBGW
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIDBGW()
    {
    CDBGWImpl* pDBGW = new CDBGWImpl();
    return static_cast <void *> (pDBGW);
    }

    //------------------------------------------------------------------------------------------------------
    // CreateIMD5
    //------------------------------------------------------------------------------------------------------
    UTIL_API void * CreateIMD5()
    {
    CMD5* pMD5 = new CMD5();
    return static_cast <void *> (pMD5);
    }

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wuming0404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:31:4210楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:32:5911楼 得分:0
    //******************************************************************************************************
    // string functions
    //******************************************************************************************************
    //------------------------------------------------------------------------------------------------------
    // CompareStringWithoutCase
    //------------------------------------------------------------------------------------------------------
    UTIL_API int CompareStringWithoutCase(const TSTRING & s1, const TSTRING & s2)
    {
    return _tcsicmp (s1.c_str(), s2.c_str());
    }

    //------------------------------------------------------------------------------------------------------
    // FindStringWithoutCase
    //------------------------------------------------------------------------------------------------------
    UTIL_API size_t FindStringWithoutCase(const TSTRING & s1, const TSTRING & s2)
    {
    TSTRING sLower1 = stolower(s1);
    TSTRING sLower2 = stolower(s2);

    return sLower1.find(sLower2);
    }

    //------------------------------------------------------------------------------------------------------
    // stoupper
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING stoupper(const TSTRING & s)
    {
    TSTRING upper(s);
    for(size_t i=0; i <s.length(); i++)
    upper[i] = _totupper(upper[i]);
    return upper;
    }

    //------------------------------------------------------------------------------------------------------
    // stolower
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING stolower(const TSTRING & s)
    {
    TSTRING lower(s);
    for(size_t i=0; i <s.length(); i++)
    lower[i] = _totlower(lower[i]);
    return lower;
    }

    //------------------------------------------------------------------------------------------------------
    // srtrim
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING srtrim(TSTRING & s)
    {
    s.erase(std::find_if(s.rbegin(), s.rend(),
    std::not1(std::ptr_fun(::isspace))).base(),
    s.end());
    return s;
    }

    //------------------------------------------------------------------------------------------------------
    // sltrim
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING sltrim(TSTRING & s)
    {
    s.erase(s.begin(),std::find_if(s.begin(), s.end(),
    std::not1(std::ptr_fun(::isspace))));
    return s;
    }

    //------------------------------------------------------------------------------------------------------
    // strim
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING strim(TSTRING & s)
    {
    return sltrim(srtrim(s));
    }

    //------------------------------------------------------------------------------------------------------
    // GetRidOfDoubleQuotationMarks
    //------------------------------------------------------------------------------------------------------
    UTIL_API void GetRidOfDoubleQuotationMarks(TSTRING & s)
    {
    if (s.find(_T("\"")) == 0)
    s = s.substr(1);

    if (s.find(_T("\"")) == s.length() - 1)
    s = s.substr(0, s.length() - 1);
    }

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wuming0404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:33:1012楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:34:2413楼 得分:0
    //------------------------------------------------------------------------------------------------------
    // Wstring2Char
    //------------------------------------------------------------------------------------------------------
    UTIL_API char * Wstring2Char(const wstring wstr, char* tch)
    {
    UINT codepage = GetACP();

    int n = 0;

    n = WideCharToMultiByte(codepage, 0, wstr.c_str(), -1, tch, MAX_PATH_IN_BYTES, NULL,NULL);
    tch[n] = '\0';

    return tch;
    }

    //------------------------------------------------------------------------------------------------------
    // Bstr2String
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING Bstr2String(const BSTR bstr, TSTRING & sString)
    {
    TCHAR *tch = NULL;

    try
    {
    size_t iLen = _tcslen(bstr) + 1;
    tch = new TCHAR[iLen];

    if (NULL != tch)
    {
    memset(tch, 0,iLen);
    #ifdef UNICODE
    _tcscpy(tch, bstr);
    #else
    UINT codepage = GetACP();

    int n = WideCharToMultiByte(codepage, 0, bstr, -1, tch, MAX_PATH_IN_BYTES, NULL,NULL);
    tch[n] = '\0';
    #endif
    sString = tch;
    delete tch;
    }
    }
    catch(...)
    {
    if (tch)
    delete tch;
    }

    return sString;
    }

    //------------------------------------------------------------------------------------------------------
    // String2Bstr
    //------------------------------------------------------------------------------------------------------
    UTIL_API BSTR String2Bstr(const TSTRING & sString, BSTR bstr)
    {
    #ifdef UNICODE
    _tcscpy(bstr, sString.c_str());
    #else
    UINT codepage = GetACP();

    int n = 0;

    n = MultiByteToWideChar(codepage, 0, sString.c_str(), -1, bstr, MAX_PATH_IN_BYTES);
    bstr[n] = '\0';
    #endif

    return bstr;
    }

    //------------------------------------------------------------------------------------------------------
    // Int2String
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING Int2String(const int iValue)
    {
    TSTRING sString;
    return Int2String(iValue, sString);;
    }

    //------------------------------------------------------------------------------------------------------
    // Int2String
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING Int2String(const int iValue, TSTRING & sString)
    {
    TCHAR tchValue[20000] = {0};
    _itot(iValue, tchValue, 10);
    sString = tchValue;

    return sString;
    }

    //------------------------------------------------------------------------------------------------------
    // Replace
    //------------------------------------------------------------------------------------------------------
    UTIL_API void Replace(TSTRING & sSource, const TSTRING & sOld, const TSTRING & sNew)
    {
    //basic_string.find will return 0 if  the string is ""
    if (sOld.empty())
    return;

    TSTRING sLine = sSource;
    TSTRING sResult;

    size_t iPos = NPOS;

    while (TRUE)
    {
    iPos = FindStringWithoutCase(sLine, sOld);
    if (iPos == NPOS)
    {
    //get suffix file description
    sResult += sLine;

    break;
    }

    //get pre file description
    sResult = sResult + sLine.substr(0, iPos) + sNew;

    sLine = sLine.substr(iPos + sOld.length());
    }

    sSource = sResult;
    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wuming0404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:34:3314楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:35:0315楼 得分:0
    //------------------------------------------------------------------------------------------------------
    // GetPathFromEnviroment
    //------------------------------------------------------------------------------------------------------
    #define ID_WINENV_SYSTEMROOT_DIRECTORIES _T("%systemroot directory%")
    #define ID_WINENV_WINDOWS_DIRECTORIES _T("%windows directory%")
    UTIL_API TSTRING GetPathFromEnviroment(const TSTRING & sEnvNameWithPercent)
    {
    //check whether sEnvNameWithPercent includes "%" at begin and end position
    if (sEnvNameWithPercent[0] != _T('%') && sEnvNameWithPercent[sEnvNameWithPercent.size() - 1] != '%')
    return _T("");

    TSTRING sPath;

    if (CompareStringWithoutCase(sEnvNameWithPercent, ID_WINENV_SYSTEMROOT_DIRECTORIES) == 0)
    {
    TCHAR tchSystemDirectory[MAX_PATH + 1] = {0};

    GetSystemDirectory(tchSystemDirectory, MAX_PATH);
    sPath = tchSystemDirectory;
    }
    else if (CompareStringWithoutCase(sEnvNameWithPercent, ID_WINENV_WINDOWS_DIRECTORIES) == 0)
    {
    TCHAR tchWindowsDirectory[MAX_PATH + 1] = {0};

    GetWindowsDirectory(tchWindowsDirectory, MAX_PATH);
    sPath = tchWindowsDirectory;
    }
    else
    {
    TCHAR tchPath[MAX_PATH + 1] = {0};

    //ExpandEnvironmentStrings gets the windows env, GetEnvironmentVariable gets the user define env
    ExpandEnvironmentStrings(sEnvNameWithPercent.c_str(), tchPath, MAX_PATH);

    if (0 == CompareStringWithoutCase(sEnvNameWithPercent, tchPath))
    {
    memset(tchPath, 0, sizeof(tchPath));
    TSTRING sEnvName = sEnvNameWithPercent;
    sEnvName = sEnvName.substr(1, sEnvName.size() - 2);
    GetEnvironmentVariable(sEnvName.c_str(), tchPath, MAX_PATH);
    }

    sPath = tchPath;
    }

    //remove the last "\" of tchPath
    if (sPath[sPath.length() - 1] == _T('\\'))
    sPath = sPath.substr(0, sPath.length() - 1);

    return sPath;
    }

    //------------------------------------------------------------------------------------------------------
    // ReplaceEnvToRealValue
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING ReplaceEnvToRealValue(const TSTRING &sTempLine)
    {
    TSTRING sLine = sTempLine;
    TSTRING sNewLine;

    size_t iPos = NPOS;

    while (TRUE)
    {
    iPos = sLine.find(_T("%"));
    if (iPos == NPOS)
    {
    //get suffix file description
    sNewLine += sLine;

    break;
    }

    //get pre file description
    sNewLine += sLine.substr(0, iPos);

    sLine = sLine.substr(iPos + 1);

    iPos = sLine.find(_T("%"));
    if (iPos == NPOS)
    return _T("");

    //get tchPath from env
    TSTRING sEnv = sLine.substr(0, iPos);
    sEnv = _T("%") + sEnv + _T("%");
    TSTRING sPath = GetPathFromEnviroment(sEnv);

    sNewLine += sPath;

    //sub line
    sLine = sLine.substr(iPos + 1);
    }

    return sNewLine;
    }

    //------------------------------------------------------------------------------------------------------
    // GetStringBytesCount
    //------------------------------------------------------------------------------------------------------
    UTIL_API int GetStringBytesCount(const unsigned char *str)
    {
    int iRtn = 0;
    const unsigned char *ptch = str;

    // go to the end of the string
    while (*ptch)
    {
    ptch++;
    iRtn++;
    }
    return iRtn;
    }

    //------------------------------------------------------------------------------------------------------
    // GetStringCharCount
    //------------------------------------------------------------------------------------------------------
    UTIL_API int GetStringCharCount(const unsigned char *str)
    {
    int iRtn = 0;
    const unsigned char *ptch = str;

    // go to the end of the string
    while (*ptch)
    {
    ptch = _mbsinc ((const unsigned char *)ptch);
    iRtn++;
    }
    return iRtn;
    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wuming0404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:35:1316楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:35:3517楼 得分:0
    //------------------------------------------------------------------------------------------------------
    // InitLsaString
    //------------------------------------------------------------------------------------------------------
    UTIL_API void InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String)
    {
    DWORD StringLength;

    if(String == NULL) {
    LsaString->Buffer = NULL;
    LsaString->Length = 0;
    LsaString->MaximumLength = 0;
    return;
    }

    StringLength = lstrlenW(String);
    LsaString->Buffer = String;
    LsaString->Length = (USHORT) StringLength * sizeof(WCHAR);
    LsaString->MaximumLength = (USHORT) (StringLength + 1) *
    sizeof(WCHAR);
    }

    //------------------------------------------------------------------------------------------------------
    // CommaSeperatedStrToStrVector
    //------------------------------------------------------------------------------------------------------
    UTIL_API void CommaSeperatedStrToStrVector(TSTRING& cms, VECSTRING& strvector)
    {
    int startpos = 0;
    int findpos = 0;
    while(findpos != cms.size())
    {
    findpos = (int)cms.find(',', startpos);
    if(findpos == TSTRING::npos)
    findpos = (int)cms.size();
    strvector.push_back(cms.substr(startpos, findpos-startpos));
    startpos = findpos+1;
    }
    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wuming0404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:35:5118楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:36:1219楼 得分:0
    //******************************************************************************************************
    // time functions
    //******************************************************************************************************
    //------------------------------------------------------------------------------------------------------
    // GetTime
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING GetTime()
    {
    SYSTEMTIME systime;
    memset(&systime, 0, sizeof(systime));

    GetLocalTime(&systime);

    TCHAR tchTime[MAX_PATH + 1] = {0};
    _stprintf(tchTime, _T("%02d:%02d:%02d"), systime.wHour, systime.wMinute, systime.wSecond);

    return tchTime;
    }

    //------------------------------------------------------------------------------------------------------
    // GetIntervalTime
    //------------------------------------------------------------------------------------------------------
    UTIL_API TSTRING GetIntervalTime(const TSTRING& sBeginTime, const TSTRING& sEndTime)
    {
    int iBeginTime, iBeginHour, iBeginMinute, iBeginSecond;
    int iEndTime, iEndHour, iEndMinute, iEndSecond;
    int iIntervalTime, iIntervalHour, iIntervalMinute, iIntervalSecond;

    iBeginHour = _ttoi(sBeginTime.substr(0,2).c_str());
    iBeginMinute = _ttoi(sBeginTime.substr(3,5).c_str());
    iBeginSecond = _ttoi(sBeginTime.substr(6).c_str());
    iBeginTime = iBeginHour * 3600 + iBeginMinute * 60 + iBeginSecond;

    iEndHour = _ttoi(sEndTime.substr(0,2).c_str());
    iEndMinute = _ttoi(sEndTime.substr(3,5).c_str());
    iEndSecond = _ttoi(sEndTime.substr(6).c_str());

    if (iEndHour < iBeginHour)
    iEndHour = iEndHour + 24;
    iEndTime = iEndHour * 3600 + iEndMinute * 60 + iEndSecond;

    div_t div_result;
    iIntervalTime = iEndTime - iBeginTime;
    div_result = div(iIntervalTime, 3600);
    iIntervalHour = div_result.quot;

    iIntervalTime = div_result.rem;

    div_result = div(iIntervalTime, 60);
    iIntervalMinute = div_result.quot;
    iIntervalSecond = div_result.rem;

    TCHAR tchTime[9] = {0};
    _stprintf(tchTime, _T("%02d:%02d:%02d"), iIntervalHour, iIntervalMinute, iIntervalSecond);

    return tchTime;
    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:37:0220楼 得分:0
    //******************************************************************************************************
    // file functions
    //******************************************************************************************************
    //------------------------------------------------------------------------------------------------------
    // CreateDir
    //------------------------------------------------------------------------------------------------------
    UTIL_API BOOL CreateDir(const TSTRING & sPath)
    {

    TCHAR dir[MAX_PATH] = {0};
    TCHAR* p = NULL;
    TCHAR* startpos = NULL;
    DWORD err = 0;

    if (sPath.empty())
    return FALSE;

    _tcscpy(dir, sPath.c_str());

    for(startpos = dir; *startpos != NULL && (startpos - dir) < MAX_PATH; startpos = p+1 )
    {
    p = wcschr(startpos, _T('\\'));
    if(p>dir && *(p-1) == _T(':'))
    continue;

    if(p == dir && *(p+1) == _T('\\'))
    { // dirpath start with '\\\\'
    p = wcschr(p+2, _T('\\')); // look up the 3th '\\'
    if(!p)
    break;
    continue;
    }

    if(p!=NULL)
    {
    *p = NULL;
    if(!CreateDirectory(dir, NULL))
    {
    err = GetLastError();
    if(err != ERROR_ALREADY_EXISTS)
    return FALSE;
    }
    *p = _T('\\');
    }else
    {
    if(dir[_tcslen(dir)-1] != _T(':') && !CreateDirectory(dir, NULL))
    {
    err = GetLastError();
    if(err != ERROR_ALREADY_EXISTS)
    return FALSE;
    }

    break;
    }
    }

    return TRUE;
    }

    //------------------------------------------------------------------------------------------------------
    // CreateFileByPath
    //------------------------------------------------------------------------------------------------------
    UTIL_API BOOL CreateFileByPath(const TSTRING & sFile)
    {
    size_t nPos = sFile.rfind(_T('\\'));
    TSTRING sPath = sFile.substr(0, nPos);
    CreateDir(sPath);
    HANDLE hFile = CreateFile(sFile.c_str(), NULL, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (INVALID_HANDLE_VALUE == hFile)
    return FALSE;

    CloseHandle(hFile);
    return TRUE;
    }

    //------------------------------------------------------------------------------------------------------
    // IsFileExist
    //------------------------------------------------------------------------------------------------------
    UTIL_API BOOL IsFileExist(const TSTRING & sFile)
    {
    DWORD attr;
    BOOL result = FALSE;

    WIN32_FIND_DATA win32FindData;

    HANDLE hFile = FindFirstFile(sFile.c_str(), &win32FindData);
    if (hFile == INVALID_HANDLE_VALUE)
    return FALSE;

    attr = win32FindData.dwFileAttributes;
    FindClose(hFile);

    if ( attr != NULL )
    result = (attr & FILE_ATTRIBUTE_DIRECTORY) == 0;

    return result;
    }

    //------------------------------------------------------------------------------------------------------
    // IsFileValid
    //------------------------------------------------------------------------------------------------------
    UTIL_API BOOL IsFileValid(const TSTRING & sFile)
    {
    if (sFile.empty())
    return FALSE;

    if( sFile[1] == _T(':') && sFile[2] == _T('\\') )
    {
    // Make sure the drive is valid
    UINT uDriveType = GetDriveType(sFile.substr(0, 3).c_str());
    if((uDriveType == DRIVE_UNKNOWN || uDriveType == DRIVE_NO_ROOT_DIR))
    return FALSE;
    }
    else if (PathIsUNC( sFile.c_str() ))
    return FALSE;
    else
    return FALSE;

    return TRUE;
    }

    //------------------------------------------------------------------------------------------------------
    // IsPathValid
    //------------------------------------------------------------------------------------------------------
    UTIL_API BOOL IsPathValid(const TSTRING & sPath)
    {
    if (sPath.empty())
    return FALSE;

    HANDLE hFile = CreateFile(sPath.c_str(), NULL, NULL, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL && FILE_ATTRIBUTE_DIRECTORY, NULL);
    if (INVALID_HANDLE_VALUE == hFile)
    {
    DWORD dwErr = GetLastError();
    if (ERROR_ALREADY_EXISTS == dwErr)
    return TRUE;
    else if (ERROR_ACCESS_DENIED == dwErr)
    return TRUE;
    else
    return FALSE;
    }

    DeleteFile(sPath.c_str());

    CloseHandle(hFile);

    return TRUE;
    }

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zyx040404
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-16 18:37:3421楼 得分:0
    //------------------------------------------------------------------------------------------------------
    // CreatePathByFullFileName
    //------------------------------------------------------------------------------------------------------
    UTIL_API BOOL CreatePathByFullFileName(const TSTRING & sFile)
    {
    if(sFile.empty())
    return FALSE;

    TCHAR tchPath[MAX_PATH] = {0};
    _tcscpy(tchPath, sFile.c_str());

    for(size_t i = _tcslen(tchPath) - 1; i >= 0; --i)
    {
    if(tchPath[i] == _T('\\'))
    {
    tchPath[i] = _T('\0');
    if(!CreateDir(tchPath))
    return FALSE;

    break;
    }
    }

    return TRUE;
    }

    //------------------------------------------------------------------------------------------------------
    // EmptyDir
    //------------------------------------------------------------------------------------------------------
    UTIL_API void EmptyDir(const TSTRING & sPath)
    {
    if(sPath.empty())
    return;

    TCHAR buf[MAX_PATH] = {0};
    TCHAR tchPath[MAX_PATH] = {0};

    _tcscpy(buf, sPath.c_str());
    _tcscat(buf, _T("\\*"));

    WIN32_FIND_DATA data;
    memset(&data, NULL, sizeof(data));
    HANDLE handle = FindFirstFile(buf, &data);
    if(handle == INVALID_HANDLE_VALUE)
    return;

    do
    {
    _tcscpy(tchPath, sPath.c_str());
    _tcscat(tchPath, _T("\\"));
    _tcscat(tchPath, data.cFileName);
    if( _tcsicmp(data.cFileName,_T("."))==0 ||  _tcsicmp(data.cFileName,_T(".."))==0)
    continue;
    if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    EmptyDir(tchPath);
    RemoveDirectory(tchPath);
    }
    else
    {
    SetFileAttributes(tchPath,FILE_ATTRIBUTE_NORMAL);
    DeleteFile(tchPath);
    }
    }while(FindNextFile(handle, &data));

    FindClose(handle);
    }

    //------------------------------------------------------------------------------------------------------
    // DeleteFiles
    //------------------------------------------------------------------------------------------------------
    //only delete all files under path, do not delete sub path
    UTIL_API void DeleteFiles(const TSTRING & sPath)
    {
    if(sPath.empty())
    return;

    TCHAR buf[MAX_PATH] = {0};
    TCHAR tchPath[MAX_PATH] = {0};

    _tcscpy(buf, sPath.c_str());
    _tcscat(buf, _T("\\*"));

    WIN32_FIND_DATA data;
    memset(&data, NULL, sizeof(data));
    HANDLE handle = FindFirstFile(buf, &data);
    if(handle == INVALID_HANDLE_VALUE)
    return;

    do
    {
    _tcscpy(tchPath, sPath.c_str());
    _tcscat(tchPath, _T("\\"));
    _tcscat(tchPath, data.cFileName);
    if( _tcsicmp(data.cFileName,_T("."))==0 ||  _tcsicmp(data.cFileName,_T(".."))==0)
    continue;
    if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    ;
    else
    {
    SetFileAttributes(tchPath, FILE_ATTRIBUTE_NORMAL);
    DeleteFile(tchPath);
    }
    }while(FindNextFile(handle