Click here to Skip to main content
15,889,600 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralA program within a program Pin
Ubik K7-Mar-05 23:36
Ubik K7-Mar-05 23:36 
GeneralRe: A program within a program Pin
BlackDice8-Mar-05 2:38
BlackDice8-Mar-05 2:38 
GeneralRe: A program within a program Pin
u6ik8-Mar-05 3:00
u6ik8-Mar-05 3:00 
GeneralRe: A program within a program Pin
Phil J Pearson8-Mar-05 4:47
Phil J Pearson8-Mar-05 4:47 
GeneralRe: A program within a program Pin
David Crow8-Mar-05 3:56
David Crow8-Mar-05 3:56 
GeneralRe: A program within a program Pin
u6ik8-Mar-05 4:12
u6ik8-Mar-05 4:12 
GeneralRe: A program within a program Pin
David Crow8-Mar-05 4:41
David Crow8-Mar-05 4:41 
QuestionData types mismatches or just incompatibility? Pin
Axonn Echysttas7-Mar-05 23:18
Axonn Echysttas7-Mar-05 23:18 
Hi... I got a weird problem here : ). I'm made a VC++ 6 DLL (Simple Win32 DLL Project). It's a DLL that installs a system-wide hook for the keyboard. I used keyboard because it's quite easy, my final purpose is to monitor messages with WH_GETMESSAGE. Anyhow, even this doesn't work.

My main Application is a VB6 Application. I will call the DLL from there. As you know, in order for a hook to work, you have to give it an address for a callback function which the hook will call each time it catches a keyboard event or message. The trick in my DLL is that the callback ain't here. I pass to the DLL (from VB) a parameter obtained from VB with the AddressOf operator. The parameter is the address of a VB function. Practically, I`m setting up the hook in VC++, but the callback function is in VB : ). I use "AddressOfCallBackFunction" parameter below to achieve this.
// ASDClock.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "winuser.h"

HANDLE hInstance;
HHOOK hhookHooks;
const int WH_KEYBOARD = 2;

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{

	hInstance = hModule;
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}

    return TRUE;
}

int HookSystem(int AddressOfCallBackFunction);
int UnHookSystem(int HookID);

int HookSystem(int AddressOfCallBackFunction)
{
	hhookHooks = SetWindowsHookEx(WH_KEYBOARD, 
		                          (HOOKPROC) AddressOfCallBackFunction,
								  (HINSTANCE) hInstance,
								  0);
	return (int)hhookHooks;
}

int UnHookSystem(int HookID)
{
	int iResult = 0;
	iResult = UnhookWindowsHookEx((HHOOK)HookID);
	if (iResult == 0) return 0; else return 1;
}


The VB code is simple and short.

In a module I got this...

Public Declare Function HookSystem Lib "D:\Lucru\Test\ASDClock.dll" (ByVal AddressOfTheCallBackFunction As Long) As Long
Public Declare Function UnHookSystem Lib "D:\Lucru\Test\ASDClock.dll" (ByVal HookID As Long) As Long

Public Function KeyboardCallback(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   MsgBox Code
End Function


And in the Form I got this.
Dim test As Long
test = HookSystem(AddressOf KeyboardCallback)


Surprise surprise! When VB reaches test = HookSystem(AddressOf KeyboardCallback) guess what? Error : BAD DLL CALLING CONVENTION. However, great must be your amazement when you hear that the hook IS ACTUALLY INSTALLED. The Application crashes, I must stop it, but the hook is succesfully installed.

As I learned, the error I just wrote above occurs when there are data type inconsistencies between VB and the DLL (or wrong number of parameters). Weird, because this is not the case. Long in VB = int in C++. The number of parameters is correct too.... Passing the parameters by ByVal is also correct. So anybody know what could be the problem?? Because I sure don't know. I've been using VB for quite some time...

The .DEF file for the VC++ DLL is :

LIBRARY ASDClock
DESCRIPTION "Testing some hook."
EXPORTS
HookSystem @1
UnHookSystem @2


Hm... I'm in the dark here : ).

-= E C H Y S T T A S =-
The Greater Mind Balance
AnswerRe: Data types mismatches or just incompatibility? Pin
Cedric Moonen8-Mar-05 0:02
Cedric Moonen8-Mar-05 0:02 
GeneralRe: Data types mismatches or just incompatibility? Pin
Axonn Echysttas8-Mar-05 3:16
Axonn Echysttas8-Mar-05 3:16 
Generalscreen capture driver Pin
TomLismont7-Mar-05 23:06
TomLismont7-Mar-05 23:06 
General3D border frame Pin
Joris van der Pol7-Mar-05 23:01
Joris van der Pol7-Mar-05 23:01 
GeneralRe: 3D border frame Pin
namaskaaram8-Mar-05 0:07
namaskaaram8-Mar-05 0:07 
GeneralRe: 3D border frame Pin
namaskaaram8-Mar-05 0:13
namaskaaram8-Mar-05 0:13 
GeneralRe: 3D border frame Pin
Joris van der Pol13-Mar-05 1:58
Joris van der Pol13-Mar-05 1:58 
Generalchange mouse cursor at run time Pin
Anand for every one7-Mar-05 22:41
Anand for every one7-Mar-05 22:41 
GeneralRe: change mouse cursor at run time Pin
namaskaaram7-Mar-05 22:56
namaskaaram7-Mar-05 22:56 
QuestionHow to handle Close(X) button on Property Sheet Pin
Duy Nghia7-Mar-05 22:24
Duy Nghia7-Mar-05 22:24 
AnswerRe: How to handle Close(X) button on Property Sheet Pin
ThatsAlok7-Mar-05 23:03
ThatsAlok7-Mar-05 23:03 
GeneralFile needed! Pin
Larsson7-Mar-05 22:15
Larsson7-Mar-05 22:15 
GeneralRe: File needed! Pin
Ravi Bhavnani8-Mar-05 2:53
professionalRavi Bhavnani8-Mar-05 2:53 
GeneralRe: File needed! Pin
Larsson8-Mar-05 9:32
Larsson8-Mar-05 9:32 
GeneralRe: File needed! Pin
Ravi Bhavnani8-Mar-05 9:36
professionalRavi Bhavnani8-Mar-05 9:36 
Questionwhat if classes need each other? Pin
ThinkingPrometheus7-Mar-05 22:01
ThinkingPrometheus7-Mar-05 22:01 
AnswerRe: what if classes need each other? Pin
Steen Krogsgaard7-Mar-05 22:42
Steen Krogsgaard7-Mar-05 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.