⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inbox.cxx

📁 这个是微软WINCE的OBEX协议例子
💻 CXX
📖 第 1 页 / 共 4 页
字号:
                DWORD dwPromptForUpload, dwPromptForOverWrite;
                if(FAILED(GetUserSecurityPrefs(&dwPromptForUpload, &dwPromptForOverWrite)))
                {
                    ResetConnection(pConn);  
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs); 
                    return TRUE;
                }
                                
                //verify the user WANTS the file                
                if(1 == dwPromptForUpload && IDNO == MessageBox(NULL, L"Do you want to receive?", L"Allow Upload?", MB_YESNO | MB_SETFOREGROUND))
                {
                    ResetConnection(pConn);  
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs); 
                    return TRUE;
                }
                                
                //try to open the file, if it exists prompt the user before overwriting
                HANDLE hExistCheck = INVALID_HANDLE_VALUE;
                
                if(NULL != pConn->szName[0])
                    hExistCheck = CreateFile (pConn->szName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
                   
                if(INVALID_HANDLE_VALUE != hExistCheck)
                {
                    CloseHandle(hExistCheck);
                    hExistCheck = INVALID_HANDLE_VALUE;
                    if(1 == dwPromptForOverWrite && IDNO == MessageBox(NULL, L"A file by this name already exists, do you want to overwrite?", L"Allow OverWrite?", MB_YESNO | MB_SETFOREGROUND))
                    {                
                        ResetConnection(pConn);  
                        LeaveCriticalSection (&g_cs);
                        pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                        EnterCriticalSection (&g_cs); 
                        return TRUE;
                    }
                }
                ASSERT(INVALID_HANDLE_VALUE == hExistCheck);
                
                //create a file with this name
                ASSERT(INVALID_HANDLE_VALUE == pConn->hFile);
                pConn->hFile = CreateFile (pConn->szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);                                
                                
                //set the connections mode
                pConn->uiMode = PUT_MODE;  
            } 
            else if (((pOT->pObex->aPropID[i] == OBEX_HID_BODY) ||
                (pOT->pObex->aPropID[i] == OBEX_HID_BODY_END)) && (pConn->hFile != INVALID_HANDLE_VALUE)) {
                
                DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : got %s segment len %d\n", pOT->uiConnectionId, (pOT->pObex->aPropID[i] == OBEX_HID_BODY) ? L"Body" : L"EndOfBody", pOT->pObex->aPropVar[i].caub.cuc));
                
                DWORD written = 0;
                if((! WriteFile(pConn->hFile, pOT->pObex->aPropVar[i].caub.puc, 
                    pOT->pObex->aPropVar[i].caub.cuc, &written, 0)) || (written < pOT->pObex->aPropVar[i].caub.cuc)) {
                    ResetConnection(pConn);
                    DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : error writing file %d\n", pOT->uiConnectionId, GetLastError ()));
                }
                else
                    pConn->uiBytesRecv += written;

                
                if ((pOT->pObex->aPropID[i] == OBEX_HID_BODY_END) && (pConn->hFile != INVALID_HANDLE_VALUE)) {
                    DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : EndOfBody reached\n", pOT->uiConnectionId));

                    //we must close the file here so that the inbox service can move the file
                    CloseHandle (pConn->hFile);
                    pConn->hFile = INVALID_HANDLE_VALUE;
                    
                    if(0 != pConn->uiBytesRecv)             
                        PassToInboxService(pOT, pConn);
                    
                    ResetConnection(pConn);          
                }
            }
        }
        
        //fill in the response type... if this is a final packet (as said
        //  by the client, send OK(0x20), otherwise tell the client that
        //  we are good to continue (OBEX_STAT_CONTINUE) <-- continue
        //  is the default set when the struct is created
        if(pOT->pObex->fFinal == OBEX_OP_ISFINAL) {
            DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : reached final packet\n", pOT->uiConnectionId));
            oc.uiResp = 0x20;

            //reset the connection (this will close up files / buffers / etc)
            if(pConn && 0 != pConn->uiBytesRecv && pConn->hFile != INVALID_HANDLE_VALUE)
            {
               CloseHandle (pConn->hFile);
               pConn->hFile = INVALID_HANDLE_VALUE;
               PassToInboxService(pOT, pConn);
            }       
            if(pConn)
               ResetConnection(pConn);
        }       
        
        //send off the command
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_RESPOND, pOT->uiTransactionId, &oc);
        EnterCriticalSection (&g_cs);
        
        return TRUE;
    } else if(pOT->pObex->uiOp == OBEX_OP_GET){
        return ServiceOPGet(pOT);
    } else if (pOT->pObex->uiOp == (OBEX_OP_SETPATH & OBEX_OP_OPMASK)) {
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] ServiceRequest : OBEX_OP_SETPATH : accepting, doing nothing\n"));
        
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_OK, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;
    }  else if (pOT->pObex->uiOp == (OBEX_OP_ABORT & OBEX_OP_OPMASK)) {
        
         //if this is a PUT packet, fetch the state information for the connection
        //  then put all the data into a file named with the OBEX_HID_NAME
        //  value.  
        InboxState *pConn = g_pConn;
        
        //search for the connection ID (the stored state)
        while (pConn) {
            if (pConn->uiConnectionId == pOT->uiConnectionId)
                break;        
            pConn = pConn->pNext;
        }    
            
        //reset the connection
        ResetConnection(pConn);
        
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_OK, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;
    }

    
    
    //if we get here, a packet of unknown type was recieved.... tell the server to 
    //  shutdown the connection
    DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] ServiceRequest : Unknown code 0x%08x for conn 0x%08x\n", pOT->pObex->uiOp, pOT->uiConnectionId));
    return FALSE;
}



/*****************************************************************************/
/*   DllServiceCallback                                                      */
/*   init the dll, but most importantly call CloseConnection on all pending  */
/*   connections when told to unload                                         */
/*****************************************************************************/
extern "C" int DllServiceCallback (ObexTransaction *pOT) {
    int iRet = FALSE;
    
    EnterCriticalSection (&g_cs);
    
    switch (pOT->uiOp) {
    case OBEX_REQ_INIT:
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] DllServiceCallback : OBEX_REQ_INIT\n"));
        iRet = TRUE;
        break;
        
    case OBEX_REQ_UNLOAD:
        ASSERT (! g_pConn);
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] DllServiceCallback : OBEX_REQ_UNLOAD\n"));
        while (g_pConn) {
            InboxState *pNext = g_pConn->pNext;
            CloseConnection (pOT, g_pConn);
            g_pConn = pNext;
        }
        
        iRet = TRUE;
        break;
        
    case OBEX_REQ_CLOSE:
        {
            DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] DllServiceCallback : OBEX_REQ_CLOSE\n"));
            InboxState *pParent = NULL;
            InboxState *pConn = g_pConn;
            while (pConn) {
                if (pConn->uiConnectionId == pOT->uiConnectionId) {
                    if (! pParent)
                        g_pConn = g_pConn->pNext;
                    else
                        pParent->pNext = pConn->pNext;
                    
                    CloseConnection (pOT, pConn);
                    
                    iRet = TRUE;
                    break;
                }
                
                pParent = pConn;
                pConn = pConn->pNext;
            }
        }
        break;
        
    case OBEX_REQ_REQUEST:
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] DllServiceCallback : OBEX_REQ_REQUEST\n"));
        iRet = ServiceRequest (pOT);
        if (! iRet) {
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);
            iRet = TRUE;
        }
        break;
        
    default:
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] DllServiceCallback : Unrecognized request 0x%08x\n", pOT->uiOp));
    }
    
    LeaveCriticalSection (&g_cs);
    
    return iRet;
}


/*****************************************************************************/
/*   DllMain                                                                 */
/*     Initilize critical sections                                           */
/*****************************************************************************/
BOOL APIENTRY DllMain(HANDLE hInst, DWORD dwReason, LPVOID lpReserved) 
{
    if (dwReason == DLL_PROCESS_ATTACH) {
        g_hMod = (HINSTANCE)hInst;
        
        svsutil_Initialize();
        InitializeCriticalSection (&g_cs);
        DisableThreadLibraryCalls((HMODULE)hInst);
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] loaded\n"));
        
        //open our key
        HKEY hkey;
        DWORD cb, dwType, dwRes;
        if ((ERROR_SUCCESS != (dwRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, OBEX_INBOX_BASE_REG, 0, KEY_QUERY_VALUE, &hkey)))) { 
            DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] Couldnt find reg key for default box\n"));
            wcscpy (szInboxDir, L"\\My Documents");          
        } else {
            //look for the value   
            cb=MAX_PATH * sizeof(WCHAR);
            if((ERROR_SUCCESS != (dwRes = RegQueryValueEx(hkey, L"BaseDir", NULL, &dwType, (LPBYTE)&szInboxDir[0], &cb))))  {
                DEBUGMSG(INBOX_GENERAL_ZONE, (L"Inbox: Couldnt find reg key for default box\n"));
                wcscpy (szInboxDir, L"\\My Documents");
            }
            RegCloseKey(hkey);
        }
        
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] Default inbox using: %s as directory\n", szInboxDir));
    
    } else if (dwReason == DLL_PROCESS_DETACH) {
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] -- Inbox going away...\n"));
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"OBEX_Deinit: (%d) memory objects not closed.. \n", svsutil_TotalAlloc()));

#if defined(DEBUG) || defined(_DEBUG)
        DEBUGMSG(1, (L"\n\n\nUNFREED MEMORY = %d bytes\n\n\n", svsutil_TotalAlloc()));  
        svsutil_LogCallStack();            
        SVSUTIL_ASSERT(0 == svsutil_TotalAlloc());       
#endif
        svsutil_DeInitialize();
        DeleteCriticalSection (&g_cs);
    }
    
    return TRUE;
}


/*****************************************************************************/
/*   DllRegisterServer                                                       */
/*     create necessary reg entries                                            */
/*****************************************************************************/
extern "C" int DllRegisterServer (void) {
    WCHAR szModule[_MAX_PATH];
    
    if (! GetModuleFileName(g_hMod, szModule, sizeof(szModule)/sizeof(WCHAR)))
        return HRESULT_FROM_WIN32(GetLastError());
    
    HKEY hk;
    DWORD dwDisp = 0;
    if (ERROR_SUCCESS != RegCreateKeyEx (HKEY_LOCAL_MACHINE, OBEX_INBOX_BASE_REG,
        0, NULL, 0, KEY_ALL_ACCESS, NULL, &hk, &dwDisp))
        return HRESULT_FROM_WIN32(GetLastError());
    
    int iRet = S_OK;
    if (ERROR_SUCCESS != RegSetValueEx (hk, L"Server", 0, REG_SZ, 
        (LPBYTE)szModule, 
        (wcslen (szModule) + 1) * sizeof(WCHAR)))
        iRet = HRESULT_FROM_WIN32(GetLastError());
    
    RegCloseKey (hk);
    
    return iRet;
}



/*****************************************************************************/
/*   DllUnregisterServer                                                     */
/*     delete reg entries                                                      */
/*****************************************************************************/
extern "C" int DllUnregisterServer (void) {
    if (ERROR_SUCCESS == RegDeleteKey (HKEY_LOCAL_MACHINE, OBEX_INBOX_BASE_REG))
        return S_OK;
    
    return GetLastError ();
}





#ifdef DEBUG
//note: these are all represented as bits, the first in the list is the least sig
//  of the 16 
DBGPARAM dpCurSettings = {
    TEXT("ObexInbox"), {
        /*D*/
        TEXT("General"),
            TEXT("Connect packets"), 
            TEXT("Disconnect packets"), 
            TEXT("Get Packets"),
            /*C*/
            TEXT("Put Packets"),
            TEXT("DLL Service Related"), 
            TEXT("Abort Packets"),
            TEXT(""),
            /*B*/
            TEXT(""),
            TEXT(""),
            TEXT(""),
            TEXT(""),
            /*A*/
            TEXT(""),
            TEXT(" Rel"),
            TEXT(""),
            TEXT("") 
            //0xABCD
    },0xFFFF
};
#endif


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -