|
I do the enumerating as follows. How can I avoid the multiple findings. I mean, say there is more than one time zone for same time offset. In that case I want to display the very first time zone name. Used a break statement as follows. For some offsets it doesn't work.
for (i=0; i < cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
char newPath[255];
sprintf(newPath, "%s\\%s", path, achKey);
printf("\n%s", newPath);
if(RegOpenKey(HKEY_LOCAL_MACHINE, newPath, &hSubKey) == ERROR_SUCCESS)
{
if(RegQueryValueEx(hSubKey, "Display", 0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
{
string strDisplay(buf);
string strSub = strDisplay.substr(4, 6);
int result;
result = strcmp(strSub.c_str(), str.c_str());
if(result == 0)
{
printf("\n%s", strDisplay.c_str());
break;
}
}
RegCloseKey(hSubKey);
}
else
{
printf("\nNo ");
}
}
I appreciate your help all the time...
Eranga
|
|
|
|
|
you wrote: For some offsets it doesn't work
which registry keys does it not work ?
|
|
|
|
|
for -09:00 it wont work. i'm expecting it for Alaskan Standard Time time zone. That is the second subkey of the Time Zones key of the registry. My code don't recognized it at all.
I appreciate your help all the time...
Eranga
|
|
|
|
|
i will use something like this:
i will store all zones into m_pszTimeZones
and after that it is easy to get information you want
.h
CStringArray m_pszTimeZones;
.cpp
void <myclassname>::TestTM
{
HKEY hKey, Key;
DWORD dwType = REG_SZ;
int iRet = 0;
CString strName, strValue, strPath, strKey;
strPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_READ, &hKey);
if (iRet != ERROR_SUCCESS)
return;
TCHAR achKey[MAX_PATH];
TCHAR achClass[MAX_PATH] = _T("");
DWORD cchClassName = MAX_PATH;
DWORD cSubKeys;
DWORD cbMaxSubKey;
DWORD cchMaxClass;
DWORD cValues;
DWORD cchMaxValue;
DWORD cbMaxValueData;
DWORD cbSecurityDescriptor;
FILETIME ftLastWriteTime;
DWORD i;
DWORD retCode;
TCHAR buffer[MAX_PATH];
DWORD dwSize = sizeof(buffer);
// Get the class name and the value count.
RegQueryInfoKey(hKey, // key handle
achClass, // buffer for class name
&cchClassName, // length of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// Enumerate the child keys, until RegEnumKeyEx fails. Then
// get the name of each child key and copy it into the list box.
for (i = 0, retCode = ERROR_SUCCESS;retCode == ERROR_SUCCESS; i++)
{
ZeroMemory(&achKey, sizeof(achKey));
cbMaxSubKey = sizeof(achKey);
retCode = RegEnumKeyEx(hKey, i, achKey, &cbMaxSubKey, NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == (DWORD)ERROR_SUCCESS)
{
strName.Format( _T("%s"), achKey);
strKey = strPath + "\\" + strName;
iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, &Key);
if (iRet == ERROR_SUCCESS)
{
ZeroMemory(&buffer, sizeof(buffer));
dwSize = sizeof(buffer); //again
iRet = RegQueryValueEx(Key, _T("Display"), NULL, &dwType, (BYTE *)buffer, &dwSize);
if (iRet == ERROR_SUCCESS)
{
strValue.Format(_T("%s"), buffer);
m_pszTimeZones.Add( strValue );
}
RegCloseKey(Key);
}
}
}
RegCloseKey( hKey );
}
|
|
|
|
|
Then do the comparing with user inputs?
I appreciate your help all the time...
Eranga
|
|
|
|
|
yes...
like where strInput = "-09:00"
CString strTest;
int i;
for(i=0;i<=m_pszTimeZones.GetUpperBound();i++)
{
strTest = m_pszTimeZones.GetAt(i);
strTest = strTest.Mid(4,6);
if ( strTest == strInput)
break;
}
|
|
|
|
|
Ok, thanks. I'll check it and let you know.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Hey dan o, your code works fine. Where I'm going wrong is the use of my buffer. I'm not sure what exactly happened, but the changing of the buffer size for different time zones gives an error.
Thanks buddy,
At the same time, I'm try to do another thing. Say I want to get both display name and standard zone name. How can I do that through the above your code.
I appreciate your help all the time...
Eranga
|
|
|
|
|
ok, anyway
just closing this item now...
just make a new stringarray
CStringArray m_pszTimeZonesNames
if (iRet == ERROR_SUCCESS)
{
ZeroMemory(&buffer, sizeof(buffer));
dwSize = sizeof(buffer); //again
iRet = RegQueryValueEx(Key, _T("Std"), NULL, &dwType, (BYTE *)buffer, &dwSize);
if (iRet == ERROR_SUCCESS)
{
strValue.Format(_T("%s"), buffer);
m_pszTimeZonesNames.Add( strValue );
}
have a nice day
|
|
|
|
|
That's what I have tried. But when I add a CStringArray and compile its ok, but I can't run the code. When I run following exception given,
Unhandled exception at 0x00414ff3 in TimeZone.exe: 0xC0000005: Access violation writing location 0x00000020.
on following line of code.
m_pMainWnd = &dlg;
I have no idea about that. Even on CStringArray documentation there is no such explanation.
I appreciate your help all the time...
Eranga
|
|
|
|
|
i wonder if you did exactly as below
.h
m_pszTimeZones
m_pszTimeZonesNames
.cpp
void <myclassname>::TestTZ()
{
HKEY hKey, Key;
DWORD dwType = REG_SZ;
int iRet = 0;
CString strName, strValue, strPath, strKey;
strPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_READ, &hKey);
if (iRet != ERROR_SUCCESS)
return;
TCHAR achKey[MAX_PATH];
TCHAR achClass[MAX_PATH] = _T("");
DWORD cchClassName = MAX_PATH;
DWORD cSubKeys;
DWORD cbMaxSubKey;
DWORD cchMaxClass;
DWORD cValues;
DWORD cchMaxValue;
DWORD cbMaxValueData;
DWORD cbSecurityDescriptor;
FILETIME ftLastWriteTime;
DWORD i;
DWORD retCode;
TCHAR buffer[MAX_PATH];
DWORD dwSize = sizeof(buffer);
// Get the class name and the value count.
RegQueryInfoKey(hKey, // key handle
achClass, // buffer for class name
&cchClassName, // length of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// Enumerate the child keys, until RegEnumKeyEx fails. Then
// get the name of each child key and copy it into the list box.
for (i = 0, retCode = ERROR_SUCCESS;retCode == ERROR_SUCCESS; i++)
{
ZeroMemory(&achKey, sizeof(achKey));
cbMaxSubKey = sizeof(achKey);
retCode = RegEnumKeyEx(hKey, i, achKey, &cbMaxSubKey, NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == (DWORD)ERROR_SUCCESS)
{
strName.Format( _T("%s"), achKey);
strKey = strPath + "\\" + strName;
iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, &Key);
if (iRet == ERROR_SUCCESS)
{
ZeroMemory(&buffer, sizeof(buffer));
dwSize = sizeof(buffer); //again
iRet = RegQueryValueEx(Key, _T("Display"), NULL, &dwType, (BYTE *)buffer, &dwSize);
if (iRet == ERROR_SUCCESS)
{
strValue.Format(_T("%s"), buffer);
m_pszTimeZones.Add( strValue );
}
if (iRet == ERROR_SUCCESS)
{
ZeroMemory(&buffer, sizeof(buffer));
dwSize = sizeof(buffer); //again
iRet = RegQueryValueEx(Key, _T("Std"), NULL, &dwType, (BYTE *)buffer, &dwSize);
if (iRet == ERROR_SUCCESS)
{
strValue.Format(_T("%s"), buffer);
m_pszTimeZonesNames.Add( strValue );
}
}
RegCloseKey(Key);
}
}
}
RegCloseKey( hKey );
}</myclassname>
|
|
|
|
|
Oops, it's work.
I have to clear the array after certain time I think. Do this after some time of use the array.
m_pszTimeZones.RemoveAll();
Now seems it is work.
Another thing also tested by me.
On comparing of user input with array element, if the user input is not found what happened.
for(i=0;i<=m_pszTimeZones.GetUpperBound();i++)
{
strTest = m_pszTimeZones.GetAt(i);
strTemp = m_pszTimeZones.GetAt(i);
stdTT = m_pszNames.GetAt(i);
strTest = strTest.Mid(4,6);
if ( strTest == strIn)
{
SetDetails(strTemp);
SetDlgItemText(IDC_STD, stdTT);
break;
}
else
{
AfxMessageBox("Error");
break;
}
}
This is what I have tried. Because of the second break it wont iterate. If removed the second break, get the error message until it is found. How should I avoid it.
I appreciate your help all the time...
Eranga
|
|
|
|
|
BOOL bFound = FALSE;
for(i=0;i<=m_pszTimeZones.GetUpperBound();i++)
{
strTest = m_pszTimeZones.GetAt(i);
strTemp = m_pszTimeZones.GetAt(i);
stdTT = m_pszNames.GetAt(i);
strTest = strTest.Mid(4,6);
if ( strTest == strIn)
{
// Here find the correct zone name
SetDetails(strTemp);
SetDlgItemText(IDC_STD, stdTT);
bFound = TRUE;
break;
}
}
if (!bFound)
{
AfxMessageBox("Error, No Found");
}
|
|
|
|
|
Hmm, thanks it works. So, it's better to rate your advice to use of CStringArray.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Hi all,
After completing the time zone application now I'm going to workout a similar application.
Say I have a folder which contain large number of files. I want to read that folder and collect some information on those files. In details view of a folder we can see that lots of details are available.
eg: File Name, Size, Date Created, Date Modified and many more.
I want to read a specific folder and collect those details. So from where I should start this work.
I appreciate your help all the time...
Eranga
|
|
|
|
|
perhaps next code snippet helps you
CStringArray m_pszFileNames
void <myclass>::TestFile()
{
struct _finddata_t g_file;
long hFile, lTime;
CString strName, strPath, strCurrentPath;
m_pszFileNames.RemoveAll();
strPath = "c:\testpath";
/* Get the current working directory: */
char buffer[_MAX_PATH];
_getcwd( buffer, _MAX_PATH );
strCurrentPath.Format( _T("%s"), buffer);
_tchdir(strPath);
if( (hFile = _findfirst( "*.*", &g_file )) == -1L )
{
printf( "No files in current directory!\n" );
}
else
{
strName = g_file.name;
m_pszFileNames.Add(strName);
/* Find the rest of the *.* files */
while( _findnext( hFile, &g_file ) == 0 )
{
strName = g_file.name;
m_pszFileNames.Add(strName);
lTime = g_file.time_write;
}
_findclose( hFile );
}
_tchdir(strCurrentPath);
}
|
|
|
|
|
Hi Experts,
I want to name of some API to perfome the following tasks:
1.How to Restart the system programatically?
2.API that determines system getting shutdown.
3.API that tells system is starting.
Thanx in advance.
|
|
|
|
|
pther wrote: 1.How to Restart the system programatically?
ExitWindowsEx().
pther wrote: 2.API that determines system getting shutdown.
WM_QUERYENDSESSION
pther wrote: 3.API that tells system is starting.
If the system is in starting state then how can your application be running? Or you want your application to run on startup?
|
|
|
|
|
pther wrote: 1.How to Restart the system programatically?
See ExitWindowsEx
pther wrote: 2.API that determines system getting shutdown.
See WM_QUERYENDSESSION, WM_ENDSESSION messages
pther wrote: 3.API that tells system is starting.
I dont know pal...
|
|
|
|
|
Hi all,
I am trying to create a file by my program on a path specified by user.
I want to verify this path that this path exist or not and is there a file with same name already created....Is there any api which could help me in doing this........
Thanks
|
|
|
|
|
|
|
Use CFileFind class & its member functions
this may help you.
|
|
|
|
|
PathFileExist is the api you have to use for said problem, also you have to include shlwapi.lib in your program!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
You can use of FindFirstFile for check does file exist or no.
|
|
|
|
|