Thursday 10 May 2012

How to solve DLL Hell


Welcome to dll hell.

Suggestion:
1) open the exe in VB, and remove the reference to the dll, close VB
2) open the dll in VB, set the project to no compatibility
3) build the dll
4) change the project to binary compatibility, and point to the newly built dll
5) open the exe, ans restore the reference.

Caveat: there is a bug in VB that prevent to release the dll once the application is run, so sometimes when you try to rebuild the dll you get permission denied. In that case, just reopen the project, that will release the dll

Tip: you can load both dll and exe projects in a group, to easily debug them together.

Saturday 5 May 2012

WHY USE AN ACTIVEX CONTROL?:



When you create an ActiveX control (OCX file), you are creating a TOOL.  This tool can be a custom control to do something that no standard control can do, or it can be a control that works exactly like a standard control works, but with slightly changed functionality, properties, events, and/or methods.  It can be whatever you want.
Let's say for example that you would like to create a PictureBox that loads and saves BMP, JPEG, and GIF pictures, as well as adds special effects to them when they are loaded.  No standard Visual Basic control can do this custom work.  You can get this functionality by putting in a HUGE amount of custom coding into your application, or by using my Advanced Bitmap Processing module (along with the JPEG module posted on this site), or something like that.  But why bother if the thing you're displaying the picture on (most often a PictureBox) can do that kind of work for you?  Creating a custom PictureBox that does this kind of custom functionality would take that kind of work off of your main application, and allow your code in your main application to be cleaner and simpler.
Another possible use for this tool would be to create a control that does things for you in order to take processing and code off your main application.  For example, you could create an ActiveX control that contains several smaller controls like a TreeView on the left, and a ListBox on the right which resizes them dynamically when the ActiveX control is resized.  It could also contain data validation for information being passed in to be displayed in the TreeView or ListBox.  This takes a LARGE amount of code off of our main application so that your main application's code is cleaner and simpler.
Another advantage to using ActiveX controls over just coding the functionality into your main application is the ActiveX control runs in its own thread.  If you are handling some intense processing, graphics work, database connectivity, data transferring, or sub-classing within your ActiveX Control and something goes wrong, the ActiveX will freeze instead of your entire application freezing up.  The ActiveX control DOES get loaded into the same memory space as your main application though, so it is possible for something to go wrong with your ActiveX control that takes down the rest of your program, but that is VERY unlikely.
Also, because ActiveX controls run in their own threads, you can create a "multi-threaded" application by embedding functionality into an ActiveX control and having them do your application's work.  For example, you could create a "Download Control" that simply goes to the FTP site you specify and downloads the file you specify to a destination file on your hard drive that you specify.  All you'd have to do to download multiple files simultaneously is create a control array (or several instances of that ActiveX control) on a form, set each one to download a different file to a different destination, and cut 'em all loose!  BOOM!  Multi-threading, baby!  Multiple downloads that are independent of each other.
But ActiveX controls are not just for use within Visual Basic, C++, Delphi, and other "COM aware" programming languages.  You can also use ActiveX controls over the internet!  It is possible to embed an ActiveX control right into a web page and then view it with Microsoft Internet Explorer 4.x or better.  
"But how do I embed an ActiveX control into my HTML or ASP web page?"
First of all, I need to be clear that ActiveX is a Microsoft technology and Netscape does not natively support it.  There are Netscape Plug-Ins that will allow you to use them within Netscape, but they obviously don't work as well as Microsoft Internet Explorer (which is designed to natively support them).
Now, let's take the "CustomButton" (CustmBtn.ocx) ActiveX control that I posted on this web site.  If you wanted to embed that into a web page, the HTML code would look something like this:
 
<
HTML>
<
HEAD>
<
TITLE>TestOCX</TITLE>

<
SCRIPT LANGUAGE="JavaScript">
  
// This function changes the caption to "Hello" and displays a MessageBox via JavaScript
  function 
ChangeCaption() {
    TestForm.CustomBtn.Caption = "Hello";
    alert ("Click OK when ready to change the caption again");
  }

</
SCRIPT>

<
SCRIPT LANGUAGE="VbScript">
  
' This function changes the caption to "Whasssup!" and displays a MessageBox via VbScript
  Sub 
ChangeBackColor
    TestForm.CustomBtn.Caption = "Whasssup!"
    
MsgBox "Caption was changed",vbOKOnly Or vbExclamation, " "
  End Sub
</
SCRIPT>

<
SCRIPT LANGUAGE="JavaScript" FOR="CustomBtn" EVENT="Click">
  
// This event fires when the ActiveX control is clicked and displays the About dialog
  
TestForm.CustomBtn.About();
</
SCRIPT>

</
HEAD>
<
BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<!-- The ActiveX control has to be within a form in order for it to be called via code //-->
<
FORM ID="TestForm">
  <
OBJECT ID="CustomBtn" CLASSID="CLSID:BB7BA40E-784E-11D4-AF87-0008C74B19A1">
    <
PARAM NAME="_ExtentX"   VALUE="2249">
    <
PARAM NAME="_ExtentY"   VALUE="714">
    <
PARAM NAME="Appearance" VALUE="0">
    <
PARAM NAME="BackColor"  VALUE="12632256">
    <
PARAM NAME="Caption"    VALUE="Testing">
    <
PARAM NAME="Enabled"    VALUE="-1">
    <
PARAM NAME="ForeColor"  VALUE="12632319">
    <
PARAM NAME="Style"      VALUE="0">
  </
OBJECT>
</
FORM>

<
SCRIPT LANGUAGE="JavaScript">
  
// Change the caption the first time via JavaScript
  
ChangeCaption();
</
SCRIPT>

<
SCRIPT LANGUAGE="VbScript">
  
' Change the caption the second time via VbScript
  Call 
ChangeBackColor
</
SCRIPT>

</
BODY>
</
HTML>
  
You'll notice a few things about this code.  First is that in order to use the ActiveX control within your page, it has to be embedded as an object.  Not only does it have to be embedded as an object, but it has to be embedded with an "ID" and within a form that also has an "ID".  The form it's embedded within doesn't have to do anything at all, but it has to be there, and it has to have an ID.  The reason for this is the embedded object can't stand alone in HTML just as a PictureBox control can't stand alone within Visual Basic without a Form, MDI Form, PropertyPage, UserControl, or UserDocument.  It has to be a part of a form within VB or HTML.  That is how the object is referenced in both cases...  by <FORM>.<CONTROL> where in this case, the form is "TestForm" and the control is "CustomBtn".  You'll notice that it is referenced in VbScript and JavaScript as such.  Again, VbScript is a Microsoft technology and isn't natively supported by Netscape (though I wish it was).
The second thing you may notice is that the ActiveX control's properties are initially set using the object's "PARAM" values, and later at run-time by referencing the <FORM>.<CONTROL>.<PROPERTY>.  That's easy enough, but what about Methods and Events?  Well, methods are called like functions from VbScript or JavaScript similar to properties... <FORM>.<CONTROL>.<METHOD>.  As far as events, those are a little bit more complex, but still fairly simple.  All you do is declare a JavaScript <SCRIPT> block and tack on "FOR" pointing to the name of the object that the event is for, and "EVENT" with the name of the event that the script block will be fired for represents.  In this case, the CLICK event is the one handled, and it calls the "About" method which shows the control's about message.
The third thing you may notice is that the ActiveX control is referenced not by location, but by "CLSID" (Class Identifier).  The CLSID is also referred to as the "GUID" (Globally Unique Identifier) or "UUID" (Universally Unique Identifier).
"But how will I know what the CLSID of my ActiveX control is?"
You'll be able to find out by looking in your Windows Registry via a tool called "Registry Editor" (REGEDIT.EXE).  Go to START > RUN and type "REGEDIT" and hit ENTER.  First locate the name of the interface you wish to reference by going to theHKEY_CLASSES_ROOT section and looking for the name of the project the class module interface you wish to reference is located.  This <PROJECT>.<CLASS> combination is refered to as the "ProgID" or "Program Identifier" and is used in calls toServer.CreateObject() in ASP, and CreateObject() in VB.  In this case, it's Custom_Button.CustomButton.  Under this registry key, you'll notice a registry key called "CLSID".  The default value for this key is the "CLSID" that you use to reference your ActiveX control as I did in the above code.
If you want to take this a step further and find out the location of the actual OCX or DLL file that is referenced by the CLSID, you can do that by going to the "CLSID" section under the HKEY_CLASSES_ROOT section and finding your CLSID... in this case {BB7BA40E-784E-11D4-AF87-0008C74B19A1}.  Once you find it, you'll notice there are several entries under it.  One of them is "InprocServer32".  The default value of this registry key will be the physical location of the file that is your ActiveX control.  In this case, CustmBtn.ocx.

WHAT IS AN ACTIVEX CONTROL?:


An ActiveX control is a COM object similar to a Standard VB Class Module or ActiveX DLL with properties, methods, and events.  The difference between an ActiveX control and an ActiveX DLL is that an ActiveX control has a user interface and requires you to put it on something (like a Form, MDI Form, Property Page, User Document, or User Control) so the user interface can be displayed.

A CommandButton, ListBox, PictureBox, Label, Timer, etc., are all ActiveX controls as well, only they are built into Visual Basic as native controls.  ActiveX Controls are the building blocks of applications, just as native types (like String, Integer, and Long) are the building blocks of custom variable Types.

Wednesday 2 May 2012

VB6 tips to quickly change controls property by just triple click



If you want to copy a number of properties from one control to one or more other controls, you can select the control you want to copy from, press Shift and select the other controls, press F4 to show the Properties window, and triple-click the name of the property you want to copy. Note that you must click the name of the property on the left, not the value cell on the right. The values of the properties on which you triple-click are copied from the source controls to all the other selected controls. This technique doesn't work with all the items in the Properties window.


Handy VB6 select control property quickly




There is one handy but undocumented technique for quickly selecting a given property of a control. You just have to select the control on the form and press the Ctrl+Shift+x key, where x is the first letter in the property's name. For instance, select a Label control, and then press Ctrl+Shift+C to display the Properties window and select the Caption property in one operation. Pressing the Ctrl+Shift+C key again moves the focus to the next property whose name begins with the C character, and so on in a cyclic fashion.

Visual Basic 6 tips to create multiple control at once


If you need to create multiple controls of the same type, you can follow this three-step procedure: First, click on the control's icon on the Toolbox window while you keep the Ctrl key pressed. Next, draw multiple controls by clicking the left button on the form's surface and then dragging the cursor. Finally, when you're finished creating controls, press the Escape key or click the Pointer icon in the upper left corner of the Toolbox.

Visual Basic 6 Strength

One of the greatest strengths of the Visual Basic language is that programmers can design an application and then test it without leaving the environment. But you should be aware that designing and testing a program are two completely different tasks. At design time, you create your forms and other visible objects, set their properties, and write code in their event procedures. Conversely, at run time you monitor the effects of your programming efforts: What you see on your screen is, more or less, what your end users will see. At run time, you can't invoke the form designer, and you have only a limited ability to modify the code you have written at design time. For instance, you can modify existing statements and add new ones, but you can't add new procedures, forms, or controls. On the other hand, at run time you can use some diagnostic tools that aren't available at design time because they would make no sense in that context (for example, the Locals, the Watches, and the Call Stack windows