Results 1 to 4 of 4

Thread: VC++ & VB Inter Process Communication Plus Introduction To VB Subclassing

Threaded View

  1. #1

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    Lightbulb VC++ & VB Inter Process Communication Plus Introduction To VB Subclassing

    Hi everybody. This is the article I've been working on about Inter Process Communication. I know the rule here is not to put compiled code in the attachments, but I'm really in a rush and trust me, the last thing I'm gonna do is spread viruses. The code is clean, well commented, the archive is clean too and without any viruses and you can remove the EXE and recompile if you don't trust me. Now I will format the article according to this forum's rules. You also have it in a nicely formatted Word document within the archive. I hope you like it. Thank you for all your help.

    Introduction
    This tutorial will walk you through the mechanisms of non DDE inter process communication. Inter process communication is something that may become vital to the future of your application. I chose for this article to achieve communication between two very different (and popular) programming languages: Visual C++ (version 6 to be more specific) and Visual Basic (again, version 6). The contents of this article will however probably work with no or little modifications in Visual C++ .NET and Visual Basic .NET too.

    We can use the term of inter process communication not only for two applications but also for an application and a DLL, which is exactly the topic covered here. Even more, this article will also be useful in a situation when you need to link two applications. Since we will use API and callbacks, there won’t be too much of a headache for the reader to also understand how to achieve communication between two applications or two DLLs or more complex arrangements.

    Note that even if you already know how to communicate between two or more processes, this article may still help you in a better understanding of this concept and may present new ideas and tricks. Beginners or programmers who aren’t so familiar with the subject will definitely find a lot of interesting things here.

    General Purpose
    My personal battle with inter process communication was caused by the need to combine the RAD (Rapid Application Development) power of Visual Basic with the efficiency of Visual C++. It is well known that Visual Basic 6 is unable to build true DLLs (only builds COM). Unfortunately, when I started one of my big projects, I was unaware of such a thing and I only discovered that when it was too late to port everything to Visual C++ (I would also lose the RAD and ease with which VB handles ActiveX). Inter process communication saved me from a very dangerous position where I would be forced to waste months porting to Visual C++ or just give up on one of my project’s most important functionalities!

    This is what this kind of communication is very good for: combining the advantages of two or even more programming languages through the use of Windows API and DLL exported functions. In my case, take the RAD from Visual Basic and the power of Visual C++, to reach the best results.

    Article Code And How To Use It
    The code consists of a Visual Basic 6 test application and a Visual C++ 6 test DLL. Simply download and extract the Zip archive at a convenient location and after that run the Visual Basic executable provided. The code for both the test application and the DLL is very well commented! I wrote it this way so that anybody can understand it.

    Zip Archive Content
    - Full source code and demo projects for the Visual Basic 6 test application and the Visual C++ 6 test DLL.
    - Diagrams (can be displayed directly via the TestVBApp.exe) to help the reader for a faster and better understanding of the code and of the processes involved, plus this article in a nicely formatted Word document.

    Coverage
    This article focuses only on a Visual Basic application and a Visual C++ DLL because it’s a situation which will cover all the required areas of inter process communication without becoming too complicated. If your personal project structure is different, the APIs and techniques detailed here will help you none-the-less. If you only work in Visual Basic, things will be even easier because Visual Basic allows easy connection of components. This article will handle only the trickier situations, things you might consider as “hard”.

    Newbies to programming techniques such as subclassing will also find this article highly educative, since I will be using subclassing in Visual Basic to achieve some little tricks which I discovered while pulling my hair out in front of the computer during late nights when I tried to link it with my Visual C++ work.

    Recommended Pre-Requisites
    - Have Microsoft Visual C++ 6 or newer and Microsoft Visual Basic 6 or newer installed on your computer.
    - Intermediate level Visual C++.
    - Intermediate level Visual Basic 6.
    - Windows Operating System (should work on any version).

    Warning
    Although I checked my code, comments and article a lot of times, I am not a guru of computer programming. Don’t expect my code to be perfect. Maybe there are some mistakes in the comments too. However, trust me that I did my best to provide you with a well written, well coded article with as few mistakes as my skills at this time made possible. Please forgive me for any silly mistakes that I might have done, and if you have the time, drop me an e-mail (axonnus at yahoo dot com) with any opinions or advices you might have. We’re all here together to improve each other and that’s exactly what I am trying to do. I do enjoy constructive criticism!

    Thanks To...
    Before finally getting to work, I would like to thank the entire internet community for helping me so much in my evolution as a computer programmer. Also thanks to Nicholas Skapura for an article that gave me quite a lot of technical insight into the works of communication between VB and VC++ and Randor for fixing the callback C++-VB array issue. Of course, my deepest “Thank you” message for the people helping on the Visual Basic Forums message boards, especially for those who answered my questions : ). They were there for me countless times and almost always gave me good ideas and helped me solve problems. The same message also stands for the other forums all around the internet and people who sacrifice their own free time and keystrokes for the good of others. You all have my respect and I hope that I will help too, through my articles and software, with a tiny bit of knowledge to everybody. Thank you all.

    Why VB?!
    Good question. Even though Pascal was my first programming language, VB is where I spent a lot more time than in any other (although C++ is catching up fast). A lot of people underestimate VB. That maybe because they either consider it “too un-elite” or simply don’t know how to use it. The truth is that indeed VB is unable to do some things, but it has other advantages. Big advantages. For example, working with ActiveX or developing User Interfaces. VB means Rapid Application Development and it sure can make your life easier.

    Example Architecture
    The layout of the example is quite simple. A Visual Basic application is used to work with a VC++ DLL. The VB application calls various methods from the VC++ DLL and the DLL answers with the appropriate actions.

    Functions like Activate and Demonstrate will not expect any result from the DLL. Demonstrate will actually trigger the DLL to perform a certain callback to a VB function or some other type of demonstration.

    VB Code:
    1. Private Sub cmdStringDemo_Click()
    2.    Demonstrate 1
    3.    'Calling the Demonstrate function from
    4.    'the VC++ DLL with 2 as parameter. 2 indicates that
    5.    'the string demo has to be started.
    6. End Sub

    The Demonstrate function in the VC++ DLL will launch a particular demonstration depending on the integer parameter sent to it.

    Code:
    //The VB Application will call this function 
    //to trigger the DLL to begin a particular demonstration.
    void WINAPI Demonstrate (int WhatToDemonstrate)
    {
      switch (WhatToDemonstrate)
      //Depending on what demonstration has been commanded.
      {
          case 1: //Demo 1 = Demonstrate calling a string function from VB.
          {
            //Calling a VB function that accepts a string as parameter.
            CallVBFunctionForString("Greetings from VC++!");
            break;
          }
          ...
      }
    }
    Functions like GetDLLData, VCNumericArray or VCStringArray will interact with the DLL using simple variables, arrays or structures.

    Subclassing
    An important part of the example relies on subclassing. Subclassing is used to get in full control of the messages that the VB application receives from the Operating System. The VC++ DLL will use WM_SETTEXT and WM_USER + 241 (241 being an arbitrary value that I chose) to send string data to VB. Or WM_COPYDATA to send numbers, strings or string arrays to VB.

    In Visual Basic, subclassing is quite easy to achieve, provided that you have the proper API functions and constants declared. Subclassing is a very powerful tool and it allows the programmer extensive control over a Visual Basic application. Without such a tool, it would be impossible to catch and react on messages as the ones I just mentioned above.

    This particular area of the example is very well explained through the comments in the code and the reader should find it quite easy to understand.
    Attached Files Attached Files
    Last edited by Axonn; Nov 22nd, 2005 at 08:39 AM.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

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