Click here to Skip to main content
15,910,787 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRegister your program as PC's antivirus Pin
Brandon-X1200010-Feb-12 17:49
Brandon-X1200010-Feb-12 17:49 
AnswerRe: Register your program as PC's antivirus Pin
«_Superman_»12-Feb-12 17:55
professional«_Superman_»12-Feb-12 17:55 
GeneralRe: Register your program as PC's antivirus Pin
Brandon-X1200012-Feb-12 22:30
Brandon-X1200012-Feb-12 22:30 
GeneralRe: Register your program as PC's antivirus Pin
David Crow13-Feb-12 2:47
David Crow13-Feb-12 2:47 
QuestionTranslating the Mod Operator in VB to c++ check Pin
jkirkerx10-Feb-12 10:56
professionaljkirkerx10-Feb-12 10:56 
AnswerRe: Translating the Mod Operator in VB to c++ check Pin
Philippe Mori10-Feb-12 13:03
Philippe Mori10-Feb-12 13:03 
GeneralRe: Translating the Mod Operator in VB to c++ check Pin
jkirkerx10-Feb-12 13:14
professionaljkirkerx10-Feb-12 13:14 
AnswerNeed extra help on this Pin
jkirkerx11-Feb-12 8:56
professionaljkirkerx11-Feb-12 8:56 
I'm trying to translate a program I use in vb to c++, to get the exact same results. The program is basically 8 lines of code. I have the first 2 lines working, but on line 3, I get back a int value of 10, and vb returns &h10, which is 16.

So this is the vb loop of the function. I decided to use char since the valid characters are
VB
Private Const VALID_CHARACTERS = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
Private Const DEFAULT_FORMAT = "&&&&-&&&&-&&&&-&&&&"

This is the source code from vb I use.
VB
For i = 1 To CountAmpersands(sFormat)
L1  strTextChar = Mid(strDomainName, (i Mod Len(strDomainName)) + 1, 1)
L2  strKeyChar = Mid(ApplicationName, (i Mod Len(ApplicationName)) + 1, 1)
L3  intTemp = (((Asc(strKeyChar) * i) * Len(ApplicationName) + 1) Mod Len(ValidCharacters) + 1) 'vb returns &H10 which is 16
L4  strTextChar = Chr(Asc(strTextChar) Xor intTemp)
L5  intTemp = (((Asc(strKeyChar) * i) * Len(DomainName) + 1) Mod Len(ValidCharacters) + 1)
L6  strTextChar = Chr(Asc(strTextChar) Xor intTemp)
L7  intEncryptedChar = ((Asc(strTextChar) Xor Asc(strKeyChar)) Mod Len(ValidCharacters)) + 1
L8  strKey = strKey & Mid(ValidCharacters, intEncryptedChar, 1)

   Select Case i
      Case 4, 8, 12
        strKey = strKey & "-"
      End Select

Next i


And this is my feeble translation to c++, I didn't do very good on it, this is the 3rd run.
C#
for (int i = 1; i <= iCount; ++i) {
L1 szTextCharA = szDomainNameA[ ( i % strlen( szDomainNameA )) ];  // Correct
L2 szKeyCharA = szAppKeyA[ ( i % strlen( szAppKeyA )) ];   // Correct
L3 iTemp = ((szKeyCharA * i) * strlen(szAppKeyA)) % strlen( LICENSEKEY_CHARACTERS );//iTemp 16 or &H10
L4 szTextCharA = (char) ((int)szTextCharA ^ iTemp );   // iTemp should be 16 or &H10
L5 iTemp = (int)(szKeyCharA * i) * strlen(szDomainNameA) % strlen( LICENSEKEY_CHARACTERS );
L6 szTextCharA = (char)((int)szTextCharA ^ iTemp);
L7 iEncryptedChar = (int)szTextCharA ^ (int)szKeyCharA % strlen(LICENSEKEY_CHARACTERS);

   // Build up the License Key till it's done.
   if ( i == 1) {
      strncpy_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
   }
   else {
      strncat_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
   }

}   // End of for loop


Question1:
On Line 3 in VB, I get back &H10, and Line 3 in c++, I get back 10. I'm not sure if I wrote the c++ L3 wrong, or if I'm not clear on the value of iTemp.

Question2:
Am I even close on my translation? From Line 3 to Line 7, and I think I need a better method of building the final key szKeyA.

Question3:
Should I have just lefted it as WCHAR, On version 2, I changed it to char.

This is the entire program so you can get a better idea of what I did

	const char LICENSEKEY_CHARACTERS [] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	const char LICENSEKEY_FORMAT [] = "&&&&-&&&&-&&&&-&&&&";

WCHAR* CA_LicenseKey::_makeKey( WCHAR *pzAppKeyW, WCHAR *pzDomainNameW )
{
	INT iTemp;
	int iEncryptedChar;	
	char szTextCharA;
	char szKeyCharA;
	char *szKeyA = NULL;
	
	if ( wcslen(pzDomainNameW ) == 0) {
		DWORD dwKey = wcslen( L"Invalid Domain Name" );
		WCHAR *szReturnW = new WCHAR[dwKey+1];
		wcsncpy_s(szReturnW, dwKey+1, L"Invalid Domain Name", dwKey );
		return szReturnW;
	}

	int iDomainNameA = (WideCharToMultiByte(CP_UTF8, 0, (pzDomainNameW), -1, NULL, 0, NULL, NULL) - 1) + 1;	
	char *szDomainNameA = new char[iDomainNameA];
	WideCharToMultiByte(CP_UTF8, 0, pzDomainNameW, -1, szDomainNameA, iDomainNameA, NULL, NULL);
	_strlwr_s( szDomainNameA, iDomainNameA );
	
	int iAppKeyA = (WideCharToMultiByte(CP_UTF8, 0, pzAppKeyW, -1, NULL, 0, NULL, NULL) - 1) + 1;	
	char *szAppKeyA = new char[iDomainNameA];
	WideCharToMultiByte(CP_UTF8, 0, pzAppKeyW, -1, szAppKeyA, iAppKeyA, NULL, NULL);
		
	int iCount = _countAmpersands();
	int iKeyA = strlen(LICENSEKEY_FORMAT)+1;
	szKeyA = new char[ iKeyA+1 ];	
		
	for (int i = 1; i <= iCount; ++i) {				
		
		szTextCharA = szDomainNameA[ ( i % strlen( szDomainNameA )) ];	// Correct
		szKeyCharA = szAppKeyA[ ( i % strlen( szAppKeyA )) ];	// Correct
		
		iTemp = ((szKeyCharA * i) * strlen(szAppKeyA)) % strlen( LICENSEKEY_CHARACTERS ); // iTemp should be 16 or &H10	
		szTextCharA = (char) ((int)szTextCharA ^ iTemp );	// iTemp should be 16 or &H10
		
		iTemp = (int)(szKeyCharA * i) * strlen(szDomainNameA) % strlen( LICENSEKEY_CHARACTERS );
		szTextCharA = (char)((int)szTextCharA ^ iTemp);
		iEncryptedChar = (int)szTextCharA ^ (int)szKeyCharA % strlen(LICENSEKEY_CHARACTERS);
		
		// Build up the License Key till it's done.
		if ( i == 1) {
			strncpy_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
		}
		else {
			strncat_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
		}
		
	}	// End of for loop

	delete [] szDomainNameA;
	delete [] szAppKeyA;
	
	WCHAR *szKeyW = NULL;
	return szKeyW;
}

int CA_LicenseKey::_countAmpersands( void )
{
	INT iCount = 0;
	int iLength = 0;
	char *szFormatA = NULL;
	
	iLength = strlen(LICENSEKEY_FORMAT);
	szFormatA = new char[iLength+1];
	strncpy_s( szFormatA, iLength+1, LICENSEKEY_FORMAT, iLength );
	
	for (int i = 0; i <= iLength; ++i) {				
		if ( strncmp( &szFormatA[i], "&", 1) == 0 ) {
			++iCount;
		}		
	}

	delete szFormatA;
	return iCount;
}

AnswerRe: Translating the Mod Operator in VB to c++ check Pin
Dave Doknjas11-Feb-12 14:36
Dave Doknjas11-Feb-12 14:36 
GeneralRe: Translating the Mod Operator in VB to c++ check Pin
jkirkerx12-Feb-12 9:05
professionaljkirkerx12-Feb-12 9:05 
Questioncharacter counting and matching Pin
jkirkerx10-Feb-12 8:52
professionaljkirkerx10-Feb-12 8:52 
AnswerRe: character counting and matching Pin
Richard Andrew x6410-Feb-12 9:21
professionalRichard Andrew x6410-Feb-12 9:21 
GeneralRe: character counting and matching Pin
jkirkerx10-Feb-12 9:42
professionaljkirkerx10-Feb-12 9:42 
AnswerRe: character counting and matching Pin
Jochen Arndt10-Feb-12 22:33
professionalJochen Arndt10-Feb-12 22:33 
GeneralRe: character counting and matching Pin
jkirkerx11-Feb-12 7:41
professionaljkirkerx11-Feb-12 7:41 
AnswerRe: character counting and matching Pin
luckyxjb201018-Feb-12 19:05
luckyxjb201018-Feb-12 19:05 
QuestionAn application between bios and windows! Pin
candogu10-Feb-12 0:54
candogu10-Feb-12 0:54 
AnswerRe: An application between bios and windows! Pin
Richard MacCutchan10-Feb-12 0:59
mveRichard MacCutchan10-Feb-12 0:59 
GeneralRe: An application between bios and windows! Pin
candogu10-Feb-12 1:00
candogu10-Feb-12 1:00 
GeneralRe: An application between bios and windows! Pin
Richard MacCutchan10-Feb-12 1:23
mveRichard MacCutchan10-Feb-12 1:23 
QuestionShellExecute problem Pin
_Flaviu9-Feb-12 23:39
_Flaviu9-Feb-12 23:39 
AnswerRe: ShellExecute problem Pin
Rajesh R Subramanian10-Feb-12 0:02
professionalRajesh R Subramanian10-Feb-12 0:02 
AnswerRe: ShellExecute problem Pin
David Crow10-Feb-12 3:12
David Crow10-Feb-12 3:12 
QuestionBCGPTabWnd OnSize Problem Pin
Jan_fuman9-Feb-12 23:30
Jan_fuman9-Feb-12 23:30 
QuestionEnumDisplaySettings() always returns false Pin
Kiran Satish9-Feb-12 18:45
Kiran Satish9-Feb-12 18:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.