Wednesday, July 7, 2010

Unicode file encryption/decryption

Here is an overview of how we will encrypt/decrypt an unicode file


Encryption:

1. Open the file as binary mode and read the content of file to a byte array.

2. Encrypt the byte array.

3. Write the byte array to a file.

Decryption:

1. Open the encrypted file as binary mode and read content of the file to a byte array.

2. Decrypt the byte array.

3. Write the byte array to a file.


The encryption and decryption functions are:


void EncryptBytes
(
unsigned char* pByte, // Bytes passed in, encrypted bytes passed out.
int iSize, // Size of the byte array
int* pKey // Encrypting key. (in/out)

)

{
int ii = 0;
unsigned char iNew = 0;
for(ii = 0; ii < iSize; ii++)
{
// Encrypt the character.
iNew = pByte[ii] ^ ((*pKey >> 8));
*pKey = ((iNew + *pKey) & 0xFFFF)*EncryptConst1 - EncryptConst2;
pByte[ii] = iNew;
}
}

void DecryptBytes
(
unsigned char* pByte, // Bytes passed in, decrypted bytes passed out.
int iSize, // Size of the byte array
int* pKey // Encrypting key. (in/out)

)

{
int ii = 0;
unsigned char iNew = 0, iVal;

for(ii = 0; ii < iSize; ii++)
{
iVal = pByte[ii];
iNew = iVal ^ ((*pKey >> 8));

*pKey = ((iVal + *pKey) & 0xFFFF)*EncryptConst1 - EncryptConst2;
pByte[ii] = iNew;
}
}

Monday, June 21, 2010

The goldmine for mfc, atl, java

http://mfcgoldmine.uuuq.com/Convert/index.html

Monday, June 14, 2010

Syntax coloring text editor

www.scintilla.org

Thursday, June 10, 2010

Sample Batch file

::
::
::
@setlocal
@echo on
::
set DIRNAME=%~dp0
::
::
echo Creating Shortcut

:: To Show all param just echo %*

echo %DIRNAME%VisualHost_CreateShortcut.bat %*
::

PAUSE
::
::
endlocal


@echo off
echo -----------------------------------
echo Shortcut Name : %1
echo Shortcut Path : %2
echo Icon Path : %3
echo Launcher File : %4
echo ActiveConfig : %5
echo ActiveConfig Name : %6
echo ActiveContext : %7
echo ActiveContext Name: %8
echo -----------------------------------
set /p name= What is your name?

Wednesday, June 9, 2010

Compact the text to fit in a rectangle

void CompactText(CFont* pFont, CRect rcText, CString& strText)
{
CRect rcClient;
CClientDC clientDC(NULL);
CDC memDC;
CBitmap bmp;
CBitmap* pBmpOld;
CFont* pFontOld;

if(pFont == NULL)
return;

memDC.CreateCompatibleDC(&clientDC);
bmp.CreateCompatibleBitmap(&clientDC, rcText.Width(), rcText.Height());

pBmpOld = memDC.SelectObject(&bmp);
pFontOld = memDC.SelectObject(pFont);

UINT nFlag = DT_NOPREFIX DT_MODIFYSTRING DT_END_ELLIPSIS DT_SINGLELINE;
memDC.DrawText(strText, rcText, nFlag);

if(pBmpOld != NULL)
memDC.SelectObject(pBmpOld);

if(pFontOld != NULL)
memDC.SelectObject(pFontOld);
}
Others API releted to it:
PathCompactPath
CDC::GetTextExtent

Tuesday, February 2, 2010

Do something while executing a process

// Create the process
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

BOOL bCreate;

bCreate = CreateProcess(szProcessName, szCmdLine, NULL, NULL, TRUE,
CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,NULL, szProcessPath, &si, &pi);

// Wait for the end of the process
while(WaitForSingleObject(pi.hProcess, 250) == WAIT_TIMEOUT)
{
//Do something here
}

// Terminate the process
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

Tuesday, December 29, 2009

Create window at maximized or minimized state

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{

if( !CMDIFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= WS_MAXIMIZE; // for minimized WS_MINIMIZE
return TRUE;
}