none
Convert C++ To VB.NET RRS feed

  • 问题

  • When I develop the programs for my phone using the OS Windows mobile,I have a problem. The former program was developed with VB.net,but I haven't learned about this language .The code I searched from the Internet was mostly written in C# or C++,which caused a lot of trouble. Here the code I am showing was a class I have to use, and I hope someone can help me to translate it into VB.net.Thank you!

    /***********ConnectManager.h*************/

    #pragma once
    #define NOTE_GPRSCONN  _T("Hello World")
    class CConnectManager
    {
     public:
     CConnectManager(void);
     virtual ~CConnectManager(void);

     void EnumNetIdentifier ( OUT CStringArray &StrAry );

     BOOL GetConnMgrAvailable();
     int MapURLAndGUID ( LPCTSTR lpszURL, GUID &guidNetworkObject, OUT CString *pcsDesc=NULL );
     BOOL EstablishConnection ( DWORD dwIndex );
     BOOL WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus=NULL );

     CString ConnStatusString(DWORD dwStatus );

     BOOL CanSocketConn();

     BOOL ConnectChinaMobile();
     BOOL ConnectChinaUniom();

     BOOL SendImage(LPCTSTR filename,LPCTSTR ip,int port);
     private:
     void ReleaseConnection ();
     HANDLE m_hConnection;
    };

     /********************ConnectManager.cpp*******************/

    #include "stdafx.h"
    #include "ConnectManager.h"

    // for connect manager function
    #include <Connmgr.h>
    #pragma comment ( lib, "Cellcore.lib" )

    CConnectManager::CConnectManager(void): m_hConnection ( NULL )
    {
    }

    CConnectManager::~CConnectManager(void)
    {
     ReleaseConnection ();
    }

    BOOL CConnectManager::GetConnMgrAvailable()
    {
     HANDLE hConnMgr = ConnMgrApiReadyEvent ();
     BOOL bAvailbale = FALSE;
     DWORD dwResult = ::WaitForSingleObject ( hConnMgr, 2000 );
     if ( dwResult == WAIT_OBJECT_0 )
     {
        bAvailbale = TRUE;
     }
     // 关闭
     if ( hConnMgr ) CloseHandle ( hConnMgr );
     return bAvailbale;
    }

    int CConnectManager::MapURLAndGUID ( LPCTSTR lpszURL, OUT GUID &guidNetworkObject, OUT CString *pcsDesc/*=NULL*/ )
    {
     if ( !lpszURL || lstrlen(lpszURL) < 1 )
        return FALSE;

     memset ( &guidNetworkObject, 0, sizeof(GUID) );
     int nIndex = 0;
     HRESULT hResult = ConnMgrMapURL ( lpszURL, &guidNetworkObject, (DWORD*)&nIndex );
     if ( FAILED(hResult) )
     {
        nIndex = -1;
        DWORD dwLastError = GetLastError ();
        AfxMessageBox ( _T("Could not map a request to a network identifier") );
     }
     else
     {
        if ( pcsDesc )
        {
      CONNMGR_DESTINATION_INFO DestInfo = {0};
      if ( SUCCEEDED(ConnMgrEnumDestinations(nIndex, &DestInfo)) )
      {
       *pcsDesc = DestInfo.szDescription;
      }
        }
     }
     return nIndex;
    }

    void CConnectManager::EnumNetIdentifier ( OUT CStringArray &StrAry )
    {
     CONNMGR_DESTINATION_INFO networkDestInfo = {0};

     for ( DWORD dwEnumIndex=0; ; dwEnumIndex++ )
     {
        memset ( &networkDestInfo, 0, sizeof(CONNMGR_DESTINATION_INFO) );
        if ( ConnMgrEnumDestinations ( dwEnumIndex, &networkDestInfo ) == E_FAIL )
        {
      break;
        }
        StrAry.Add ( networkDestInfo.szDescription );
        OutputDebugString(networkDestInfo.szDescription);
     }
    }

    BOOL CConnectManager::EstablishConnection ( DWORD dwIndex )
    {
     ReleaseConnection ();

     CONNMGR_DESTINATION_INFO DestInfo = {0};
     HRESULT hResult = ConnMgrEnumDestinations(dwIndex, &DestInfo);
     BOOL bRet = FALSE;
     if(SUCCEEDED(hResult))
     {
        CONNMGR_CONNECTIONINFO ConnInfo;

        ZeroMemory(&ConnInfo, sizeof(ConnInfo));
        ConnInfo.cbSize = sizeof(ConnInfo);
        ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
        ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
        ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
        ConnInfo.guidDestNet = DestInfo.guid;
        ConnInfo.bExclusive = FALSE;
        ConnInfo.bDisabled = FALSE;

        DWORD dwStatus = 0;
        hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );
        if(FAILED(hResult))
        {
      CString sStatus = ConnStatusString(dwStatus);
      m_hConnection = NULL;
        }
        else bRet = TRUE;
     }

     return bRet;
    }

    BOOL CConnectManager::WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus/*=NULL*/ )
    {
     DWORD dwStartTime = GetTickCount ();
     BOOL bRet = FALSE;
     while ( GetTickCount ()-dwStartTime < (DWORD)nTimeoutSec * 1000 )
     {
        if ( m_hConnection )
        {
      DWORD dwStatus = 0;
      HRESULT hr = ConnMgrConnectionStatus ( m_hConnection, &dwStatus );
      if ( pdwStatus ) *pdwStatus = dwStatus;
      if ( SUCCEEDED(hr) )
      {
       if ( dwStatus == CONNMGR_STATUS_CONNECTED )
       {
        bRet = TRUE;
        break;
       }
      }
        }
        Sleep ( 100 );
     }
     return bRet;
    }

    void CConnectManager::ReleaseConnection ()
    {
     if ( m_hConnection )
     {
        ConnMgrReleaseConnection(m_hConnection, FALSE);
        m_hConnection = NULL;
     }
    }

    CString CConnectManager::ConnStatusString( DWORD dwStatus )
    {
     CString sStatus = _T("");
     switch (dwStatus)
     {
     case CONNMGR_STATUS_CONNECTED:
        sStatus = "CONNECTED";
        break;
     case CONNMGR_STATUS_CONNECTIONCANCELED:
        sStatus = "User aborted connection";
        break;
     case CONNMGR_STATUS_UNKNOWN:
        sStatus = "Unknown status";
        break;
     case CONNMGR_STATUS_SUSPENDED:
        sStatus = "Connection is up but suspended";
        break;
     case CONNMGR_STATUS_DISCONNECTED:
        sStatus = "Connection is disconnected";
        break;
     case CONNMGR_STATUS_CONNECTIONFAILED:
        sStatus = "Connection failed and cannot not be reestablished";
        break;
     case CONNMGR_STATUS_CONNECTIONDISABLED:
        sStatus = "Connection is ready to connect but disabled";
        break;
     case CONNMGR_STATUS_NOPATHTODESTINATION:
        sStatus = "No path could be found to destination";
        break;
     case CONNMGR_STATUS_WAITINGFORPATH:
        sStatus = "Waiting for a path to the destination";
        break;
     case CONNMGR_STATUS_WAITINGFORPHONE:
        sStatus = "Voice call is in progress";
        break;
     case CONNMGR_STATUS_PHONEOFF:
        sStatus = "Phone resource needed and phone is off";
        break;  
     case CONNMGR_STATUS_EXCLUSIVECONFLICT:
        sStatus = "the connection could not be established because it would multi-home an exclusive connection";
        break;  
     case CONNMGR_STATUS_NORESOURCES:
        sStatus = "Failed to allocate resources to make the connection.";
        break;  
     case CONNMGR_STATUS_CONNECTIONLINKFAILED:
        sStatus = "Connection link disconnected prematurely.";
        break;  
     case CONNMGR_STATUS_AUTHENTICATIONFAILED:
        sStatus = "Failed to authenticate user.";
        break;  
     case CONNMGR_STATUS_NOPATHWITHPROPERTY:
        sStatus = "Path with connection having requested property, ex. WAKE_ON_INCOMING, is not available.";
        break;  
     case CONNMGR_STATUS_WAITINGCONNECTION:
        sStatus = "Attempting to connect";
        break;  
     case CONNMGR_STATUS_WAITINGFORRESOURCE:
        sStatus = "Resource is in use by another connection";
        break;  
     case CONNMGR_STATUS_WAITINGFORNETWORK:
        sStatus = "Network is used by higher priority thread or device is roaming.";
        break;  
     case CONNMGR_STATUS_WAITINGDISCONNECTION:
        sStatus = "Connection is being brought down";
        break;  
     case CONNMGR_STATUS_WAITINGCONNECTIONABORT:
        sStatus = "Aborting connection attempt";
        break;
     }
     return sStatus;
    }

    BOOL CConnectManager::CanSocketConn()
    {
     CSocket sock;
     sock.Create ();
     BOOL ret = ( sock.Connect ( L"192.168.0.168", 5000 ) );
     if(ret)sock.Close();
     return ret;
    }

    BOOL CConnectManager::SendImage( LPCTSTR filename,LPCTSTR ip,int port )
    {
       CSocket m_Socket;
     if(m_Socket.Create())
     {

        if(m_Socket.Connect(ip, port))
        {
      char szRecValue[10240];
      ZeroMemory(szRecValue,10240);
      CFile m_File;
      m_File.Open(filename,CFile::typeBinary|CFile::modeRead);
      ULONGLONG iLen = m_File.GetLength();
      char* pBuf = new char[m_File.GetLength()+1];
      ZeroMemory(pBuf,m_File.GetLength()+1);
      m_File.Read(pBuf,m_File.GetLength());
      LPSTR sIn = pBuf;
      LPSTR sOut = new char[2048000];
      ZeroMemory(sOut,2048000);
      int dOutLen = 0;
      CBase64::encode(sIn,m_File.GetLength(),sOut,(LPDWORD)&dOutLen);
      char head[] = "<WW_VIDEO><COMMAND>1000</COMMAND><CONTENT>";
      char tail[] = "</CONTENT></WW_VIDEO>";
      int totallen = strlen(head) + dOutLen + strlen(tail) + 1;
      char * buffer = new char[totallen];
      ZeroMemory(buffer,totallen);
      strcat(buffer,head);
      strcat(buffer,sOut);
      strcat(buffer,tail);
      m_Socket.Send(buffer,totallen);
     //     CString strText;
     //     strText.Format("<WW_VIDEO><COMMAND>1000</COMMAND><CONTENT>%s</CONTENT></WW_VIDEO>",sOut);
     //     m_Socket.Send(strText.GetBuffer(0), strText.GetLength());
      delete [] sOut;
      delete [] buffer;
      m_File.Close();
       
      return TRUE;
        /* Sleep(500);
      int iRev = m_Socket.Receive((void *)szRecValue, 10240);
      AfxMessageBox(szRecValue);*/
        }
     }
     return FALSE;
    }

     

    2011年10月17日 9:42

答案