Click here to Skip to main content
15,889,852 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Sanity Check on Registry Value REG_MULTI_SZ - format value Pin
Richard MacCutchan27-Oct-11 21:59
mveRichard MacCutchan27-Oct-11 21:59 
GeneralRe: Sanity Check on Registry Value REG_MULTI_SZ - format value Pin
jkirkerx28-Oct-11 6:41
professionaljkirkerx28-Oct-11 6:41 
GeneralRe: Sanity Check on Registry Value REG_MULTI_SZ - format value Pin
Richard MacCutchan28-Oct-11 6:57
mveRichard MacCutchan28-Oct-11 6:57 
GeneralRe: Sanity Check on Registry Value REG_MULTI_SZ - format value Pin
jkirkerx28-Oct-11 7:17
professionaljkirkerx28-Oct-11 7:17 
AnswerI rewrote the my function for WCHAR Pin
jkirkerx28-Oct-11 8:22
professionaljkirkerx28-Oct-11 8:22 
AnswerRe: I rewrote the my function for WCHAR Pin
Richard MacCutchan28-Oct-11 23:13
mveRichard MacCutchan28-Oct-11 23:13 
GeneralRe: I rewrote the my function for WCHAR Pin
jkirkerx29-Oct-11 7:39
professionaljkirkerx29-Oct-11 7:39 
GeneralRe: I rewrote the my function for WCHAR Pin
jkirkerx29-Oct-11 8:20
professionaljkirkerx29-Oct-11 8:20 
Works like a charm, I had to move the pszString above the switch because the compiler complained. I changed the function by hand, and only copied the ERROR_Success because it was alot to type. So I have to delete somthing in the calling function, not sure what yet.

WCHAR* CA_Registry::_get_SQLServer_InstalledInstances( void )
{
	HKEY keyHandle;
	WCHAR rgValue[1024];
	WCHAR *regPath = L"SOFTWARE\\Microsoft\\Microsoft SQL Server\\";
	WCHAR *regReq = L"InstalledInstances";
	DWORD size1 = sizeof(rgValue);
	DWORD dwType;
	DWORD regStatus;

	int ndx;
	WCHAR* pszString = rgValue;
	WCHAR *szReturnValue = NULL;
	
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath, 0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS) {
		regStatus = RegQueryValueEx( keyHandle, regReq, NULL, &dwType, (LPBYTE)rgValue, &size1);
		RegCloseKey(keyHandle);	

		switch ( regStatus ) {
			case ERROR_SUCCESS:
				// scan the strings, replacing single nulls by semi-colons
				{
					while (*pszString)
					{
						ndx = wcslen(pszString);
						// add string length so we point at trailing null character
						pszString += ndx;
						if (pszString[1] != L'\0')
						*pszString++ = L';';    // add semi-colon if not the last string
					}
				}
            
				ndx = wcslen(rgValue);	// we need this value below
				wprintf(L" SQL Server Instance: %s %i\n", rgValue, ndx);				
            
				// copy to new buffer here
				szReturnValue = new wchar_t[ndx + 1]; // +1 for trailing null character
				wcscpy_s(szReturnValue, ndx + 1, rgValue);            

				break;

			case ERROR_MORE_DATA:
				cout << "Buffer too small\n";
				break;			

			default:			
				cout << "Count not read key\n";
		}
	}
	else {
		cout << "Can not open key";
		cin.ignore();
	}

	return szReturnValue;	
}


This is the calling function just for reference, I wrote it from scratch, no help, lot cleaner than the registry functions I wrote. It's a rough draft, needs more detail, but in the end, after searching by wire, then search the local machine registry, if nothing is found, I will ask if they want to download the X86 or X64 version, and install it using an automated script that I write on the fly. It's starting to shape up now.

BOOL CA_SQLServer_Scan::_scan_Enumerate_Registry( HWND txtProgress, HWND lblServers, HWND pbProgress )
{
	CA_Registry caReg;
	BOOL bResult = FALSE;
	BOOL bInstance = FALSE;
	int instanceLength;
	WCHAR *szComputerName = NULL;
	WCHAR *szInstanceNames = NULL;
	WCHAR *szCurrentVersion = NULL;
	WCHAR szServerInstance[125];

	// Get the SQL Server instance names
	szComputerName = caReg._get_ComputerName();
	szInstanceNames = caReg._get_SQLServer_InstalledInstances();
	instanceLength = wcslen(szInstanceNames);
			
	if (instanceLength > 0) {

		WCHAR *szInstanceName = NULL;
		WCHAR *token1, *nextToken;
		WCHAR seps[] = L";";
		WCHAR vSeps[] = L".";
		token1 = wcstok_s(szInstanceNames, seps, &nextToken);
		do {
			if (token1 == NULL) { 
				break;
			}
			else {
				szInstanceName = token1;
				szCurrentVersion = caReg._get_SQLServer_Version(szInstanceName);
				
				// Parse the version Numbers
				int szVerValue = wcslen(szCurrentVersion);
				if ( szVerValue > 1 ) {
					WCHAR *szVerMajor = NULL, *szVerMinor = NULL, *szVerBuild = NULL;
					WCHAR *vToken1, *vToken2, *vToken3, *vNextToken;
					int iVerMajor=0, iVerMinor=0, iVerBuild=0;
					
					vToken1 = wcstok_s(szCurrentVersion, vSeps, &vNextToken);
					if (!vToken1 == NULL) {
						szVerMajor = vToken1;
						iVerMajor = _wtoi(szVerMajor);
					}
					
					vToken2 = wcstok_s(NULL, vSeps, &vNextToken);
					if (!vToken2 == NULL) {
						szVerMinor = vToken2;
						iVerMinor = _wtoi(szVerMinor);
					}

					vToken3 = wcstok_s(NULL, vSeps, &vNextToken);
					if (!vToken3 == NULL) {
						szVerBuild = vToken3;
						iVerBuild = _wtoi(szVerBuild);
					}

					WCHAR pbMessage[80];
					wcsncpy_s(pbMessage, _countof(pbMessage), L"Registered: ", wcslen(L"Registered: "));
					//wcsncat_s(pbMessage, _countof(pbMessage), L"LOCAL", wcslen(L"LOCAL"));
					wcsncat_s(pbMessage, _countof(pbMessage), L"\\", wcslen(L"\\"));
					wcsncat_s(pbMessage, _countof(pbMessage), szInstanceName, wcslen(szInstanceName));					
					SetWindowText(txtProgress, pbMessage);
					Sleep(500);
					
					// Analyse the Version Numbers
					switch (iVerMajor) 
					{
					case 11:
						bInstance = TRUE;
						break;
					case 10:
						bInstance = TRUE;
						break;
					case 9:
						bInstance = TRUE;
						break;
					default:
						bInstance = FALSE;
						break;
					}

					if (bInstance) {
						int iComputerName = wcslen(szComputerName);
						int iInstanceName = wcslen(szInstanceName);						
						
						// Build the Computer\\Instance
						wcsncpy_s(szServerInstance, _countof(szServerInstance), szComputerName, iComputerName);
						wcsncat_s(szServerInstance, _countof(szServerInstance), L"\\", wcslen(L"\\"));
						wcsncat_s(szServerInstance, _countof(szServerInstance), szInstanceName, iInstanceName);
						
						// Trim the wChar Array
						iInstanceName=wcslen(szServerInstance);	
						szServerInstance[iInstanceName-0]=(WCHAR)L'\0';

						// Get the Collection Count
						int iCollectionCount;
						iCollectionCount = sqlSrvCollection.Count();

						if (iCollectionCount > 0) {
							// Check for Duplicate
							WCHAR *szComparision = NULL;
							
							for (int i=0; i<iCollectionCount; i++) {
								szComparision = sqlSrvCollection[i];
								if (wcscmp(szServerInstance, szComparision) != 0) {
									int iCollection = sqlSrvCollection.Add();
									sqlSrvCollection[iCollection] = szServerInstance;
								}								
							}							
						}
						else {
							// Add the Server Instance
							int iCollection = sqlSrvCollection.Add();
							sqlSrvCollection[iCollection] = szServerInstance;
						}
					}
				}

				// Get the next token to cycle through
				token1 = wcstok_s(NULL, seps, &nextToken);

			}
		} while (TRUE);
	}
	
	return bResult;
}

GeneralRe: I rewrote the my function for WCHAR Pin
Richard MacCutchan29-Oct-11 22:21
mveRichard MacCutchan29-Oct-11 22:21 
GeneralRe: I rewrote the my function for WCHAR Pin
jkirkerx30-Oct-11 6:32
professionaljkirkerx30-Oct-11 6:32 
GeneralRe: I rewrote the my function for WCHAR Pin
Richard MacCutchan30-Oct-11 7:16
mveRichard MacCutchan30-Oct-11 7:16 
GeneralRe: I rewrote the my function for WCHAR - grovel Pin
Richard MacCutchan29-Oct-11 23:19
mveRichard MacCutchan29-Oct-11 23:19 
QuestionRegistry usage in MFC Doc/ View - opinions / commentary wanted Pin
Vaclav_27-Oct-11 5:18
Vaclav_27-Oct-11 5:18 
AnswerRe: Registry usage in MFC Doc/ View - opinions / commentary wanted Pin
Albert Holguin27-Oct-11 5:35
professionalAlbert Holguin27-Oct-11 5:35 
Questionfopen crashed when special characters found. Pin
tagopi27-Oct-11 2:12
tagopi27-Oct-11 2:12 
AnswerRe: fopen crashed when special characters found. Pin
Luc Pattyn27-Oct-11 2:53
sitebuilderLuc Pattyn27-Oct-11 2:53 
AnswerRe: fopen crashed when special characters found. Pin
Chris Losinger27-Oct-11 3:22
professionalChris Losinger27-Oct-11 3:22 
AnswerRe: fopen crashed when special characters found. Pin
David Crow27-Oct-11 4:49
David Crow27-Oct-11 4:49 
AnswerRe: fopen crashed when special characters found. Pin
Andrew Brock27-Oct-11 7:43
Andrew Brock27-Oct-11 7:43 
GeneralRe: fopen crashed when special characters found. Pin
jschell27-Oct-11 8:30
jschell27-Oct-11 8:30 
GeneralRe: fopen crashed when special characters found. Pin
Andrew Brock27-Oct-11 8:36
Andrew Brock27-Oct-11 8:36 
AnswerRe: fopen crashed when special characters found. Pin
Pranit Kothari27-Oct-11 19:01
Pranit Kothari27-Oct-11 19:01 
AnswerRe: fopen crashed when special characters found. Pin
tagopi27-Oct-11 19:40
tagopi27-Oct-11 19:40 
QuestionHow to refresh folder Pin
MKC00227-Oct-11 0:52
MKC00227-Oct-11 0:52 
AnswerRe: How to refresh folder Pin
Chandrasekharan P27-Oct-11 0:59
Chandrasekharan P27-Oct-11 0:59 

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.