|
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
Private Const VALID_CHARACTERS = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
Private Const DEFAULT_FORMAT = "&&&&-&&&&-&&&&-&&&&"
This is the source code from vb I use.
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)
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.
for (int i = 1; i <= iCount; ++i) {
L1 szTextCharA = szDomainNameA[ ( i % strlen( szDomainNameA )) ];
L2 szKeyCharA = szAppKeyA[ ( i % strlen( szAppKeyA )) ];
L3 iTemp = ((szKeyCharA * i) * strlen(szAppKeyA)) % strlen( LICENSEKEY_CHARACTERS );
L4 szTextCharA = (char) ((int)szTextCharA ^ iTemp );
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);
if ( i == 1) {
strncpy_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
}
else {
strncat_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
}
}
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 )) ];
szKeyCharA = szAppKeyA[ ( i % strlen( szAppKeyA )) ];
iTemp = ((szKeyCharA * i) * strlen(szAppKeyA)) % strlen( LICENSEKEY_CHARACTERS );
szTextCharA = (char) ((int)szTextCharA ^ iTemp );
iTemp = (int)(szKeyCharA * i) * strlen(szDomainNameA) % strlen( LICENSEKEY_CHARACTERS );
szTextCharA = (char)((int)szTextCharA ^ iTemp);
iEncryptedChar = (int)szTextCharA ^ (int)szKeyCharA % strlen(LICENSEKEY_CHARACTERS);
if ( i == 1) {
strncpy_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
}
else {
strncat_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
}
}
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;
}
|
|
|
|
|
The second one will be something like:
strTextChar = static_cast<wchar_t>(static_cast<int>(strTextChar[0]) ^ intTemp);
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
|
|
|
|
|
Thanks Dave,
Your hint gave me insight into the proper and more consistent way to write the 8 lines of code.
Now I'm getting accurate results so far going through the loop. Looks like I will be able to complete this today and move on.
|
|
|
|
|
I'm missing the boat here on something. I have this constant, and I just want to count how many ampersands are in it. But I keep getting a character or type mismatch, I can't go though each character to check the value.
Overall, it's my idea of how to replace the mid function used in vb. mid(szFormat, 1,1)
const WCHAR LICENSEKEY_FORMAT [] = L"&&&&-&&&&-&&&&-&&&&";
for (int i = 0; i <= dwLength; ++i) {
if (wcsncmp(szFormat [i], L"&", 1) ==0) {
++dwCount;
}
}
The error I keep getting is
error C2664: 'wcsncmp' : cannot convert parameter 1 from 'WCHAR' to 'const wchar_t *'
If I szFormat[i] == L"&", I sort of get the same error.
Question:
Is it the const that I made is not stated correctly?, If not, what correction do I need?
|
|
|
|
|
try:
wcsncmp( &szFormat[i], L"&", 1 ) == 0
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
That did the trick,
Thanks Richard
|
|
|
|
|
Use this (note the single quotation marks):
if (szFormat [i] == L'&')
You may also have a look on the CString class which provides the Left() , Mid() , and Right() functions.
|
|
|
|
|
I changed it to char, so the single quotes don't work right now. I might change it back to WCHAR today, because I'm not getting the desired results. I do get an accurate count, but I'm trying to translate a vb function, so it needs to be the same.
I'm going to post an extension to the post above in about 10 minutes.
|
|
|
|
|
int wcsncmp( const wchar_t *string1, const wchar_t *string2, size_t count );
szFormat[i] is not a addr
for (int i = 0; i <= dwLength; ++i) {
if (wcsncmp(&szFormat [i], L"&", 1) ==0) {
++dwCount;
}
}
|
|
|
|
|
Hi everyone, I want to develop an application or whatever (driver), this will run before windows starts, and will ask for a password and if the password is correct system will continue to the windows. What can I do? Thanks.
|
|
|
|
|
The simplest thing would be to add a BIOS password[^].
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
I know that... But I want to write it myself.
|
|
|
|
|
Take a look at GINA[^], I think that is what you need to work with.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
I want to launch a web address with ShellExecute in hidden way :
CString sLink = _T("http://www.codeproject.com/somepage/");
ShellExecute(NULL, NULL, sLink, NULL, NULL, SW_HIDE);
and goes well, but also, I want to close the launched application after execution ... how could I ?
|
|
|
|
|
If you want control over the spawned process, use CreateProcess()[^] instead.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Try using ShellExecuteEx() instead. It gives you back information that you can subsequently send to TerminateProcess() .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Hello,
I have trouble with an OnSize handler.
In the OnSize Handler of a view I call a "Tab->MoveWindow(...)"
But in this Tab-Control (of kind CBCGPTabWnd) I never step into the OnSize Handler when I'm sizing the framewindow.
I tested it with breakpoints and OutputDebugStrings.
I even set a breakpoint into the WindowProc function to test if a WM_SIZE message occurs, but it never happens.
If I reproduce this strange effect on 2 computers, it works all fine on the first, but the second one has this effect.
It's the same Exe-File, both computers are using Windows 7 64bit (with Visual Studio 2010)
Does anyone knows a solution??
Thanks ahead
Best wishes, Jan
modified 10-Feb-12 6:56am.
|
|
|
|
|
I am trying to get the resolution of all the monitors that are on connected to the system. Here is how I am trying to do
1) Read number of monitors in the system
2) For each monitor get the device info using EnumDisplayDevices().
3) In step2, I am getting all the info including DeviceName
4) Using the DeviceName from above I am calling EnumDisplaySettings(), but this function is always returning false except when I use NULL for the first parameter where it gives me details of primary monitor.
Any ideas why its returning false when using the DeviceName from step2.
thanks
PKNT
|
|
|
|
|
Quoting from MSDN:
Graphics mode indexes start at zero. To obtain information for all of a display device's graphics modes, make a series of calls to EnumDisplaySettings, as follows: Set iModeNum to zero for the first call, and increment iModeNum by one for each subsequent call. Continue calling the function until the return value is zero.
When you call EnumDisplaySettings with iModeNum set to zero, the operating system initializes and caches information about the display device. When you call EnumDisplaySettings with iModeNum set to a nonzero value, the function returns the information that was cached the last time the function was called with iModeNum set to zero.
Check if it helps
You talk about Being HUMAN. I have it in my name
AnsHUMAN
|
|
|
|
|
Thanks for the info. But I already tried this with the same result.
result = EnumDisplaySettings(lpdParams.DeviceName,0,&lpDMode);
where lpdParams is DISPLAY_DEVICE variable that has been populated with data returned from
EnumDisplayDevices(NULL, 1, &lpdParams, EDD_GET_DEVICE_INTERFACE_NAME);
Not sure where I am doing wrong.
thanks
PKNT
|
|
|
|
|
Do you have some sort of loop set up for this? Something like:
int result = -1;
for (int x = 0; result != 0; x++)
result = EnumDisplaySettings(lpdParams.DeviceName, x, &lpDMode);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
I have no loops for this functions. This function is called the same way I noted earlier after EnumDisplayDevices() only once.
thanks
PKNT
|
|
|
|
|
Kiran Satish wrote: I have no loops for this functions.
My bad. I just assumed when _AnsHUMAN_ suggested the same thing, that your "But I already tried this..." reply meant you were using a loop of some sort.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
I found out that this problem is only on Windows 7, the function ran fine on XP. Any ideas about this issue on win7??
thanks
PKNT
|
|
|
|
|
Kiran Satish wrote: Any ideas about this issue on win7??
UAC, perhaps?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|