Dynamic reading of satellite DLL without changing GUI using MFC - mfc

So I have a MFC application which has language resources in several satellite DLL files.
When the program starts it loads the correct DLL and sets it for the application resources.
My question, is it possible to read, say a STRINGTABLE entry from a DLL without setting it as a GUI? For example, I am running the application in ITALIAN, but for reasons I want to allow the show to show some information in SPANISH.
Can we access the STRINGTABLE of a satellite DLL without setting it as the application GUI?

Use
CString::LoadString(HINSTANCE hInstance, UINT nID) or
CString::LoadString(HINSTANCE hInstance, UINT nID, WORD wLanguageID).
Example:
HINSTANCE hinst = LoadLibrary(L"path.dll");
CString str;
if (str.LoadString(hinst, ID_STRING100))
AfxMessageBox(str);
You can also call AfxSetResourceHandle(hinst) and simply follow up with str.LoadString(ID_STRING100). Example:
AfxSetResourceHandle(hinst);
str.LoadString(ID_STRING100);
...
//reset resource handle:
AfxSetResourceHandle(AfxGetInstanceHandle());

Related

Spy++ Win32 API Getting Window instance from Spy++ Information

I am using Spy++ to find windows, I am doing this as a test and realise the Handles change fequently. However, here is the information I get from Spy++. Can I use these handles to grab that window in C++
Here's how I get it from the name.
HWND main_window_handle = FindWindowA(NULL, WINDOW_NAME);
How can I get it using either the Window Handle or Instance Handle.
The window handle is the HWND and their values are not stable, it will probably change every time you run the program.
The instance handle (HINSTANCE) is also not stable and has little to do with finding a specific window in another application, it is the load address of the module (.exe or .dll) that created the window.
To find a window you will generally call FindWindow with a specific class name. If the class name of the window you are looking for is not really unique then you should probably use EnumWindows and try to look for other specific attributes and/or child windows to identify the top level window you are looking for.
It is also possible (and often the best approach) to use UI Automation to find and manipulate windows in 3rd-party applications.
Try using
HINSTANCE myInstance = (HINSTANCE)&__ImageBase;

Creating a 'shutting down' window for a closing application

I am relatively new to C++ programming and currently using Visual Studio 2012 API. My OS is Windows 7 64-bit. I would like to write a program in C++ which displays a message box or window with a simple shutdown message e.g. 'shutting down.....' or something similar when I close a specific application. This window I am hoping will appear for the duration of the app exit time and then close.
Is it possible to create a handle which will retrieve the exit time for a running application when it is abruptly closed? And if so, how could I use this exit time in a statement which will display the message box?
I would appreciate constructive criticism as I am new to this language. Thank you sincerely for any advice you impart. If requested, I will display all source code.
I don't think this is the right way to go about this but anyway, below is a snippet of the code which I have been toying with, as part of a greater VS Win32 application project:
LPTSTR lpchText(_T("Shutting down...."));
LPFILETIME lpExitTime = 0; //Initialise
TCHAR Buffer[_MAX_PATH];
DWORD size = _MAX_PATH;
LPCTSTR lpStr(_T("C:\Program Files\Common Files\ExampleApp.exe")); // Path to executable app.
AssocQueryString( ASSOCF_OPEN_BYEXENAME,
ASSOCSTR_EXECUTABLE,
lpStr,
NULL,
Buffer,
&size
);
GetProcessTimes(
AssocQueryString,
NULL,
lpExitTime,
NULL,
NULL
);
while(lpExitTime){
MessageBoxEx(
_In_opt_ hWnd,
_In_opt_ lpchText,
_In_opt_ lpCaption,
_In_opt_ MB_ICONEXCLAMATION
,0
);
};
return TRUE;
enter code here
I guess shutting down the application is a lengthy operation othervise a normal application will be closed so fast that the user will have no chance to actual see the window.
But of course it possible, in meta code this is the steps.
Attach to the running process. You can enumrate the running process from the name of the executable to find the correct process.
Once you have the handle to the process you can attach to the process message loop.
Listen for the WM_QUIT (assuming it is a Windows applictaion) message and then display the window.
Wait for the process handle using MsgWaitForMultipleObject, the function will signal when the process terminates.
Attaching to other processes has some security issues so it might not work for a regular user.
Other options to explore is to handle it in a power Shell script or make a small launcher application that in turn starts the actual application.
Is it an option to modify the application itself to have this functionality?

How do I make my console C++ program work in the background?

I need it to take up as little processor time as possible and there's absolutely no need to create an actual window. I just need that process to hang out in the backdground.
Windows application are running always at background, and not have to be with GUI window, so this is what you ask for.
If you already have a console application, you'll just need to do the following:
In visual studio create an empty project (not a console, a Win32 app).
Add new c++ source file into that project
put the following code in the new source file:
this:
#include <Windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
//your program should replace this loop:
while(1){
Sleep(100); //because of the the empty loop, I don't want to waste CPU...
}
return 0;
}
use this wWinMain as your main, if you need arguments you'll need to parse pCmdLine.
Note that this is the UNICODE version, for non UNICODE use just WinMain and PSTR pCmdLine
like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
if your program needed to be stopped you can use task manager to kill it.
a system try icon also can help you, but then you'll need at least one window (possibly hidden)
This request fits Windows Service concept best. The example. You may implement that many different ways, C/C++ with Qt or not/C#. The advantage of creating the service and not hidden console is that the program is manageable via standard Windows Service interface for (un)register/start/stop/etc. with both administrative UI and console utilities.
Making the app consuming less CPU is much more delicate thing and depends on what your app does but the simpler answer is that don't run tight loops and make it wait until it is needed to process something. There are too many things to cover e.g. threading etc. but we don't know what your program does.

Generating Mouse & Keyboard input from one application to another

I have worked with the win32 API and the DirectX API for interpreting input from the user, but haven't found much in the way of generating input that matches original user input.
My goal is to make a program that will run transparently in the background, minimized, or in the quicklaunch area and have that program artificially implant keyboard and mouse input for a third party, full screen application.
I've found some commands for verifying which window has focus, and some code samples for checking the process name or ID of said application, but not as much on generating input via directx or anything else, to simulate legitimate input.
Any suggestions would be much appreciated, I want it to register as close to real input as possible to help facilitate automated testing.
My apologies in advance if this question is too general or "under-researched", I'm just not quite sure where to start on this one!
The program will be in either C++ or C# due to my familiarity with those languages.
I have done this:
One app to act as an global keyboard_hock, and then i did send matching patterns to an second app, direct in memory - i used examples from All-in-one code framework examples you can download, lock for CSFileMappingServer & CSFileMappingClient (get / set) to memory from two separete processes. And CSWindowsHook project to catch global keyboard events
I was a litle quick, but to send virtual keyboard you can use:
BOOL
WINAPI
PostMessageA(
__in_opt HWND hWnd,
__in UINT Msg,
__in WPARAM wParam,
__in LPARAM lParam);
But the winow you send to has to be in focus, you can use:
BOOL
WINAPI
BringWindowToTop(
__in HWND hWnd);
And to get HWIND to the running app, you can use:
HWND
WINAPI
FindWindowA(
__in_opt LPCSTR lpClassName,
__in_opt LPCSTR lpWindowName);

Find a window using c++ and modifying controls

I would like to use c++ without mfc(and not clr) in order to modify textbox's and activate a button on a form outside of my project. I don't know where to start. I've done a lot of searching but can only find information for VB. A starting point would help.
Thanks.
I tried this and it doesn't seem to work.
HWND fWindow = FindWindow(NULL ,(LPCWSTR)"title");
and I also tried this
HWND fWindow = FindWindow(NULL ,LPCWSTR("title"));
I ALSO tried using LPTSTR instead of LPCWSTR, incase it was a unicode deal.
Maybe I don't understand this microsoft LPCWSTR and LPTSTR crap.
I also tried
HWND fWindow = FindWindow(NULL,TEXT("title"));
and that didn't work.
I guess the windows api must just be broken.
I tried the function on other programs...I'm using xp and I tried catching the calculator, and an explorer window, and something else. But I got nothing.
Heres some of the exact code I'm using to try and figure this out.
HWND face = NULL;
face = FindWindow(NULL,TEXT("My Computer"));
LPSTR title = TEXT("");
GetWindowText(face,title,250);
if(face != NULL)
{
MessageBox(NULL,title,TEXT("WOOP"),1);
}
face = nothing.
title = ""
Bear in mind, I'm not actually trying to hook explorer, I just want to figure out how to get it to work.
Use spy++ or winspector to see the actual "text" of the window.
(Strictly speaking, the caption of the window need not match it's window text. Especially true of "fancy" windows which paint their own caption.)
The following works fine for me (using Calc.exe to test).
HWND hwnd = NULL;
hwnd = FindWindow(NULL,_T("Calculator"));
TCHAR title[251];
if(hwnd != NULL)
{
GetWindowText(hwnd,title,250);
MessageBox(NULL,title,_T("WOOP"),MB_OK);
}
else
MessageBox(NULL,_T("No such window."),_T("OOPS"),MB_OK);
Edit: You should have used _TEXT instead of TEXT.
One way to do this is to use FindWindow to get a handle to the form. Then if you know the button and edit window Ids, you can use GetDlgItem to get their window handles. If you dont know the ids, you can use EnumChildWindows to examine all of the controls on the form.
Once you have the window handles for the controls, you can use SetWindowText to set the text on the edit control, and send a WM_COMMAND message to the form window with the button ID as the command value to make the form think that the button has been clicked.
There are a lot of ways to go about this once you have the correct window handles. There are security issues when you use the window handles of another process, but if the process isn't secured, then inter-process use of window handles just works. For a secured process, you won't be able to find out the window handles.
The windows API provides Methods for this. These should be independent of MFC and CLR, as they are plain win32. I had a project once accessing the Form fields of an Applictation from a loaded DLL (don't ask why).
you might want to look here (Codeproject)
or here (msdn)
At first, you need to obtain a handle to the process you want to access.
When have this, you can use GetDlgItem() (search msdn for that) to retrieve a handle to the desired textbox.
With this handle, you should be able to modify the control in question.
If your trying to get big (and do some more UI automation), you sould have a closer look at these:
Microsoft Active Accessibility
IAccessible2
Microsoft UI Automation
Windows Automation API (Win7)