|
1. If your source file does not change,
2. and you read it each time, or keep the file handle open,
3. it is already sorted by your first key,
then you might also optimize your existing system by keeping track of the file position where each set of elements begin. That is, where do the 1 begin, the 2 begin, the 3 begin, etc. Then you can SetFilePosition() or seek() to that spot, and at least read until you get something NOT matching your number.
Otherwise, if always in memory, then use the multimap.
|
|
|
|
|
GREAT IDEAS!
THANK YOU!
CARLOS
|
|
|
|
|
I am trying to inject a dll into all running processes so I can hook all calls to CreateProcess(). I am going to intercept all calls to CreateProcess() by modifying the IAT, I know how to do that. My problem is injecting the dll into all processes using SetWindowsHookEx(). This is my code, it runs once or twice (triggering the MessageBox function in the main program), and then if you run it again, the computer freezes (and doesn't trigger the MessageBox function in the main program, meaning it got stuck on the InjectEnable() function). If anyone knows what the problem is, that would be highly appreciated.
// Main program:
#include <windows.h> //windows header
#include <stdio.h> //stdio header
#include "MyDLL.h"
#pragma comment(lib,"MyDLL.lib")
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
InjectEnable(); // Function in MyDLL.dll, enables injection
MessageBox(NULL,"End Application","Notification",MB_OK);
InjectDisable(); // Function in MyDLL.dll, disables injection
return 0;
}
// DLL cpp file:
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h> // Windows header
#include "MyDLL.h"
// Global variables (shared)
#pragma data_seg (".shared")
HHOOK g_hHook = 0;
#pragma data_seg ()
#pragma comment(linker,"/SECTION:.shared,RWS")
// Global variables (unshared)
HINSTANCE hDll;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
hDll = (HINSTANCE)hModule;
break;
}
case DLL_THREAD_ATTACH:
{
break;
}
case DLL_THREAD_DETACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
default:
break;
}
return TRUE;
}
LRESULT HookProc (
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
return ::CallNextHookEx(g_hHook, code, wParam, lParam);
}
bool MYDLL_API InjectEnable()
{
g_hHook = SetWindowsHookEx( WH_CBT, (HOOKPROC)HookProc, hDll, NULL );
if( g_hHook == NULL )
{
return false;
}
return true;
}
bool MYDLL_API InjectDisable()
{
UnhookWindowsHookEx( g_hHook );
g_hHook = NULL;
// Send a broadcast message, this forces the hook to trigger, and thus unload
SendMessage(HWND_BROADCAST,WM_NULL,0,0);
return true;
}
// DLL header file:
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
bool MYDLL_API InjectEnable();
bool MYDLL_API InjectDisable();
|
|
|
|
|
I'm not so sure why, but when I commented out the MessageBox in the main program, it did not crash. It installed and removed the hook successfully (to my knowledge). I have another problem though. With the hook installed, if I try to open another program, like Internet Explorer or some kind of software, Windows Crashes. It freezes and then all my icons dissappear and the taskbar goes away, leaving only the background and the cursor. Anyone have any idea why this would be happening? Any help is appreciated.
Thanks in advance
-Dev578
|
|
|
|
|
As an exercise, I am wrapping some of the lower level win32 structures and methods into a C++ object library.
I'd like to avoid forcing the user to do things like "register" classes. I think this is error prone to have the user make 'one' time calls, so I am trying to take advantage of C++ static properties. Currently, I declare an class (static) ATOM and initialize it with a function that is local to the cpp file.
My question revolves around the HINSTANCE member of the WNDCLASSEX struct. It clear that for win2k+, the hinstance parameter is ignored in the CreateWindowEx function - but what about for the population of WNDCLASSEX and subsequent RegisterClassEx calls?
It seems to work in my demo application (using WNDCLASSEX.hInstance = NULL that is). I have also had luck using the return value of GetModuleHandle.
Any suggestiong? I'd like to know whats going on under the hood. Will these methods work in simple cases and not more complex? I've even done this in a small DLL and seemed to have no problems using absolutely no HINSTANCE in the WNDCLASSEX ....
When one registers a class, is it registered systemwide? so that any HINSTANCE could instantiate it? The documentation says the WNDPROC must exist in the HINSTANCE. When I don't provide a hinstance, maybe it assumes the hinstance the call is being made from and so far, my WNDPROC always exists there?
This all comes back to the question, should I force the user to provide a HINSTANCE and consequently, Register classes explicitly.
Any direction would be appreciated.
Thanks in advance,
-Luther
|
|
|
|
|
Hi Luther,
I can only answer some of the questions you've put here. Hopefully someone else will jump in and answer the remainder.
Regarding HINSTANCE
I only know what's available in MSDN and the platform SDK, and these don't cover what goes on "under the hood" for functions like RegisterClassEx and CreateWindowEx. From experience, using NULL for the HINSTANCE doesn't seem to make much difference (but you already knew that). I don't know the consequence of using NULL for the HINSTANCE in these functions.
HINSTANCE can be important for other things though. One example is the TBADDBITMAP structure for a toolbar must get a real HINSTANCE value (not NULL) to create the toolbar.
The bottom line is that you will probably find you'll need the HINSTANCE of your application to do some things, but you'll get away with NULL for some others. Not a very adequate answer, but there you go.
Regarding RegisterClassEx
You need to call this in your application before it creates a normal window. The name registered for the window is not global, each application needs to register the windows before creating it. Using NULL as the HINSTANCE doesn't make it global or system wide.
Some predefined window types don't need to be registered. These include dialogs, edit controls, tool bars, status bars and so forth. All "general purpose" windows, however, need to be registered in the application that uses them.
By the way, you might find my article "Win32 Framework for SDI Applications" a handy reference. The URL is: http://www.codeproject.com/win32/Framework.asp
regards,
David
If it was easy, anyone could do it.
|
|
|
|
|
Many Thanks David.
I have a built a very similar framework - but with templates. Mainly to get as far away from virtual tables as possible. Sometimes subclassing, sometimes superclassing ... but templates add a nice twist.
I've come across one or two articles on Thunking (a lengthy one here on codeproject) but it didn't seem to work perfectly in .NET.
I'm not an ASSEMBLER, and I was probably doing something incorrectly, but setting the callback function to the thunk wasn't properly executing and the application would freeze. I'm not especially fond of the GetWindowLongPtr method, but at least it works. I'm concerned that an application with windows containing several children - each one using GetWindowLongPtr approach is not going to scale well.
Of course that's relative - but c windows apps will always have that speed advantage. It'll probabaly work out just fine ... just trying to explore for a clean, fast solution.
Hoping the lack of HINSTANCE in the WNDCLASSEX doesn't come back to bite me.
Thanks again,
-Luther
|
|
|
|
|
Luther Baker wrote:
Hoping the lack of HINSTANCE in the WNDCLASSEX doesn't come back to bite me.
Actually I was interested in your question about HINSTANCE, and was hoping someone else would come up with a definitive answer (hence my delay in answering your question). If you ever get to the bottom of the implications of using an HINSTANCE of NULL it might make for an interesting article on Code Project (for me anyway ).
I'm currently toying with using TLS (Thread Local Storage) as my thunk (as one of the comments to my article suggested). Seems to work well. Might be easier and more maintainable than assembly. Just a thought.
Best Regards,
David
|
|
|
|
|
Hello friend, somebody know how i can to repaint my dialog, because I open a dialog2 over dialog1 but when I close this dialog2, the dialog1 and his controls is cover with a gray shade, Why???. .
How I can to repaint the dialog???.
All my dialogs are open in form modal. The dialog1 is Child over the main Application, the dialog2 is a child opened over the dialog1 (popup)
I will be waiting your helps, .
The best Regards, .
HackPrince
NiponWare
|
|
|
|
|
Just a guess but this sounds like a WS_CLIPCHILDREN and/or WS_CLIPSIBLINGS problem
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
In Where such the messages?, It's in the Dialog1 or Dialog2???, because I make it with both, but I can't to resolve.
Please somebody know about it???
Regards
NiponWare
|
|
|
|
|
I call the dialog2 with dialog2.DoModal();
NiponWare
|
|
|
|
|
HackPrince wrote:
In Where such the messages?
They are styles, not messages. Look in your project's .rc file and see if the dialog templates (including the controls) use either of these styles.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I want the letter 'O' to be typed in the document of the wordpad application (sample of VC++6.0), when I type 'k'. Similarly I should be able to type one letter and get a sequence of letters (predefined string)typed in the current place of the document. How can I achieve this ?
Thanks in Advance.
Jai Srimannarayana
|
|
|
|
|
Sounds like you might need either a keystroke hook, or you need to process the WM_CHAR message.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks for your suggestion. I added onChar to the view and tried to access the nChar. But I couldn't use unshift alphabet in SendMessage whereas it works for Capital alphabet. How can I achieve it for small alphabet ?
Thanks
Jai Srimannarayana
|
|
|
|
|
kristna wrote:
But I couldn't use unshift alphabet in SendMessage...
What does the call to SendMessage() look like?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
It is not working for characters beyond 96. Any other way ?
void CWordPadView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case 'b':
SendMessage(WM_CHAR,'e',0);
break;
...
Can I get some solution ?
Jai Srimannarayana
Jai Srimannarayana
|
|
|
|
|
Is it possible (legally?) to hook CoCreateInstance / CoGetClassObject class for the current process?
I'm trying to move global component registration to side-by-side-DLL's, and hooking these would make the process awfully efficient
Pandoras Gift #44: Hope. The one that keeps you on suffering. aber.. "Wie gesagt, der Scheiss is' Therapie" boost your code || Fold With Us! || sighist | doxygen
|
|
|
|
|
You can redirect CoGetClassObject() on a per-CLSID basis by setting the TreatAs reg entry in the HKCR\CLSID\{guid} reg key; there are docs on this under "TreatAs" and "CoTreatAsClass" in MSDN.
--Mike--
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ
Strange things are afoot at the U+004B U+20DD
|
|
|
|
|
I've hooked CoGetClassObject with success, but I also had to hook LoadTypeLibEx as well. The reason is because even tho it was LoadLibrary'd from the correct location, a previously registered activex control may trigger LoadTypeLibEx (not sure what does trigger it) to load the dll as a resource and use the IDL contained within. Caused me much grief for a bit as I knew the dll was loaded correctly, but the activex calls were calling the wrong stuff.
There are other "TypeLib" functions you may need to hook as well. I had a specific need and didn't research all the variations.
Hope this helps.
--
Joel Lucsy
|
|
|
|
|
Hi,
Has any one tried to compare two images using the cximage library. If you know how to do it or if you have any sample codes. Please help me with that. Thanks a lot.
Madhurya.
|
|
|
|
|
My method was to convert the images to grey scale and then check each pixel.
But I was more interested in seeing if there were any differences between two images not if they were equal.
But if the images are identical, then the files are also identical, just do a 'diff' on the files.
"An education isn't how much you have committed to memory, or even how much you know. It's being able to differentiate between what you do know and what you don't." - Anatole France
|
|
|
|
|
|
If you just need to know if they are exactly the same or not then you might try this:
if( img1.GetSize() == img2.GetSize() )
{
if( memcmp(img1.GetDib(),img2.GetDib(),img1.GetSize()) )
else
}
Or you could just use the GetPixelColor() function to read the individule pixels one at a time, and compare them by calling CompareColors(). That would allow you to know exactly what pixels are different.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|