精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>C/C++>>网络与通讯>>网络蚂蚁源代码(一)

主题:网络蚂蚁源代码(一)
发信人: u_rover(柠檬树)
整理人: wenbobo(2003-08-23 10:59:18), 站内信件
/*  
网络蚂蚁源代码(不完全)  
小弟曾深觉网络蚂蚁写得不错,得闲之时便用ida反汇编几行, 时日一久  
竟得其概要. 有感于作者之技术全面, 现贴出来与各位共享.  

其中的sub1() sub2() sub3()虽然已反汇编出来, 其作用尚不很明白,  
估计是用于数据校验的. 望知者不吝赐教.  
*/  

// Component for Drag&Drop, from drop.dll  
class CDropTarget : IDropTarget  
{  
public:  
    HWND hChildWnd;  
    HWND hMainWnd;  
    cRefCount;  
};  

CDropTarget::CDropTarget(HWND hChild, HWND hMain)  
{  
    hChildWnd = hChildWnd;  
    hMainWnd = hMain;  
    cRefCount = 0;  
}  
      
CDropTarget* CreateDropTarget(HWND hChildWnd, HWND hMainWnd)  
{  
    CDropTarget* pDropTarget = new CDropTarget(hChildWnd, hMainWnd);  
    pDropTarget->AddRef();  

    // lock an object to ensure that it stays in memory.  
    CoLockObjectExternal((IUnknown*)pDropTarget, 1, 0);  
    RegisterDragDrop(hMainWnd, (IDropTarget*)pDropTarget);  

    return pDropTarget;  
}  

//------------------------------------------------------------------------------  

const char NullChar[]    = '\0';  
const char language[]    = "language";  
const char Language[]    = "Language";  
const char English[]    = "English"  
const char String[]        = "String";  

char* GetModuleName(char* ModuleFileName)  
{  
    char* ret, p;  

    if (ModuleFileName == NULL)  
        return 0;;  

    for (ret = p = ModuleFileName; *p; p++)  
        if (*p == '\\' || *p == ':')  
            ret = p + 1;  

    return ret;  
}  

void CentralizeWindow(HWND hWnd)  
{  
    RECT r;  
    int cx, cy;  

    GetWindowRect(hWnd, &r);  
    cx = r.right - r.left;  
    cy = r.bottom - r.top;  

    SetWindowPos(hWnd, NULL,  
        (GetSystemMetrics(SM_CXSCREEN) - cx) / 2,  
        (GetSystemMetrics(SM_CYSCREEN) - cy) / 2, cx, cy, SWP_NOSIZE | SWP_NOZORDER);  
}  

LPPICTURE LoadOemSplash(IStream* is)  
{  
    OFSTRUCT of;  
    DWORD NumOfBytesRead, FileSize, FileSizeHigh;  
    HANDLE hFile;  
    HGLOBAL hGlb;  
    LPPICTURE pPicture;  

    if ((hFile = OpenFile((LPCSTR)is, &of, OF_READ)) == HFILE_ERROR)  
        return NULL;  

    FileSize = GetFileSize(hFile, &FileSizeHigh);  

    if ((hGlb = GlobalAlloc(GMEM_MOVEABLE, FileSize)) == 0) {  
        ShowErrMsg("GlobalAlloc");  
        CloseHandle(hFile);  
        return NULL;  
    }  

    ReadFile(hFile, GlobalLock(hGlb), FileSize, &NumOfBytesRead, 0);  

    if (NumOfBytesRead != FileSize) {  
        ShowErrMsg("ReadFile");  
        CloseHandle(hFile);  
        GlobalUnlock(hGlb);  
        GlobalFree(hGlb);  
        return NULL;  
    }  

    CloseHandle(hFile);  
    GlobalUnlock(hGlb);  

    if (CreateStreamOnHGlobal(hGlb, 1, &is)) {  
        ShowErrMsg("IStorage->CreateStream");  
        GlobalFree(hGlb);  
        return NULL;  
    }  

    if (OleLoadPicture(is, 0, FALSE, IID_IPicture, (LPVOID *)&pPicture)) {  
        is->Release();  
        return NULL;  
    }  

    is->Release();  
    return pPicture;  
}  

HPALETTE CreatePaletteForBmp(LPBITMAPINFOHEADER pBmp, int* ColorCount)  
{  
    LPLOGPALETTE lpLp;  
    HPALETTE hPal;  

    *ColorCount = (pBmp->biBitCount <= 8)? (1 << pBmp->biBitCount) : 0;  

    if (*ColorCount == 0)  
        return 0;  

    if ((lpLp = (LPLOGPALETTE)malloc(sizeof(LOGPALETTE) + *ColorCount * sizeof(DWORD))) == NULL) {  
        ShowErrMsg("Malloc");  
        return 0;  
    }  

    lpLp->palVersion = 0x300;  
    lpLp->palNumEntries = *ColorCount;  

    if (*ColorCount > 0) {  
        LPRGBQUAD lpRgb = (char*)pBmp + sizeof(BITMAPINFOHEADER);  
        LPPALETTEENTRY lpPe = &lpLp->palPalEntry;  
        for (i = 0; i < *ColorCount; i++) {
lpPe->peRed = lpRgb->rgbRed;  
            lpPe->peBlue = lpRgb->rgbBlue;  
            lpPe->peGreen = lpRgb->rgbGreen;  
            lpPe->peFlags = 0;  
            lpPe++;  
            lpRgb++;  
        }  
    }  

    if ((hPal = CreatePalette(lpLp)) == NULL)  
        ShowErrMsg("CreatePalette");  

    free(lpLp);  

    return hPal;  
}  

HBITMAP LoadDIBitmap(HINSTANCE hInst, short RID, int* p)  
{  
    HDC hDC;  
    HRSRC hRsc;  
    HGLOBAL hGlb;  
    HPALETTE hPal;  
    HBITMAP hBmp;  
    int ColorCount;  

    if ((hDC = GetDC(NULL)) == NULL) {  
        ShowErrMsg("GetDC");  
        return 0;  
    }  

    if ((hRsc = FindResource(hInst, MAKEINTRESOURCE(RID), RT_BITMAP)) == NULL) {  
        ShowErrMsg("FindResource");  
        return 0;  
    }  

    if ((hGlb = LoadResource(hInst, hRsc)) == NULL) {  
        ShowErrMsg("LoadResource");  
        ReleaseDC(NULL, hDC);  
        return 0;  
    }  

    if ((lpv = LockResource(hRsc)) == NULL) {  
        ShowErrMsg("LockResource");  
        ReleaseDC(NULL, hDC);  
        FreeResource(hRsc);  
        return 0;  
    }  

    if ((hPal = CreatePaletteForBmp((LPBITMAPINFOHEADER)lpv, &ColorCount)) == NULL) {  
        ReleaseDC(NULL, hDC);  
        FreeResource(hRsc);  
        return 0;  
    }  

    *p = hPal;  

    SelectPalette(hDC, hPal, FALSE);  
    RealizePalette(hDC);  

    if ((hBmp = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpv, CBM_INIT, lpv + *(WORD*)lpv + ColorCount * sizeof(RGBQUAD), (BITMAPINFO)lpv, 0)) == NULL) {  
        ShowErrMsg("CreateDIBitmap");  
        ReleaseDC(NULL, hDC);  
        FreeResource(hRsc);  
        return 0;  
    }  

    ReleaseDC(NULL, hDC);  
    FreeResource(hRsc);  
    return hBmp;  
}  

LRESULT SplashWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)  
{  
    PAINTSTRUCT ps;  
    LPPICTURE pPicture;  
    HBITMAP hBmp;  
    RECT r;  
    short width;  

    switch (uMsg)  
    {  
    case WM_CREATE:  
        if (!SetTimer(hWnd, 14, 2000, NULL))  
            ShowErrMsg("SetTimer");  
        CentralizeWindow(hWnd);  
        return 0;  

    case WM_DESTROY:  
        KillTimer(hWnd, 14);  
        return 0;  

    case WM_PAINT:  
        if ((hDC = BeginPaint(hWnd, &ps) == 0) {  
            ShowErrMsg("BeginPaint");  
            return 0;  
        }  

        if (gsOemSplash) {  
            char OemSplash[MAX_PATH];  
            sprintf(OemSplash, "%s\\%s", ModuleDir, gsOemSplash);  
            pPicture = LoadOemSplash(OemSplash);  
        }  
        else  
            pPicture = NULL;  

        if (pPicture) {  
            pPicture->get_Width(&width);  
            pPicture->get_Height(&wParam);  
            pPicture->Render(hDC, 0, 0, 300, 200, 0, wParam, width, -wParam, 0);  
            pPicture->Release();  
        }  
        else {  
            if ((uMsg = CreateCompatibleDC(hDC)) == NULL) {  
                ShowErrMsg("CreateCompatibleDC");  
                EndPaint(&ps);  
                return 0;  
            }  

            if ((hBmp = LoadDIBitmap(gInstance, MAKEINTRESOURCE(138), &lParam)) == NULL) {  
                EndPaint(&ps);  
                DeleteDC(uMsg);  
                return 0;  
            }  

            SelectPalette(hDC, (HPALETTE)lParam, FALSE);  
            RealizePalette(hDC);  

            SelectPalette(uMsg, (HPALETTE)lParam, FALSE);  
            RealizePalette(uMsg);  

            hBmp = SelectObject(uMsg, hBmp);  
            BitBlt(hDC, 0, 0, 300, 200, uMsg, 0, 0, SRCCOPY);  

            DeleteObject(SelectObject(uMsg, hBmp));  
            DeleteDC(uMsg);  
            DeleteObject((HGDIOBJ)lParam);  
        }  

        if (gbOemTitle) {  
            uMsg = SelectObject(hDC, GetStockObject(ANSI_FIXED_FONT));  
            SetRect(&r, 3, 3, 0, 0);  
            DrawText(hDC, gsOEMTitle, -1, &r, DT_CALCRECT | DT_SINGLELINE);  
            r.right += 4;  
            r.bottom += 4;  
            FillRect(hDC, &r, GetStockObject(BLACK_BRUSH));  
            SetTextColor(hDC, RGB(0xff, 0xff, 0xff));  
            SetBkMode(hDC, TRANSPARENT);  
            DrawText(hDC, gsOEMTitle, -1, &r, DT_SINGLELINE | DT_VCENTER | DT_CENTER);  
            SelectObject(hDC, uMsg);  
        }  
        EndPaint(&ps);  
        return 0;  

    case WM_LBUTTONDOWN:  
    case WM_TIMER:  
        DestroyWindow(hWnd);  
        return 0;  

    default:  
        return DefWindowProc(hWnd, uMsg, wParam, lParam);  
    }  
}  

BOOL ShowSplashWindow()  
{  
    HKEY hKey;  
    HWND hWnd;  
    BOOL NoSplash;  
    DWORD sz;  

    WNDCLASS wc;  

    RegOpenKeyEx(HKEY_CURRENT_USER, "Software\NetAnts\Layout", NULL, KEY_ALL_ACCESS, &hKey);  
    if (RegQueryValueEx(hKey, "NoSplash", NULL, NULL, &NoSplash, &sz))  
        NoSplash = FALSE;  

    if (!NoSplash) {  
        wc.s tyle = 0;  
        wc.lpfnWndProc = SplashWinProc;  
        wc.cbClsExtra = 0;  
        wc.cbWndExtra = 0;  
        wc.hInstance = ghInstance;  
        wc.hIcon = 0;  
        wc.hCursor = LoadCursor(0, IDC_ARROW);  
        wc.hbrBackground = 0;  
        wc.lpszMenuName = 0;  
        wc.plszClassName = "NA_Splash";  
        if (!RegisterClass(&wc)) {  
            ShowErrMsg("RegisterClass");  
            return FALSE;  
        }  

        hWnd = CreateWindowEx(0x88h, "NA_Splash, 0, 0x96000000, 0, 0, 300, 200, 0, 0, ghInstance, 0);  
        if (hWnd == 0) {  
            ShwoErrMsg("CreateWindowEx(Splash)");  
            return FALSE;  
        }  
        ShowWindow(hWnd);  
        UpdateWindow(hWnd);  
    }  

    return TRUE;  
}  

BOOL NetInit()  
{  
    WNDCLASS wc;  
    WSADATA wd;  

    wc.s tyle = 0;  
    wc.cbClsExtra = 0;  
    wc.cbWndExtra = 0;  
    wc.hIcon = 0;  
    wc.hCursor = 0;  
    wc.hbrBackground = 0;  
    wc.plszMenuName = 0;  
    wc.lpfnWndProc = DefWindowProc;  
    wc.hInstance = ghInstance;  
    wc.plszClassName = "Dummy";  
    if (!RegisterClass(&wc)) {  
        ShowErrMsg("RegisterClass");  
        return FALSE;  
    }  

    if (WSAStartup(0x101, &wd))  
        return 0;  
    if ((byte)wd.wVersion != 1 || *((byte*)&wd.wVersion + 1) != 1) {  
        WSACleanup();  
        return 0;  
    }  
}  

int CreateContextMenu(HMENU hMenu)  
{  
    HMENU hContextMenu;  
    LanguageData* ld;  
    MENUITEMINFO mi;  
    int i;  

    if ((mi.hbmpItem = CreatePopupMenu()) == NULL) {  
        ShowErrMsg("CreatePopupMenu");  
        return -1;  
    }  

    for (i = 0, ld = gLanguageDataPtr; ld; ld = ld->next) {  
        ZeroMemory(&mi, sizeof(mi));  
        mi.fType = MFT_STRING;  
        mi.wID = 50000 + i;  
        mi.cbSize = sizeof(mi);  
        mi.fMask = MIIM_ID | MIIM_TYPE | MIIM_DATA;  
        mi.dwItemData = (ULONG_PTR)ld;  
        mi.dwTypeData = (LPTSTR)ld;  
        if (InsertMenuItem(mi.hbmpItem, i++, TRUE, &mi) == 0) {  
            ShowErrMsg("InsertMenuItem");  
            return -1;  
        }  
    }  

    if (AppendMenu(hMenu, MF_SEPARATOR, 0, 0) == 0 ||  
        AppendMenu(hMenu, MF_POPUP, mi.hbmpItem, "&Language") == 0)  
    {  
        ShowErrMsg("AppendMenu");  
        return -1;  
    }  

    return 1;  
}  

int ClearOwnerDrawMenu(HMENU hMenu, BOOL IsTop)  
{  
    MENUITEMINFO mii;  
    int sz = sizeof(mii) - sizeof(mii.hbmpItem);  

    if (!IsMenu(hMenu))  
        return -1;  

    // This member is available only for Windows 98 and Windows 2000 or later,  
    // The menu can display items using text, bitmaps, or both.  
    for (mii.hbmpItem = 0; mii.hbmpItem < GetMenuItemCount(hMenu); mii.hbmpItem++) {
memset(mii, 0, sz); // for windows 95 compatibility
mii.cbSize = sz;
mii.fMask = MIIM_SUBMENU | MIIM_TYPE | MIIM_DATA;
mii.dwTypeData = 0;
mii.cch = 0;

if (!GetMenuItemInfo(hMenu, mii.hbmpItem, TRUE, &mii)) {
ShowErrMsg("GetMenuItemInfo");
return -1;
}
if (mii.hSubMenu) {
ClearOwnerDrawMenu(mii.hSubMenu, FALSE);
if (IsTop) continue;
}

int type = mii.fType;
if (type & MFT_OWNERDRAW) {
MenuData* md = mii.dwItemData;
if (md) {
memset(&mii, 0, sz);
mii.cbSize = sz;
mii.fMask = MIIM_TYPE | MIIM_DATA;
mii.fType = type & ~MFT_OWNERDRAW;
mii.dwTypeData = &md->str;  
                mii.dwItemData = md->OrgItemData;  
                if (!SetMenuItemInfo(hMenu, mii.hbmpItem, TRUE/*by pos*/, &mii)) {  
                    ShowErrMsg("SetMenuInfo");  
                    return -1;  
                }  
                free(md);  
            }  
        }  
    }  

    return 1;  
}  

void MinimizeWindow(HWND hWnd)  
{  
    ShowWindow(hWnd, SW_HIDE);  
}  

typedef struct BandwidthNode  
{  
    int Bandwidth;  
    HEVENT hEvent;  
    BandwidthNode * next;  
};  

int DoMenuLocalization(HMENU hMenu) // @41925c  
{  
    char *ptr, String1[100], String2[100];  
    MENUITEMINFO mi;  

    mi.hbmpItem = 0;  
    if (GetMenuItemCount(hMenu) > 0) do {  
        ZeroMemory(&mi);  
        mi.dwTypeData = &String2;  
        mi.cbSize = sizeof(mi);  
        mi.fMask = MIIM_ID | MIID_SUBMENU | MIIM_TYPE;  
        mi.cch = sizeof(String2);  
        if (GetMenuItemInfo(hMenu, mi.hbmpItem, TRUE, &mi) == 0) {  
            ShowErrMsg("GetMenuItemInfo");  
            return -1;  
        }  
        if (mi.hSubMenu) DoMenuLocalization(mi.hSubMenu);  

        if (mi.fType == MFT_STRING) {  
            if ((ptr = strchr(String2, '\t')) != NULL) *ptr = '\0';  
            GetPrivateProfileString(String, String2, &NullChar, String1, sizeof(String1), gpCurrentLanguage->LanguagePath);  
            if (lstrlen(String1) == 0) lstrcpy(String1, String2);  
            ZeroMemory(&mi, sizeof(mi));  
            mi.dwTypeData = &String1;  
            mi.cbSize = sizeof(mi);  
            mi.fMask = MIIM_TYPE;  
            mi.fType = MFT_STRING;  
            if (SetMenuItemInfo(hMenu, mi.hbmpItem, TRUE, &mi) == 0) {  
                ShowErrMsg("SetMenuItemInfo");  
                return -1;  
            }  
        }  
    } while (++mi.hbmpItem < GetMenuItemCount(hMenu));

return 1;
}

HMENU LocalizeMenu(HINSTANCE hInst, LPCTSTR lpMenuName)
{
HMENU hMenu;

if ((hMenu = LoadMenu(hInst, lpMenuName)) == NULL)
return NULL;

if (lstrlen(gLanguageDataPtr->LanguagePath))  
        DoMenuLocalization(hMenu);  

    return hMenu;  
}  
typedef struct tagMenuData  
{  
    HMENU hMenu;  
    char str[100];  
    int ImageIndex;  
    int OrgItemData;  
} MenuData;  

typedef struct tagIndexMap  
{  
    int index;  
    int MenuId;  
} IndexMap;  

IndexMap aIndexMap[38] = {  
    {0, -1},  {1,40041},  {2,40042}, {3,40045}, {4, 40006},  
    {4, 40053}, {5, 40007}, {5,40054}, {6,40010}, {6, 40046},  
    {7, 40011}, {8,40012},  {9,40013}, {10,40014},{11, 40015},  
    {12,40016}, {13,40017}, {14,40018},{15,40501},{16, 40019},  
    {17,40001}, {18,40004}, {19,40025},{19,40049},{19, 40028},  
    {20,40022}, {20,40068}, {20,40073},{21,40021},{21, 40066},  
    {22,40002}, {23,40020}, {24,40023},{25,40044},{26, 40043},  
    {27,40069}, {28,40071}, {28,40072}  
};  

int GetImageIndex(int ID)  
{  
    int i = 0;  
    IndexMap* im = &aIndexMap[0];  

    do {  
        if (im->MenuId == ID)  
            break;  
        im++;  
        i++;  
        if (im >= md2list + sizeof(aIndexMap)  
            return -1;  
    } while (1);  

    return im->index;  
}  

int LocalizeContextMenu(HMENU hMenu, BOOL SkipLocaleSubMenuItem)  
{  
    char *ptr, String2[100];  
    MENUITEMINFO mi;  
    HMENU hm = hMenu;  
    MenuData* md;  

    if (!IsMenu(hMenu))  
        return -1;  

    hm = hMenu;  
    hMenu = 0;  
    if (GetMenuItemCount(hm) > 0) do {  
        ZeroMemory(&mi);  
        mi.dwTypeData = &String2;  
        mi.cbSize = sizeof(mi);  
        mi.fMask = MIIM_ID | MIID_SUBMENU | MIIM_TYPE | MIIM_DATA;  
        mi.cch = sizeof(String2);  
        if (GetMenuItemInfo(hm, hMenu, TRUE, &mi) == 0) {  
            ShowErrMsg("GetMenuItemInfo");  
            return -1;  
        }  
        if (mi.hSubMenu) {  
            LocalizeContextMenu(mi.hSubMenu, FALSE);  
            if (SkipLocaleSubMenuItem) continue;  
        }  

        if (mi.fType == MFT_STRING) {  
            if ((md = (MenuData*)malloc(sizeof(MenuData))) == NULL) {  
                ShowErrMsg("Malloc");  
                return -1;  
            }  
            md.hMenu = hm;  
            lstrcpy(md.str, String2);  
            md.ImageIndex= GetImageIndex(mi.wID);  
            md.OrgItemData = mi.dwItemData;  

            ZeroMemory(&mi, sizeof(mi));  
            mi.dwTypeData = 0;  
            mi.cbSize = sizeof(mi);  
            mi.fMask = MIIM_TYPE | MIIM_DATA;  
            mi.fType = MFT_OWNERDRAW;  
            mi.dwItemData = md;  
            if (SetMenuItemInfo(hm, hMenu, TRUE, &mi) == 0) {  
                ShowErrMsg("SetMenuItemInfo");  
                free(md);  
                return -1;  
            }  
        }  
    } while (++hMenu < GetMenuItemCount(hm));

return 1;
}

[关闭][返回]