Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to DLL's need a guiding hand

Status
Not open for further replies.

ADoozer

Programmer
Dec 15, 2002
3,487
AU
im writing my first ever DLL.
Its basically butchered together code from examples on the net.

KeyboardHook.h code
Code:
//#ifdef KEYHOOK_EXPORTS
#define KEYHOOK_API __declspec(dllexport)
//#else
//#define KEYHOOK_API __declspec(dllimport)
//#endif

KEYHOOK_API void HookApplication(HWND h);//This function installs the Keyboard hook.
KEYHOOK_API void ReleaseHook();//This function removes the previously installed hook.
KEYHOOK_API void UploadString(LPCTSTR SD, LPCTSTR KD);//upload string data from VB
KEYHOOK_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam);//This function is called when the keyboard is operated.

i had to comment the top section out because it wouldnt compile properly... not exactly sure why its there (like i said butchered code)

KeyBoardHook.cpp
Code:
// KeyboardHook.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "KeyboardHook.h"

#pragma data_seg(".HOOKDATA")//Shared data among all instances.
HHOOK hook = NULL;
HWND hwnd = NULL;
CHAR KickData[80];
CHAR SignalData[20];
#pragma data_seg()

#pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directive

HINSTANCE hinstance = NULL;

BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }

	hinstance = (HINSTANCE)hModule;
	hook = NULL;

    return TRUE;
}

//if my understanding is correct h will be the handle
//to the program i want to look at 
//(hWND returned by getwindow API in VB)
KEYHOOK_API void HookApplication(HWND h)
{
	hook = NULL;
	hwnd = h;
	hook = SetWindowsHookEx(WH_KEYBOARD,hookproc,hinstance,NULL);
	if(hook==NULL)
		MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
}

KEYHOOK_API void ReleaseHook()
{
	UnhookWindowsHookEx(hook);
}

KEYHOOK_API void UploadString(LPCTSTR SD, LPCTSTR KD)
{
        //assign SD to SignalData
        //assign KD to KickData
}

KEYHOOK_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
    //insert code to look for SignalData
    //when SignalData found replace with KickData
    //notify VB app that string replaced
	return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}

with a few comments here and there i managed to get this to compile with no errors and warnings (obviously not much is happening yet)

can anyone spot any redundant code, or anything missing. (i really dont want to try and use the dll and kill the PC)

thnx in advance

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top