Results 1 to 2 of 2

Thread: what's wrong with this code?

  1. #1
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,469

    what's wrong with this code?

    this is a c++ example that i found on the internet + i'm trying to modify. it compiles successfully but when i try to use the functions in a vb.net application it causes a System.EntryPointNotFoundException error:

    Code:
    // hookDll.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    
    #pragma data_seg("Shared")		//these variables will be shared among all processes to which this dll is linked
    HHOOK hkKey = NULL;				
    HINSTANCE hInstHookDll=NULL;
    HWND parentHWnd = NULL;
    #pragma data_seg()
    
    #pragma comment(linker,"/section:Shared,rws")
    
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
    	switch(ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    		hInstHookDll = (HINSTANCE)hModule;		//we initialize our variable with the value that is passed to us
    		break;
    	}
        return TRUE;
    }
    
    LRESULT CALLBACK procCharMsg(int nCode,WPARAM wParam, LPARAM lParam)		//this is the hook procedure
    {
    	MSG *msg;																//a pointer to hold the MSG structure that is passed as lParam
    	if(nCode >=0 && nCode == HC_ACTION)										//if nCode is less than 0 or nCode is not HC_ACTION we will call CallNextHookEx
    	{
    		msg=(MSG *)lParam;													//lParam contains pointer to MSG structure.
    		if(msg->message==WM_CTLCOLOREDIT)									//we handle only WM_CTLCOLOREDIT messages
    		{
    			SendMessage(parentHWnd,WM_CTLCOLOREDIT,0,0);					//passing this message to parent application
    		}
    	}
    	return CallNextHookEx(hkKey,nCode,wParam,lParam);						
    }
    
    //set the hook
    //how can i change this to a function that returns hWnd if it hooks successfully?
    HWND __stdcall SetHook(HWND hWnd)
    {
    	if(hkKey == NULL)
    		hkKey = SetWindowsHookEx(WH_GETMESSAGE,procCharMsg,hInstHookDll,0);
    	    parentHWnd = hWnd;
    		return parentHWnd;
    }
    
    //remove the hook
    void __stdcall RemoveHook()
    {
    	if(hkKey !=NULL)
    		UnhookWindowsHookEx(hkKey);
    	    hkKey = NULL;
    }
    this is the .def file:

    Code:
    EXPORTS
    SetHook @2
    RemoveHook @3
    + this is how i'm declaring them in vb.net:

    vb Code:
    1. Declare Function SetHook Lib "hookDll.dll" (ByVal hWnd As IntPtr) As IntPtr
    2. Declare Sub RemoveHook Lib "hookDll.dll" ()

    what am i doing wrong?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,653

    Re: what's wrong with this code?

    Have you tried using the DLLImport method instead? - http://msdn.microsoft.com/en-us/libr...attribute.aspx

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •