CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Location
    srilanka
    Posts
    88

    Calling a vc++ dll from vb

    Sir i want to creat a Vc++ system wide hook dll.{{Whatever hook articles available on the internet are intended for advanced developers who know thing or 2 in vc++.}}But students like me who are new to vc++ find it difficult to grasp some ideas.
    I want a basic level explanation from some experts who really wants to teach those things to their students.
    The hooks i am intended to create is Wh_Getmessage and i have to call that dll from vb6
    Say my objective of this hook is say if i a user type A then i have to change to B for any application running.

    I am a stranger to vc++ so i need help in how to create this {{system wide}} wh_getmessge hook

    1.In vc++ in new what dll i have to chose ]
    a.MFC appwizard Dll
    b.WIn32 Dynamic link library
    What is the difference between these 2 dlls.?
    2.What is an header file and what are the things we have to declare here and why we have to declare??

    3.How functions in vc++ dll should be declared inorder to call those functions from vb.?

    4.Say i am calling SetHook funciton from vb which is in dll then hook is set.
    MsgFilterHook = SetWindowsHookEx( WH_GETMESSAGE,(HOOKPROC)MsgFilterFunc, dll_hInstance, threadID );
    I am having the hookhandle in MsgFilterHook which i can use to unhook this hook.
    Threadid = 0 to create a system wide hook
    MsgFilterFunc is my callback function
    My doubt is once hook is set , will the system automatically call my callback filter function for any application which process getmessage
    Say i have notepad and i am typing A and in my call back function i will get a wm_char = A.NOw if i change to another application say wordpad and type B
    then will it be possible to get the wm_char=B message in my callback function.Please Explain me how does this work.

    5.Say n no of editor applications are running and i want to disable the character T from going to any application and i have to make T into H how to do this in my callbackfunciton.Issue in this question is how am going to get these hook work for any application which is running.??

    Issues
    1.How to start creating a dll in vc++6
    2.Header file decleration ideas.
    3.Export functions declerations.
    4.Explanations of how these hook work on a systemwide basis(i understood how dll maps the memory of other applications but wanted codings point of view like what are the parameters come to callback function each time a new application is actvated.)
    5.What is #pragma and shared data segment ?How variables should be decalred in the hook dll and how it should be accessed??Please explain at a lower level how this things are implemented why is the need?

    Thanks in advance.

  2. #2
    Join Date
    Dec 2002
    Location
    tennessee
    Posts
    104

    re:

    Hi, I had the same problem starting out until i found a book called
    Visual basic black book which is a very good book btw.

    Snce your use to working with vc++, I've attached a program to
    this message that's my own written program that will write your
    dll files for you. The .h file, the .cpp file, and the .def file. All you have to do is load the files into vc++.

    Like this: File, New, and select the win32 dynamic link library.
    Name the dll what ever you want and then goto next or what ever, and create an empty project. When the project is showing
    the file tabs and stuff on the left hand side click on the file tab.

    Next right click on the source folder and add your .cpp file.
    Do the same for the header folder for the .h file and the .def file in the resource folder.

    BTW... after you use my program those files should be located on your c:\newdll path. Don't worry the program will make this folder
    for you, that's how i know they'll be there.

    Ok, then just add your code inside the{} brackets and build your dll and that's all. However I know you wanna see and example so here's one!!!!

    ====================
    C side: this is the .cpp file:
    ====================

    #include <afxwin.h>
    #include "dynam.h"


    extern "C"
    int PASCAL EXPORT addem (int value1, int value2){

    return value1 + value2;
    }

    class Cdynam : public CWinApp
    {
    public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();

    Cdynam(LPCTSTR pszAppName) : CWinApp(pszAppName){}
    };

    BOOL Cdynam::InitInstance(){
    SetDialogBkColor();

    return TRUE;
    }

    int Cdynam::ExitInstance(){
    return CWinApp::ExitInstance();
    }

    Cdynam dynam("dynam.dll");


    ============
    This is the .h file:
    ============

    extern "C" {
    int PASCAL EXPORT addem(int value1, int value2);

    }

    =============
    This is the .def file:
    =============

    LIBRARY dynam

    EXPORTS
    addem


    ========
    On vb side:
    =========

    General

    Private Declare Function addem lib "dynam" (ByVal value1 as integer, ByVal value2 as Integer) as Integer

    Private Sub Command1_Click()

    text1.text = "2+3 = " & str(addem(2,3))

    end sub

    That should put you on your way! Hope this will help
    Attached Files Attached Files

  3. #3
    Join Date
    Dec 2002
    Location
    tennessee
    Posts
    104
    I forgot to say after you build your dll you have to copy the dll to the windows system folder because that's where vb will look for it.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured