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

📄 inbox.cxx

📁 这个是微软WINCE的OBEX协议例子
💻 CXX
📖 第 1 页 / 共 4 页
字号:
{
    HRESULT hr = E_FAIL;
    
    //check to see what(if any) dll we need to pass the file to
    if(NULL != pConn && NULL != pOT && NULL != pConn->szName) 
    {              
        //find the last period
        WCHAR *lpPrev = wcsrchr (pConn->szName, '.');
        
        if (!lpPrev) 
            lpPrev = pConn->szFileName;		
        else
            lpPrev ++;
        
        WCHAR szDLLName[MAX_PATH];
        DWORD dwDLLSize = MAX_PATH;
        BOOL fMoveSucceeded = FALSE;

        if(ERROR_SUCCESS ==  RetrieveServiceDLL(0, lpPrev, szDLLName, &dwDLLSize))
        {           
            HINSTANCE hDll = LoadLibrary (szDLLName);
            if (NULL != hDll) {       
           	    //get a pointer to the DLL's function
                OBEXClientPFunct clientPFunct = (OBEXClientPFunct)GetProcAddress (hDll, _T("OBEXInboxClient"));
           	              	   
                //rename the file to be the object name (so we dont
                //  give a weird name to the DLL   
                fMoveSucceeded = MoveFile(pConn->szFileName, pConn->szName);
                
                if(!fMoveSucceeded &&  ERROR_ALREADY_EXISTS == GetLastError()) {
                    //delete the orig file so that MoveFile will succeed
                   	//  NOTE: this is safe because the user was prompted before we got here
                   	//    (or they set OBEX_PROMPT_OVERWRITE in registry)
                    DeleteFile(pConn->szName);
                    fMoveSucceeded = MoveFile(pConn->szFileName, pConn->szName);
                } 
				
				if(!fMoveSucceeded) {
                    WCHAR Msg[MAX_PATH + 200];
                    swprintf(Msg,L"Transfer failed:  your object remains as %s", pConn->szFileName);
                    MessageBox(NULL, Msg, L"Error", MB_OK | MB_SETFOREGROUND);
                }

                //do some memory cleaup
		        if(pConn->szFileName)		   
		              delete [] pConn->szFileName;		  
		        pConn->szFileName = NULL;
                
                if (clientPFunct && fMoveSucceeded) {
                    DEBUGMSG(INBOX_PUT_ZONE,(L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : Calling into service DLL %s\n", pOT->uiConnectionId, szDLLName));
                    //pass off the filename
                    __try {
                        clientPFunct(pConn->szName);
                        hr = S_OK;
                    } __except (1) {
                        DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : EXCEPTION IN SERVICE CALL\n", pOT->uiConnectionId));
                    }
                } else {
                    DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : service DLL %s does not have entry point\n", pOT->uiConnectionId, szDLLName));
                }
                //free the lib
                FreeLibrary(hDll);
            } else
            DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : failed load on service DLL %s\n", pOT->uiConnectionId, szDLLName));
        } 
        else {
            DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : no handlers, just leaving the file as is (%s)\n", pOT->uiConnectionId, pConn->szName));
            
            //if there is a file here delete it
            if(NULL != pConn->szName[0]) {
                fMoveSucceeded = MoveFile(pConn->szFileName, pConn->szName);
                
                if(!fMoveSucceeded &&  ERROR_ALREADY_EXISTS == GetLastError()) {
                    //delete the orig file so that MoveFile will succeed
                   	//  NOTE: this is safe because the user was prompted before we got here
                   	//    (or they set OBEX_PROMPT_OVERWRITE in registry)
                    DeleteFile(pConn->szName);
                    fMoveSucceeded = MoveFile(pConn->szFileName, pConn->szName);
                } 
				
				if(!fMoveSucceeded) {
                    WCHAR Msg[MAX_PATH + 200];
                    swprintf(Msg,L"Transfer failed:  your object remains as %s", pConn->szFileName);
                    MessageBox(NULL, Msg, L"Error", MB_OK | MB_SETFOREGROUND);
                }
            } else {
                DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : invalid file name (NULL) -- deleting temp file (%s)\n", pOT->uiConnectionId, pConn->szFileName));
                DeleteFile(pConn->szFileName);
				fMoveSucceeded = TRUE; //to avoid the ASSERT
            }

            //do some memory cleaup
	        if(pConn->szFileName)		   
		       delete [] pConn->szFileName;	
    	    pConn->szFileName = NULL;	      
            hr = S_OK;
        }

        ASSERT(fMoveSucceeded);
    }	
	return hr;
}



/******************************************************************************/
/*   ServiceRequest                                                           */
/*   Handle a incoming packet                                                 */
/*                                                                            */
/*     Types of packets handled:                                              */
/*        OBEX_OP_CONNECT:                                                    */
/*            init data, put the connection onto the connectoin linked list   */
/*            and then respond with an OK packet                              */
/*                                                                            */
/*        OBEX_OP_DISCONNECT:                                                 */
/*            remove the connection from the linked list, call CloseConnection*/
/*            and send off a disconnet packet                                 */
/*                                                                            */
/*        OBEX_OP_PUT:                                                        */
/*            look into the packet for a OBEX_HID_NAME.  when found create    */
/*            a file with that name.  all OBEX_HID_BODY info goes into that   */ 
/*            file                                                            */
/*                                                                            */
/******************************************************************************/
int ServiceRequest (ObexTransaction *pOT) {
    
    //if this is a connect response, simply reply back saying that I'm here
    if (pOT->pObex->uiOp == (OBEX_OP_CONNECT & OBEX_OP_OPMASK)) {
        DEBUGMSG(INBOX_CONNECT_ZONE, (L"[OBEX-INBOX] ServiceRequest : CONNECT 0x%08x max packet %d\n", pOT->uiConnectionId, pOT->pObex->sPktData.ConnectRequestResponse.maxlen));
        
        //create a new object to hold this connections state
        InboxState *pNew = new InboxState;
        if (! pNew) {
            DEBUGMSG(INBOX_CONNECT_ZONE, (L"[OBEX-INBOX] ServiceRequest : CONNECT 0x%08x OUT OF MEMORY\n", pOT->uiConnectionId, pOT->pObex->sPktData.ConnectRequestResponse.maxlen));
            return FALSE;
        }
     
        //initilize all important fields
        pNew->uiConnectionId = pOT->uiConnectionId;
        pNew->uiPacketLimit =
            pOT->pObex->sPktData.ConnectRequestResponse.maxlen;
        if(pNew->uiPacketLimit < 255)
            pNew->uiPacketLimit = 255; 
        pNew->pNext = g_pConn;
        g_pConn = pNew;
        
        ObexCommand oc;
        unsigned int aPropID[4];
        ObexVariant  aPropVar[4];

        //zero out the command
        memset (&oc, 0, sizeof(oc));
        
        //fill in the command info
        oc.fFinal = TRUE;
        oc.uiOp = pOT->pObex->uiOp;
        oc.uiResp = OBEX_STAT_OK;
        oc.sPktData.ConnectRequestResponse.version = 0x10;
        oc.sPktData.ConnectRequestResponse.maxlen = OBEX_INBOX_MAXPACKET;
        oc.cProp = 0;        
        oc.aPropID = aPropID;
        oc.aPropVar = aPropVar;
        
        //put in a connection ID
        aPropID[oc.cProp] = OBEX_HID_CONNECTIONID;
        aPropVar[oc.cProp].ui = pOT->uiConnectionId;
        ++oc.cProp;

        //send out the packet
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_RESPOND, pOT->uiTransactionId, &oc);
        EnterCriticalSection (&g_cs);
        
        return TRUE;
    } else if(pOT->pObex->uiOp == (OBEX_OP_DISCONNECT & OBEX_OP_OPMASK)) {
        DEBUGMSG(INBOX_DISCONNECT_ZONE, (L"[OBEX-INBOX] ServiceRequest : DISCONNECT 0x%08x\n", pOT->uiConnectionId));
        
        InboxState *pParent = NULL;
        InboxState *pConn = g_pConn;
        
        //search for the connection ID (the stored state)
        while (pConn) {
            if (pConn->uiConnectionId == pOT->uiConnectionId)
                break;            
            pParent = pConn;
            pConn = pConn->pNext;
        }
                    
        if(pConn) {
            //remove this connectionID from the ongoing list
            if (! pParent)
                g_pConn = g_pConn->pNext;
            else
                pParent->pNext = pConn->pNext;
            
            //cleanup state information
            CloseConnection(pOT, pConn);
        } else {
            DEBUGMSG(INBOX_DISCONNECT_ZONE, (L"[OBEX-INBOX] ServiceRequest : DISCONNECT 0x%08x : connection not found\n", pOT->uiConnectionId));
        }
        //send disconnect message
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_OK, pOT->uiTransactionId, NULL);
        pOT->ObexExecute (OBEX_RESP_DISCONNECT, pOT->uiConnectionId, NULL);
        EnterCriticalSection (&g_cs);
        
        return TRUE;
    } else if(pOT->pObex->uiOp == OBEX_OP_PUT) 
    {
        DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x\n", pOT->uiConnectionId));
        
        //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 *pParent = NULL;
        InboxState *pConn = g_pConn;
        
        //search for the connection ID (the stored state)
        while (pConn) {
            if (pConn->uiConnectionId == pOT->uiConnectionId)
                break;            
            pParent = pConn;
            pConn = pConn->pNext;
        }    
        
        //
        //  if pre-stored state couldnt be found the person is probabally 
        //    giving us data without connecting first... this is allowed in 
        //    OBEX so get them some state! :)
        if(!pConn) 
        {
            //create a new object to hold this connections state
            InboxState *pNew = new InboxState;
            if (! pNew) {
                DEBUGMSG(INBOX_PUT_ZONE,(L"[OBEX-INBOX] ServiceRequest : GET making a CONNECT 0x%08x OUT OF MEMORY\n", pOT->uiConnectionId, pOT->pObex->sPktData.ConnectRequestResponse.maxlen));
                return FALSE;
            }
            
            //initilize all important fields
            pNew->uiConnectionId = pOT->uiConnectionId;
            pNew->uiPacketLimit =
                pOT->pObex->sPktData.ConnectRequestResponse.maxlen;
            if(pNew->uiPacketLimit < 255)
                pNew->uiPacketLimit = 255;            
                 
            pNew->pNext = g_pConn;
            pNew->uiMode = PUT_MODE;
            g_pConn = pNew;
            
            DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : connection not found -- making a new one\n", pOT->uiConnectionId));
            pConn = pNew;        
        }
        
        
        //make sure we are in the right mode (NO_MODE or PUT_MODE)
        ASSERT(pConn && pConn->uiMode == NO_MODE || pConn->uiMode == PUT_MODE);  
        if(pConn->uiMode != NO_MODE && pConn->uiMode != PUT_MODE)
        {
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs); 
            return TRUE;
        }
        
        
        unsigned int aPropID[3];
        ObexVariant  aPropVar[3];
        ObexCommand oc;    
        
        //zero out the command
        memset (&oc, 0, sizeof(oc));
        
        //fill in the command info
        oc.fFinal = TRUE;
        oc.uiOp = pOT->pObex->uiOp;
        oc.cProp = 0;
        oc.aPropID = aPropID;
        oc.aPropVar = aPropVar;
        oc.uiResp = 0x10; //OBEX_STAT_CONTINUE;
        
        //search out all properties in the packet
        for (int i = 0 ; i < (int)pOT->pObex->cProp ; ++i) {
            //if this is the name of the packet,
            if (pOT->pObex->aPropID[i] == OBEX_HID_NAME && pConn->hFile == INVALID_HANDLE_VALUE) {
                //first check to see if there is any invalid in the filename... if so
                //  bail out
                if(!PathIsValidFileName (pOT->pObex->aPropVar[i].pwsz))
                {
                    DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : INVALID file name!\n"));
                    ResetConnection(pConn);  
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs); 
                    return TRUE;                    
                }
                
                if (GetFileAttributes(szInboxDir) == 0xffffffff)
                    CreateDirectory (szInboxDir, NULL);
                
                //write down the name of the object (for possible reference later)
                UINT uiNameSize = wcslen (pOT->pObex->aPropVar[i].pwsz); 
                UINT uiDirLen = wcslen (szInboxDir);
                
                ASSERT(!pConn->szName); //if this is set, we leak
                pConn->szName = new WCHAR[uiDirLen + uiNameSize + 2];
                if(!pConn->szName)
                {
                    ASSERT(FALSE);       
                    DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- Error, couldnt allocate mem\n"));
                    return FALSE;
                }
                
                if(NULL != pOT->pObex->aPropVar[i].pwsz && NULL != *(pOT->pObex->aPropVar[i].pwsz)) {
                    memcpy(pConn->szName, szInboxDir, uiDirLen * sizeof(WCHAR));
                    pConn->szName[uiDirLen] = '\\';
                    memcpy(pConn->szName+uiDirLen+1, pOT->pObex->aPropVar[i].pwsz, uiNameSize * sizeof(WCHAR));
                    pConn->szName[uiDirLen + uiNameSize + 1] = 0;
                } else {
                    DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- Possible error -- they are giving us a null file name -- will check for mime type later\n"));
                    pConn->szName[0] = NULL;
                }
                
                //generate a new temp filename (to temporarly hold this object) 
                ASSERT(NULL == pConn->szFileName); //if this is set, we leak
                pConn->szFileName = new WCHAR[MAX_PATH];
                if(!pConn->szFileName)
                {
                    ASSERT(FALSE);       
                    DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- Error, couldnt allocate mem\n"));
                    return FALSE;
                }
                
                if(0 == GetTempFileName(szInboxDir, L"OBX", 0, pConn->szFileName))
            	 {
	                ResetConnection(pConn);  
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs); 
                    return TRUE;
            	 }
                
                DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : name %s temp name %s\n", pOT->uiConnectionId, pConn->szName, pConn->szFileName));
                 
                          
                          
                //get prompting keys from registry          

⌨️ 快捷键说明

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