Search This Blog

Friday, May 15, 2009

UnMashalling WebBroser Pointer

          DWORD dwBrowserPtr = NULL;
          dwBrowserPtr =dwStream //input
         
          ::CoInitialize(NULL);
          IStream** pStream = NULL ;
          pStream = reinterpret_cast<IStream**>(dwBrowserPtr);
         
         
          if( pStream == NULL )
          {
               ::CoUninitialize();
               return -1;
          }
         
          CComPtr<IDispatch> spBrowserPointer ;
          IWebBrowserQIPtr pBrowserPtr;
         
          HRESULT hr = CoGetInterfaceAndReleaseStream(*pStream,IID_IWebBrowser2,(LPVOID*)&pBrowserPtr);
          if ( hr == S_OK )
          {
               //success
          }
          else
          {
               //Failed To get browser pointer
               ASSERT(0);
              
          }

Marshalling WebBrowser Pointer

     /* ============================================================================
     #include <mshtml.h> */


     typedef CComQIPtr<IWebBrowser2,&IID_IWebBrowser2>                              IWebBrowserQIPtr;

     IStream** pStream = NULL ;
     pStream = new IStream* ; // free this memory only at the end of parsing.If you are freeing before that parse will fail

     CComPtr<IDispatch> spBrowserPointer ;         
     spBrowserPointer = APPPTR->m_pdlg->m_webBwsrCntrl.get_Application();

     IWebBrowserQIPtr pBrowserPtr;
     pBrowserPtr = spBrowserPointer;

     HRESULT hr = ::CoMarshalInterThreadInterfaceInStream(IID_IWebBrowser2,pBrowserPtr,pStream);    

     DWORD dwStream = 0;

     if( hr == S_OK )
     {
          dwStream = reinterpret_cast<DWORD>( pStream );
     }
     else
     {
          return -1;
     }

     //(*pStream)->Release(); // Do not release this.While calling CoGetInterfaceAndReleaseStream it will be automaticaly released.

     //=============================================================================

UIThreadImplementation

Create a new class and inherit CWinThread
Add dialog member object in the Thread class


#pragma once
#include "MyDialog.h"
// CMyNewThread

class CMyNewThread : public CWinThread
{
     DECLARE_DYNCREATE(CMyNewThread)

protected:
     CMyNewThread(); // protected constructor used by dynamic creation
     virtual ~CMyNewThread();

public:
     virtual BOOL InitInstance();
     virtual int ExitInstance();

     CMyDialog *m_dlg;

protected:
     DECLARE_MESSAGE_MAP()
};


BOOL CMyNewThread::InitInstance()
{    
     m_dlg = new CMyDialog();
     m_dlg->Create (IDD_DIALOG_MY, NULL);
     m_dlg->ShowWindow( TRUE );
     m_dlg->UpdateWindow();
     return TRUE;

     //return TRUE;
}

// calling the Thread
void CMainFrame::OnNewCreatenewwindow()
{
// TODO: Add your command handler code here
CWinThread *pThread = AfxBeginThread( RUNTIME_CLASS( CMyNewThread ) );
}

Thursday, May 14, 2009

WorkerThread Implementation



class CWorkerThreadImplementationDlg : public CDialog
{
     typedef struct DLG_STRUCT
     {
          CWorkerThreadImplementationDlg* _this;
     }DLG_STRUCT , *PDLGSTRUCT;


void CWorkerThreadImplementationDlg::OnBnClickedOk()
{    
     //CString *cstrValue = new CString("Hai");
     //AfxBeginThread( CWorkerThreadImplementationDlg::RunProgress , (LPVOID)cstrValue );
    
     PDLGSTRUCT lpDLG1 = new DLG_STRUCT;
     lpDLG1->_this = this;
     AfxBeginThread( CWorkerThreadImplementationDlg::RunProgress1 , lpDLG1 );
     PDLGSTRUCT lpDLG2 = new DLG_STRUCT;
     lpDLG2->_this = this;
     AfxBeginThread( CWorkerThreadImplementationDlg::RunProgress2 , lpDLG2 );
}

UINT CWorkerThreadImplementationDlg::RunProgress( LPVOID lpThreadParam )
{
     CString pstr = *((CString*)lpThreadParam);
     return 1;
}

UINT CWorkerThreadImplementationDlg::RunProgress1( LPVOID lpThreadParam )
{
     PDLGSTRUCT pstr = (PDLGSTRUCT)lpThreadParam;
     int nPos = 0;
     while( nPos < 100 )
     {
          pstr->_this->m_Progress1.SetPos( nPos++ );
          Sleep( 100 );
     }
     return 1;
}
UINT CWorkerThreadImplementationDlg::RunProgress2( LPVOID lpThreadParam )
{
     PDLGSTRUCT pstr = (PDLGSTRUCT)lpThreadParam;
     int nPos = 0;
     while( nPos < 100 )
     {
          pstr->_this->m_Progress2.SetPos( nPos++ );
          Sleep( 100 );    
     }
     return 1;
}

Thursday, April 16, 2009

Read and write .ini files



void ReadwriteINI()
{
     CString csIniPath,csKey;
     int nPos;

     char lpszKeyNames[4096];
     DWORD dSize;
     dSize = sizeof(lpszKeyNames);
     csIniPath="settings.ini";
     GetPrivateProfileSection( "IPADDRESS" , lpszKeyNames , dSize , csIniPath );
     char *pKeyName = lpszKeyNames;    
     CString csIP(""), csMyKey(""),csSubnet,csProxyIP,csGateway ,csPort;
    
     while (*pKeyName != 0x00)
     {
          nPos = -1;
          if(*pKeyName != 0x00)
          {
               csKey = pKeyName;
               if((nPos = csKey.Find("=")) > -1)
               {    
                    csMyKey = csKey.Left(nPos);
                    if( csMyKey.Find("IPADDR") > - 1 )
                    {
                         csIP= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("SUBNET") > - 1 )
                    {
                         csSubnet= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("GATEWAY") > - 1 )
                    {
                         csGateway= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("PROXYIP") > - 1 )
                    {
                         csProxyIP= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("PORT") > - 1 )
                    {
                         csPort= csKey.Mid(nPos+1);
                    }
               }
          }
          pKeyName = pKeyName + strlen(pKeyName) + 1;
     }


     if(csIP.IsEmpty())
     {
          csIP="192.168.0.2;
          WritePrivateProfileString("IPADDRESS","IPADDR",csIP,csIniPath);
     }
     if(csSubnet.IsEmpty())
     {
          csSubnet="255.255.255.0";
          WritePrivateProfileString("IPADDRESS","SUBNET ",csSubnet,csIniPath);
     }
     if(csGateway.IsEmpty())
     {
          csGateway="192.168.0.3";
          WritePrivateProfileString("IPADDRESS","GATEWAY",csGateway,csIniPath);
     }
     if(csProxyIP.IsEmpty())
     {
          csProxyIP="198.168.0.1";
          WritePrivateProfileString("IPADDRESS","PROXYIP",csProxyIP,csIniPath);
     }
     if(csPort.IsEmpty())
     {
          csPort="3128";
          WritePrivateProfileString("IPADDRESS","PORT",csPort,csIniPath);
     }

    

}

INI file content:
[IPADDRESS] IPADDR= SUBNET= GATEWAY= PROXYIP= PORT= 


Registry Write example by enable Proxy settings

void EnableProxy()
{
     HKEY hkResult;
     LPCTSTR lpValueName = "VALUE_NAME";
     LPCTSTR lpszKeyName = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
     LPCTSTR lpszValueName = "ProxyEnable";
     // Opens a key in the registry and keeps an handle - hkResult - for future use.
     LONG Res = RegCreateKeyEx (HKEY_CURRENT_USER, lpszKeyName,
          0,NULL,
          REG_OPTION_NON_VOLATILE ,KEY_ALL_ACCESS,NULL, &hkResult,
          NULL);
     if (Res != ERROR_SUCCESS)
          throw "Unable to open the key";

     // Write value in the registry
     DWORD dwData;
     dwData=1;
     Res=RegSetValueEx(hkResult, TEXT(lpszValueName),0, REG_DWORD,(const BYTE*) &dwData, sizeof(DWORD));
     if (Res != ERROR_SUCCESS)
          throw "Unable to set value";


     Res = RegCloseKey(hkResult);    

}

Registry Read example to get Proxy IP and Port settings

void GetProxyIPPortRegistry()
{
     HKEY hkResult;
     LPCTSTR lpValueName = "VALUE_NAME";
     LPCTSTR lpszKeyName = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
     LPCTSTR lpszValueName = "ProxyServer";
     int iValue;

     // Opens a key in the registry and keeps an handle - hkResult - for future use.
     LONG Res = RegCreateKeyEx (HKEY_CURRENT_USER, lpszKeyName,
          0,NULL,
          REG_OPTION_NON_VOLATILE ,KEY_ALL_ACCESS,NULL, &hkResult,
          NULL);
     if (Res != ERROR_SUCCESS)
          throw "Unable to open the key";

    
     CString csIP,csIPAddress,csPort;
     // Reads a value from the registry.
     unsigned long lSize=1;
     DWORD dwType;
     char *lpValue;
     if ((Res = RegQueryValueEx(hkResult,lpszValueName,NULL,NULL,
          NULL,&lSize)) == ERROR_SUCCESS)
     {
          lpValue = new char [lSize + 1];
          Res =
               RegQueryValueEx(hkResult, (LPTSTR)lpszValueName,NULL, &dwType,
               (LPBYTE)lpValue, &lSize);

          if (Res != ERROR_SUCCESS)
               throw "Unable to query value";
          iValue = ::atoi(lpValue);
          csIP=lpValue;
     }
     Res = RegCloseKey(hkResult);

     int nPos=-1;
     if( (nPos=csIP.Find(":"))>-1)
     {
          csIPAddress=csIP.Mid(nPos+1);
          csPort=csIP.Left(nPos);

     }

     delete lpValue;


}