I'm trying to recreate the famous Windows 93 Hydra virus for Windows 95 using Microsoft Visual C++ 4.0, but due to my inexperience with Win32 I'm having trouble launching the main dialog box.
Here is what I was able to come with so far (the rest of the code can be found in this github repo):
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"
static HINSTANCE hInstanceGlobal;
BOOL CALLBACK DialogProc(HWND H, UINT M, WPARAM W, LPARAM L);
inline int CreateHead()
{
return DialogBox(hInstanceGlobal,
MAKEINTRESOURCE(HYDRA),
0, DialogProc);
}
BOOL CALLBACK DialogProc(HWND H, UINT M, WPARAM W, LPARAM L)
{
switch (M)
{
case WM_COMMAND:
CreateHead();
CreateHead();
case WM_INITDIALOG:
return TRUE;
default:
return FALSE;
}
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInstanceGlobal = hInstance;
return CreateHead();
}
The program compiles, but when I run it does absolutely nothing.
What am I doing wrong? How could I achieve the desired effect?
Related
I'm trying to develop a little helper tool to enable mouse integration for Windows 3.11 in VMWare Fusion since VMWare does not provide tools for this platform.
I can already read the mouse position in my application using the WH_MOUSE hook using the following code:
// VMOUSE - Hook global mouse to escape VM automatically
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "smprintf.h"
HHOOK hMouseHook;
LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
MOUSEHOOKSTRUCT *pMouseStruct = (MOUSEHOOKSTRUCT*)lParam;
printf("Mouse position X = %d, Y = %d\n", pMouseStruct->pt.x, pMouseStruct->pt.y);
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
hMouseHook = SetWindowsHookEx(WH_MOUSE, mouseProc, hInstance, NULL);
MessageBox(NULL, "Hello World", "Title", MB_OK);
return 0;
}
I understand that modern Win32 provides a WH_MOUSE_LL hook to detect mouse movement outside of the application itself and I was wondering if such a hook was available for Windows 3.11.
The communication between the host and the VM is a whole other thing, currently I'm planning to do this via winsock and trigger the keystroke on the host.
It would be amazing if anyone actually knows how to do this.
Thank you
I'm working on window in the winapi that displays a cef browser. My code compiles without errors and doesn't run into any runtime errors, but while my window displays, my cef webpage does not (my window is entirely blank). I've spent about 6 hours going about this but still haven't got anything to work.
I have my window in a separate class from my main function, and I think that might be the cause of my problems, as my g_handler might not be passed correctly.
Thanks for your help!
I stripped all of my windows api code of my examples (as it has been working fine) to keep my code samples short.
Here is my code:
Winmain:
#include "trackboxWrapper.h"
#include <stdexcept>
#include <thread>
#include "include\cef_app.h"
CefRefPtr<trackboxCefHandler> g_handler;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
try
{
CefMainArgs main_args(hInstance);
int exitCode = CefExecuteProcess(main_args, NULL);
if (exitCode >= 0) {
return exitCode;
}
CefSettings settings;
CefRefPtr<CefApp> cefApplication;
CefInitialize(main_args, settings, cefApplication);
trackboxWrapper window(g_handler);
CefRunMessageLoop();
CefShutdown();
}
catch (std::exception& e)
{
MessageBoxA(0, (std::string("Trackox Internal Error \n\n") + e.what()).c_str(), "=(", 0);
}
}
trackboxWrapper(only cef relavent parts are shown):
header(trackboxWrapper.h):
[code]#include <windows.h>
#include "include\cef_app.h"
#include "include\cef_base.h"
#include "include\cef_browser.h"
#include "include\cef_client.h"
class trackboxWrapper
{
public:
CefRefPtr<trackboxCefHandler> & g_handler;
trackboxWrapper(CefRefPtr<trackboxCefHandler> g_handler_pointer);
~trackboxWrapper();
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
}
cpp(trakcboxWrapper.cpp):
trackboxWrapper::trackboxWrapper(CefRefPtr<trackboxCefHandler> g_handler_pointer) : g_handler(g_handler_pointer) {}
LRESULT CALLBACK trackboxWrapper::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
trackboxWrapper *window = reinterpret_cast<trackboxWrapper*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (!window) return DefWindowProc(hwnd, msg, wparam, lparam);
switch (msg)
{
case WM_CREATE:
{
window->g_handler = new trackboxCefHandler();
RECT trackboxCefRect;
GetClientRect(hwnd, &trackboxCefRect);
CefWindowInfo info;
CefBrowserSettings settings;
info.SetAsChild(hwnd, trackboxCefRect);
CefBrowserHost::CreateBrowser(info, window->g_handler.get(), L"http://www.google.com", settings);
}
}
trackboxCefHandler.h:
#ifndef TRACKBOXCEFHANDLER_H_
#define TRACKBOXCEFHANDLER_H_
#include "include/cef_client.h"
class trackboxCefHandler : public CefClient {
public:
trackboxCefHandler() {}
~trackboxCefHandler() {}
IMPLEMENT_REFCOUNTING(trackboxCefHandler);
IMPLEMENT_LOCKING(trackboxCefHandler);
};
#endif
You create g_handler in three places, that might be your problem.
One is in the file with your Winmain (first code snippet where you declare this variable).
CefRefPtr<trackboxCefHandler> g_handler;
Another is in trackboxWrapper.h.
class trackboxWrapper
{
public:
CefRefPtr<trackboxCefHandler> g_handler;
....
And the third one is in trackboxWrapper.cpp:
case WM_CREATE:
{
CefRefPtr<trackboxCefHandler> g_handler = new trackboxCefHandler();
These are three different variables because they are fully declared in each of these places, so they shadow each other too (though the one in header file gets initialized with the argument in constructor, which leaves two).
You definitely don't need to create it in the third snippet, because you have already declared g_handler in your header:
case WM_CREATE:
{
window->g_handler = new trackboxCefHandler();
will be enough.
That way you should have the same object everywhere.
I am running the following to create a dialog box in windows. when I run it I get the follwoing error:
Error 1 error C2065: 'IDD_DLGFIRST' : undeclared identifier
Here is the code:
HWND hWnd;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
I do know that there are resource files but I haven't understood that very well.
could someone help me out to resolve this error please.
You need to define the symbol both in the resource file, as well as the file that calls MAKEINTRESOURCE. usually it's done via a common header file that you #include in both places (for instance #include resource.h in resource.rc and main.cpp).
And in the resource.h you put #define IDD_DLGFIRST 1 for example. Just make sure that the number is unique across resources.
EDIT:
As an example:
resource.h
#define IDD_DLGFIRST 1001
#define IDC_STATIC 1002
yourapp.rc
#include <windows.h>
#include "resource.h"
IDD_DLGFIRST DIALOGEX 0, 0, 170, 62
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My dialog"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "My first dialog box, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
DEFPUSHBUTTON "OK",IDOK,113,41,50,14,WS_GROUP
END
yourapp.cpp
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//Open dialog box
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST), HWND_DESKTOP, DlgProc);
return 0;
}
INT_PTR CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
break; //Don't forget about the break;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
}
Easiest way to put this: My code is throwing an access violation error whenever I run it. I have concluded it is the fault of calling this->d3ddev.(whatever) inside of the function System::renderFrame(). This function starts on line 112. If anyone could help me out here, that'd be great.
(by the way, I've gotten this working, but I wanted to put this code into classes, and that's where I started having my troubles. Also, I was told before to make sure to initialize all pointers. They are initialized, through d3d->createDevice())
system.h
#ifndef SYSTEM_H
#define SYSTEM_H
#include "stdinc.h"
class System {
private:
void initD3D (void);
void cleanD3D (void);
void setUpHWND (HINSTANCE, LPSTR, int);
static LRESULT CALLBACK StaticWindowProc(HWND, UINT, WPARAM, LPARAM);
LRESULT WindowProc(HWND, UINT, WPARAM, LPARAM);
HWND window;
WNDCLASSEX windowClass;
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
HINSTANCE hInstance;
LPSTR lpCmdLine;
int nCmdShow;
public:
System (void);
System (HINSTANCE, LPSTR, int);
System (const System&);
~System (void);
void renderFrame (void);
};
#endif
system.cpp
#include "system.h"
//////////////////////////////////////////////////
// Class: System
// Private
//////////////////////////////////////////////////
void System::initD3D (void) {
this->d3d = Direct3DCreate9(D3D_SDK_VERSION);
ZeroMemory(&(this->d3dpp), sizeof(d3dpp));
this->d3dpp.Windowed = WINDOWED;
this->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
this->d3dpp.hDeviceWindow = this->window;
this->d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
this->d3dpp.BackBufferWidth = SCREEN_WIDTH;
this->d3dpp.BackBufferHeight = SCREEN_HEIGHT;
this->d3dpp.EnableAutoDepthStencil = TRUE;
this->d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
this->d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
this->window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&(this->d3dpp),
&(this->d3ddev));
this->d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
this->d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
this->d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
};
void System::cleanD3D (void) {
this->d3d->Release();
this->d3ddev->Release();
};
void System::setUpHWND (
HINSTANCE hInstance,
LPSTR lpCmdLine,
int nCmdShow) {
this->hInstance = hInstance;
this->lpCmdLine = lpCmdLine;
this->nCmdShow = nCmdShow;
ZeroMemory(&(this->windowClass), sizeof(WNDCLASSEX));
this->windowClass.cbSize = sizeof(WNDCLASSEX);
this->windowClass.style = CS_HREDRAW | CS_VREDRAW;
this->windowClass.lpfnWndProc = System::StaticWindowProc;
this->windowClass.hInstance = this->hInstance;
this->windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
this->windowClass.lpszClassName = "WindowClass";
RegisterClassEx(&(this->windowClass));
this->window = CreateWindowEx(NULL, "WindowClass", "The Direct3D Program",
WS_OVERLAPPEDWINDOW, SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT,
NULL, NULL, this->hInstance, NULL);
};
LRESULT CALLBACK System::StaticWindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
System *SystemPtr = (System*)GetWindowLong(hWnd, GWLP_USERDATA);
if(SystemPtr)
{
return SystemPtr->WindowProc(hWnd, message, wParam, lParam);
}
else
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
};
LRESULT System::WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
};
//////////////////////////////////////////////////
// Class: System
// Public
//////////////////////////////////////////////////
System::System (void) {
};
System::System (
HINSTANCE hInstance,
LPSTR lpCmdLine,
int nCmdShow) {
this->setUpHWND(hInstance, lpCmdLine, nCmdShow);
ShowWindow(this->window, this->nCmdShow);
this->initD3D();
};
System::System (const System &) {
};
System::~System (void) {
this->cleanD3D();
};
void System::renderFrame (void) {
// Update the camera here
// Update objects
// Clear objects
// FOR SOME REASON THERE IS AN ERROR HERE
this->d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
this->d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
this->d3ddev->BeginScene();
// Draw objects
// Finish up
this->d3ddev->EndScene();
this->d3ddev->Present(NULL, NULL, NULL, NULL);
};
main.cpp
#include "system.h"
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) {
System MainSys;
MainSys = System(hInstance, lpCmdLine, nCmdShow);
// Enter the main loop
MSG msg;
while (TRUE)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (msg.message == WM_QUIT)
break;
MainSys.renderFrame();
}
// Clean up DirectX and the COM
//delete MainSys;
return msg.wParam;
};
I tried to compile your code and changed the following lines:
System MainSys;
MainSys = System(hInstance, lpCmdLine, nCmdShow);
into
System MainSys(hInstance, lpCmdLine, nCmdShow);
and it worked.
I admit I don't know exactly why it fails, but there's a lot of stuff in your System class and you are constructing two instances of it (once in both lines) and then assign one to the other. This is not a good idea in any case and unnecessary. I guess it has somethinig to do with Direct3D reference counting and you are not honoring it when the class gets copied to MainSys in the second line.
EDIT
I just noticed that the destructor of MainSys gets called after the class is constructed with System(hInstance, lpCmdLine, nCmdShow); So the just obtained device is Released() again.
I don't see anything imeediately wrong here but its easy to miss something.
I suggest checking the HRESULTS from every d3d api call to make sure something didn't fail, and assing assert(pointer != NULL) all over to try to catch the problem. Or step through with a debugger and look at the variables to determine when it starts to fail.
Also, if you've not done so, turn on the d3d9 debug version and look at the log. It often tells you what you've missed.
I have been trying to make a Global Keyboard hook program in visual C++ that write keystrokes in file "log.txt"..I am new to windows programming and i have gone through the msdn library to get an understanding of hooks....I think i have understood the concept theoretically but when i implement the code it doesn't work..The compiler doesn't show any error in both the DLL file and EXE file....Moreover the "Log.txt" file never gets created...
Here are the code files
First Dll file:
#include<windows.h>
#include<stdio.h>
HHOOK g_hhk;
__declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode>=0)
{
char ch;
FILE *fp;
fp=fopen("log.txt","a");
if((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
{
if(wParam==VK_RETURN)
ch='\n';
fwrite(&ch,1,1,fp);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan;
scan=0;
ToAscii(wParam,scan,ks,&w,0);
ch =char(w);
fwrite(&ch,1,1,fp); // copy character to log file
}
fclose(fp);
}
return CallNextHookEx(g_hhk, nCode, wParam, lParam);
}
Now the EXE file:
#include<windows.h>
HOOKPROC hkprckb;
static HINSTANCE hinstDLL;
static HHOOK hhookkb;
int WINAPI WinMain(HINSTANCE hInstance1,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
hinstDLL=LoadLibrary(TEXT("C:\\Documents and Settings\\Attar Singh\\My Documents\\Visual Studio 2008\\Projects\\key\\Debug\\key.dll"));
hkprckb=(HOOKPROC)GetProcAddress(hinstDLL,"KeyProc");
hhookkb=SetWindowsHookEx(
WH_KEYBOARD_LL,
hkprckb,
hinstDLL,
0);
MessageBox(NULL,NULL,NULL,MB_OK);
return 1;
}
This program is giving me nightmares...Any sort of help will be greatly appreciated...Thanks in Advance...
wParam is supposed to have one of the following values:
WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP.
To get the virtual key code of the key pressed you have to use: ((KBDLLHOOKSTRUCT*)lParam)->vkCode