MFC SDI OpenDocument() is resetting my main window size. How can I stop it? - mfc

I have implemented code to save and load the main window size and location, which works fine, however, when I open a document it changes the window size back to some internal default.
These are the calls from Windows I see leading up to when the change happens:
CSingleDocTemplate::OpenDocumentFile()
CFrameWnd::InitialUpdateFrame()
CWnd::SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE)
This is my code for saving and loading the window info:
BOOL CDisplayApp::InitInstance()
{
// existing code .....
LONG Ret;
HKEY RegistryKey;
DWORD type = REG_BINARY;
WINDOWPLACEMENT sWP;
DWORD sizewp = sizeof(WINDOWPLACEMENT);
Ret = RegOpenKeyEx(
HKEY_CURRENT_USER,
_T("SOFTWARE\\Local AppWizard-Generated Applications\\display\\PreservedWindowPos"),
0,
KEY_READ,
&RegistryKey);
if (Ret == ERROR_SUCCESS) {
Ret = ::RegQueryValueEx(RegistryKey,
_T("PosAndSize"),
0,
&type,
(LPBYTE)&sWP,
&sizewp);
if (Ret != ERROR_SUCCESS)
m_pMainWnd->ShowWindow(SW_SHOW);
else
m_pMainWnd->SetWindowPlacement(&sWP);
}
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// existing code .....
this->LoadBarState(_T("MainToolBar"));
}
void CMainFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default
LONG Ret;
HKEY Registry_Key;
DWORD disposition;
WINDOWPLACEMENT sWindow_Position;
SaveBarState(_T("MainToolBar"));
Ret = RegOpenKeyEx(
HKEY_CURRENT_USER,
_T("SOFTWARE\\Local AppWizard-Generated Applications\\display\\PreservedWindowPos"),
NULL,
KEY_WRITE,
&Registry_Key);
if (Ret != ERROR_SUCCESS)
{
RegCreateKeyEx(
HKEY_CURRENT_USER,
_T("SOFTWARE\\Local AppWizard-Generated Applications\\display\\PreservedWindowPos"),
NULL,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&Registry_Key,
&disposition);
}
GetWindowPlacement(&sWindow_Position);
RegSetValueEx(
Registry_Key,
_T("PosAndSize"),
NULL,
REG_BINARY,
(BYTE*)&sWindow_Position,
sizeof(WINDOWPLACEMENT));
RegCloseKey(Registry_Key);
CFrameWnd::OnClose();
}
I would like it if the window size and location were to stay the same when I open documents. How can I do that?

I think the correct answer here is that my code is old, and I need to create a new SDI project and graft my program onto it before going any further with this. Then I will see if I still have these problems.
NEVER MIND! I am ashamed to report that the problem was coming from my own window sizing code that I wrote in CView::OnInitialUpdate() and forgot about long ago. SORRY!

Related

In c++, how to disable windows UAC control by program?

In program C++, how to change the windows UAC to always notify or never notify.
Manually, I should change like this:
I found C# code from Internet:
const string UACkey = #"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
const string Subkey = "EnableLUA";
RegistryUtilities.SetValue(UACkey, Subkey, **1**, Microsoft.Win32.RegistryValueKind.DWord, RegistryUtilities.WOW64Key.Key64);
Does somebody know how to change into c++ code?
I tried to change the Registry with c++, but it does not work.
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwSize;
const size_t count = MAX_PATH*2;
wchar_t szValue[count] = {};
lResult = RegOpenKeyExW(HKEY_CURRENT_USER, L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, (KEY_WRITE | KEY_READ), &hKey);
fSuccess = (lResult == 0);
if (fSuccess)
{
dwSize = (wcslen(szValue)+1)*2;
lResult = RegSetValueExW(hKey, L"EnableLUA", 0, REG_SZ, (BYTE*)szValue, dwSize);
fSuccess = (lResult == 0);
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
To answer your stated question, you can set registry keys with RegSetKeyValue, however you have to get past UAC in order to change the registry. To answer your implicit question, yes you could probably disable UAC programatically for malicious purposes, however you have to get past UAC in the first place to do so. Once you're on the other side of the airtight hatchway, you can do whatever you want of course. However you still have to get past the UAC hurdle.
Furthermore, I highly recommend you do a cursory google search before posting a question like this. The Windows C API is documented very well on MSDN.
Your code is actually very close;
DWORD val = 0;
HKEY uaKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_WRITE , &uaKey);
if(uaKey == NULL)
{
//Handle error message here
}
else
{
if (RegSetValueEx (uaKey, "EnableLUA", 0, REG_DWORD, (const BYTE *)&val, sizeof(val)) != ERROR_SUCCESS)
{
//Error
}
else
{
//Success
}
RegCloseKey(uaKey);
}

How can i add registry key with c++ on HKEY_LOCAL_MACHINE [duplicate]

Could anybody tell me what's wrong is with this code? There is no errors. Everything returns ERROR_SUCCESS but in register can't see any changes.
void Utils::writePath(LPCTSTR data)
{
HKEY hkey;
DWORD dwDisposition;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\aaTestCompany\\testApp"),
0, NULL, 0,
KEY_WRITE, NULL,
&hkey, &dwDisposition) == ERROR_SUCCESS)
{
long setRes = RegSetValueEx (hkey, "testPath", 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if (setRes == ERROR_SUCCESS) {
printf("Success writing to Registry.");
} else {
printf("Error writing to Registry.");
}
RegCloseKey(hkey);
}
else
MessageBox(NULL,"error","",0);
}
As the application is 32-bit on a 64-bit OS the registry key will actually be created beneath HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node.
See 32-bit and 64-bit Application Data in the Registry.
Can you try with 5th and 6th parameters as REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS

Add Application to Startup (Registry)

I'm trying to add my software to registry, I have found some pieces of the codes I can use but not full working code C/C++ is new to me and can't create it on my own. But here is the basic idea: Check if reg key set if not create it.
I was able to get my program location using this code:
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);
And was able to create the key with: (Not sure if it's the right way)
HKEY newValue;
RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&newValue);
RegSetValueEx(newValue,"myprogram",0,REG_SZ,(LPBYTE)szPath,sizeof(szPath));
RegCloseKey(newValue);
return 0;
What is missing, A small check if the key isn't already there...
Thank you!
Here's some code that likely does what you want. Call RegisterProgram for your EXE to self-register itself for automatically being started when the user logs in. This function calls GetModuleFileName and then invokes another helper function called RegisterMyProgramForStartup that does the writing to the registry.
Call IsMyProgramRegisteredForStartup(L"My_Program") to detect if the registration actually exists and appears valid.
One quick note. The performance impact of checking to see if the key exists before actually writing it out again is negligible. You could just call RegisterProgram blindly and it will overwrite the key if it already exists. Detecting if the registration exists is useful for initializing your UI checkbox that enables or disables auto-start. (You are giving your users a choice, right? Because I hate apps that automatically install themselves to run automatically without giving me a choice.)
BOOL IsMyProgramRegisteredForStartup(PCWSTR pszAppName)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwRegType = REG_SZ;
wchar_t szPathToExe[MAX_PATH] = {};
DWORD dwSize = sizeof(szPathToExe);
lResult = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey);
fSuccess = (lResult == 0);
if (fSuccess)
{
lResult = RegGetValueW(hKey, NULL, pszAppName, RRF_RT_REG_SZ, &dwRegType, szPathToExe, &dwSize);
fSuccess = (lResult == 0);
}
if (fSuccess)
{
fSuccess = (wcslen(szPathToExe) > 0) ? TRUE : FALSE;
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return fSuccess;
}
BOOL RegisterMyProgramForStartup(PCWSTR pszAppName, PCWSTR pathToExe, PCWSTR args)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwSize;
const size_t count = MAX_PATH*2;
wchar_t szValue[count] = {};
wcscpy_s(szValue, count, L"\"");
wcscat_s(szValue, count, pathToExe);
wcscat_s(szValue, count, L"\" ");
if (args != NULL)
{
// caller should make sure "args" is quoted if any single argument has a space
// e.g. (L"-name \"Mark Voidale\"");
wcscat_s(szValue, count, args);
}
lResult = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, (KEY_WRITE | KEY_READ), NULL, &hKey, NULL);
fSuccess = (lResult == 0);
if (fSuccess)
{
dwSize = (wcslen(szValue)+1)*2;
lResult = RegSetValueExW(hKey, pszAppName, 0, REG_SZ, (BYTE*)szValue, dwSize);
fSuccess = (lResult == 0);
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return fSuccess;
}
void RegisterProgram()
{
wchar_t szPathToExe[MAX_PATH];
GetModuleFileNameW(NULL, szPathToExe, MAX_PATH);
RegisterMyProgramForStartup(L"My_Program", szPathToExe, L"-foobar");
}
int _tmain(int argc, _TCHAR* argv[])
{
RegisterProgram();
IsMyProgramRegisteredForStartup(L"My_Program");
return 0;
}
To check whether or not the value exists, call RegQueryValueEx.
LONG retval = RegQueryValueEx(hKey, "myprogram", NULL, NULL, NULL, NULL);
Note that what you called newValue is actually a key rather than a value. To avoid confusion you should name it such. I used the name hKey.
Then to check whether or not the value exists, compare retval against ERROR_SUCCESS as described in the documentation.
The other problem with your code is that there is absolutely no error checking. I'll leave that to you to address.
You forget to write an argument about security access

Save Me Simple Add to Startup via Registry [duplicate]

I'm trying to add my software to registry, I have found some pieces of the codes I can use but not full working code C/C++ is new to me and can't create it on my own. But here is the basic idea: Check if reg key set if not create it.
I was able to get my program location using this code:
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);
And was able to create the key with: (Not sure if it's the right way)
HKEY newValue;
RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&newValue);
RegSetValueEx(newValue,"myprogram",0,REG_SZ,(LPBYTE)szPath,sizeof(szPath));
RegCloseKey(newValue);
return 0;
What is missing, A small check if the key isn't already there...
Thank you!
Here's some code that likely does what you want. Call RegisterProgram for your EXE to self-register itself for automatically being started when the user logs in. This function calls GetModuleFileName and then invokes another helper function called RegisterMyProgramForStartup that does the writing to the registry.
Call IsMyProgramRegisteredForStartup(L"My_Program") to detect if the registration actually exists and appears valid.
One quick note. The performance impact of checking to see if the key exists before actually writing it out again is negligible. You could just call RegisterProgram blindly and it will overwrite the key if it already exists. Detecting if the registration exists is useful for initializing your UI checkbox that enables or disables auto-start. (You are giving your users a choice, right? Because I hate apps that automatically install themselves to run automatically without giving me a choice.)
BOOL IsMyProgramRegisteredForStartup(PCWSTR pszAppName)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwRegType = REG_SZ;
wchar_t szPathToExe[MAX_PATH] = {};
DWORD dwSize = sizeof(szPathToExe);
lResult = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey);
fSuccess = (lResult == 0);
if (fSuccess)
{
lResult = RegGetValueW(hKey, NULL, pszAppName, RRF_RT_REG_SZ, &dwRegType, szPathToExe, &dwSize);
fSuccess = (lResult == 0);
}
if (fSuccess)
{
fSuccess = (wcslen(szPathToExe) > 0) ? TRUE : FALSE;
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return fSuccess;
}
BOOL RegisterMyProgramForStartup(PCWSTR pszAppName, PCWSTR pathToExe, PCWSTR args)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwSize;
const size_t count = MAX_PATH*2;
wchar_t szValue[count] = {};
wcscpy_s(szValue, count, L"\"");
wcscat_s(szValue, count, pathToExe);
wcscat_s(szValue, count, L"\" ");
if (args != NULL)
{
// caller should make sure "args" is quoted if any single argument has a space
// e.g. (L"-name \"Mark Voidale\"");
wcscat_s(szValue, count, args);
}
lResult = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, (KEY_WRITE | KEY_READ), NULL, &hKey, NULL);
fSuccess = (lResult == 0);
if (fSuccess)
{
dwSize = (wcslen(szValue)+1)*2;
lResult = RegSetValueExW(hKey, pszAppName, 0, REG_SZ, (BYTE*)szValue, dwSize);
fSuccess = (lResult == 0);
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return fSuccess;
}
void RegisterProgram()
{
wchar_t szPathToExe[MAX_PATH];
GetModuleFileNameW(NULL, szPathToExe, MAX_PATH);
RegisterMyProgramForStartup(L"My_Program", szPathToExe, L"-foobar");
}
int _tmain(int argc, _TCHAR* argv[])
{
RegisterProgram();
IsMyProgramRegisteredForStartup(L"My_Program");
return 0;
}
To check whether or not the value exists, call RegQueryValueEx.
LONG retval = RegQueryValueEx(hKey, "myprogram", NULL, NULL, NULL, NULL);
Note that what you called newValue is actually a key rather than a value. To avoid confusion you should name it such. I used the name hKey.
Then to check whether or not the value exists, compare retval against ERROR_SUCCESS as described in the documentation.
The other problem with your code is that there is absolutely no error checking. I'll leave that to you to address.
You forget to write an argument about security access

Access is Denied for registry

I am playing with the registry programmatically for the first time, and it's not working that well (but at least I haven't destroyed my computer). Specifically, I keep getting back Error 5 (Access is Denied) from RegCreateKeyEx and RegSetValueEx. The thing that is strangest to me is that when HKEY_CURRENT_USER\Software\dir1\Sub Directory already exists, RegCreateKeyEx fails with Error 5, but when it doesn't already exist, it creates it successfully; and then fails on the RegSetValueEx.
Am I doing anything wrong in this code?
BOOL MyDialog::SaveLocationsToRegistry()
{
HKEY hkey;
DWORD dwDisposition;
DWORD dwType, dwSize;
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
0, NULL, 0, 0, NULL, &hkey, &dwDisposition);
if(result == ERROR_SUCCESS)
{
LPCTSTR szLastFolder = "C:\\Documents and Settings\\user\\My Documents\\Copy of item\0";
dwType = REG_SZ;
dwSize = strlen(szLastFolder)+1;
LONG setResult = RegSetValueEx(hkey, TEXT("LastFolder"), 0, dwType,
(PBYTE)&szLastFolder, dwSize);
RegCloseKey(hkey);
return setResult == ERROR_SUCCESS;
}
else
{
return false;
}
}
Note: The absolute path is only there temporarily. Baby steps ;-)
You're not asking for any access rights. You probably want to specify KEY_WRITE (or something) for the 6th parameter (samDesired).
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
0, NULL, 0, KEY_WRITE, NULL, &hkey, &dwDisposition);