home *** CD-ROM | disk | FTP | other *** search
- // Xceed Encryption Library - Memory Encrypt sample
- // Copyright (c) 2001 Xceed Software Inc.
- //
- // [MemoryEncrypt.cpp]
- //
- // This file contains the application core, and some utility functions
- // like HexToBinary and BinaryToHex.
- //
- // This file is part of the Xceed Encryption Library sample applications.
- // The source code in this file is only intended as a supplement to Xceed
- // Encryption Library's documentation, and is provided "as is", without
- // warranty of any kind, either expressed or implied.
-
-
- #include "stdafx.h"
- #include "MemoryEncrypt.h"
- #include "MemoryEncryptDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
- //------------------------------------------------------------------------------------
- // Convert a hexadecimal string to a byte array returned in a variant.
- //------------------------------------------------------------------------------------
- COleVariant HexToBinary( LPCSTR sHexValue )
- {
- COleVariant vaNewValue;
- long I = 0;
-
- long lHexLen = lstrlen( sHexValue );
-
- if( lHexLen > 0 )
- {
- BYTE* pcArray = new BYTE[ lHexLen / 2 + 1 ];
- long lNbElem = 0;
-
- while( I < lstrlen( sHexValue ) )
- {
- int nTemp;
-
- sscanf( sHexValue + I, "%2x", &nTemp );
- pcArray[ lNbElem ] = nTemp;
- lNbElem++;
- I += 2;
- }
-
- SAFEARRAYBOUND saBound[1];
-
- saBound->lLbound = 0;
- saBound->cElements = lNbElem;
- vaNewValue.vt = ( VT_ARRAY | VT_UI1 );
- vaNewValue.parray = SafeArrayCreate( VT_UI1, 1, saBound );
- CopyMemory( vaNewValue.parray->pvData, pcArray, lNbElem );
- delete [] pcArray;
- }
-
- return vaNewValue;
- }
-
- //------------------------------------------------------------------------------------
- // Convert a byte array in a variant to an hexadecimal string.
- //------------------------------------------------------------------------------------
- CString BinaryToHex( VARIANT* vaBinaryValue )
- {
- CString sNewValue;
-
- if( vaBinaryValue->vt = ( VT_ARRAY | VT_UI1 ) )
- {
- long lHigh;
- long lLow;
-
- SafeArrayGetUBound( vaBinaryValue->parray, 1, &lHigh );
- SafeArrayGetLBound( vaBinaryValue->parray, 1, &lLow );
-
- // This pointer will contain the address of the byte array
- char* pcByteArray = NULL;
-
- // We get a pointer on the actual data in the safe array.
- SafeArrayAccessData( vaBinaryValue->parray, ( void** )&pcByteArray );
-
- for( long I = 0; I < lHigh - lLow + 1; I++ )
- {
- CString szTemp;
-
- szTemp.Format( "%02X", ( (BYTE*)vaBinaryValue->parray->pvData)[I] );
- sNewValue += szTemp;
- }
-
- // We release the lock on the safe array
- SafeArrayUnaccessData( vaBinaryValue->parray );
- }
-
- return sNewValue;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CMemoryEncryptApp
-
- BEGIN_MESSAGE_MAP(CMemoryEncryptApp, CWinApp)
- //{{AFX_MSG_MAP(CMemoryEncryptApp)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMemoryEncryptApp construction
-
- CMemoryEncryptApp::CMemoryEncryptApp()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CMemoryEncryptApp object
-
- CMemoryEncryptApp theApp;
-
- /////////////////////////////////////////////////////////////////////////////
- // CMemoryEncryptApp initialization
-
- BOOL CMemoryEncryptApp::InitInstance()
- {
- SetRegistryKey( "Xceed" );
-
- // Initialize OLE libraries
- if (!AfxOleInit())
- {
- AfxMessageBox("OLE initialization failed. Make sure that the OLE libraries are the correct version.");
- return FALSE;
- }
-
- AfxEnableControlContainer();
-
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
-
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
-
- CMemoryEncryptDlg dlg;
- m_pMainWnd = &dlg;
- dlg.DoModal();
-
- // Since the dialog has been closed, return FALSE so that we exit the
- // application, rather than start the application's message pump.
- return FALSE;
- }
-