Professional Documents
Culture Documents
just what the heck is an activex exe? how do i use it and more importantly, why?
ok, if an activex exe is so similar, why would i choose to use an activex exe over
an activex dll you may ask? good question but let me explain how an activex dll
works first, before i tell you about an activex exe.
an activex dll is mapped into the address space of the client application and
becomes part of client�s process; the activex dll does not spawn its own thread.
this means, in versions prior to vb.net any way, the thread of the client
application must yield processing and give that thread to the activex dll to use.
this gives you synchronous processing; the calling application can not continue
until the activex dll returns from the method call. this is a form of apartment
threading; the dll is not single threaded, that would mean all of the classes
given out to clients would share the same thread, in apartment threading, each
class is dependant on the thread given to it by the client application and all of
those threads work independently.
an activex exe on the other hand creates a new thread for each new class that it
creates! each thread is independent of the client application. hold on their
guru, are you talking about multi-threading in a vb application??? your damn
straight, you can create a fully multi-threaded application by just creating
classes from an activex exe. so, you can create an asynchronous call, meaning
that the client application can continue processing while the activex exe is
processing too. just be sure to notify the client application that processing has
either finished or has an error. the best way to do this is to create an event in
the activex exe, then when creating the activex exe class, declare it with the
�with events� keyword.
ok, sounds great, but when am i ever going to use it? as scotty on star trek
would say, �the right tool for the right job�, an activex exe is a tool and there
is specific reasons when and why you would use it. the number one reason not to
use it is when you will be transferring large amounts of data between the client
and com component. remember, and activex exe is running out side of the clients
processes, meaning, they can not pass data directly between the two, the operating
system must �marshall� the data back and forth, this creates a tremendous amount
of over head and can significantly slow down the processing time; not a good idea.
there is also a �gotcha� involved in an activex exe, they do not always work as
stated, yes, the class does have its own thread and yes that thread is independent
from the client application, but in vb anyway, the vb main thread does have a
tendency to yield, just like a dll. the work around that i have used in the past
is to include the �timer lite� dll that you will find in our code section. i have
the client call the main method, that method starts a timer for less then one
second and returns to the client application. when the timer fires, i then call
an internal (private) method that will execute the code the client thought was
going to process. i know this sounds confusing, but it will all come together
when we do the sample.
so, if passing data back and forth is very light and you need to process
something independently of the client application, or you what something to run
strictly firing it�s own events with minimal interaction with the client
application, an activex exe could be your savior.
let me give you an example of when i have used an activex exe in the past.
i needed to create an ivr (interactive voice response, you know, press 1 for this,
press 2 for that) system. it was to have 96 lines coming in and each line must
run independently, it must have a �master control� application that controls the
whole thing, for example, start and stop the system and monitor what is going on
in the system, detect �dead� lines, clear them and re-start that line again.
ok, i need 96 instances of something that work independently of each other and
independently of the client application. the thing i need to create will have
minimal communications with the client except for sending up a few messages to let
it know what is going on. hmmmmm�..sounds like a job for an activex exe to me,
what do you think?
the final product worked like this. a vb based application would start, it would
check the ivr board to see how many lines are available. it would then create
that many instances of a class in an activex exe (in this case, 96), it would then
pass a line to the class to monitor; the vb app then just sat back and waited.
the exe class would handle it�s own events, for example �line ring�, and would
process the call, it would send up a message to the vb app via an event to let the
vb app know the line was being used and when it was finished with a call.
we now need to add an event to our class so we can notify our client application
when processing has been completed. we also need to add a timer and a var to hold
the value passed to the main sub to our class, paste in the following code in the
top you clsmain:
option explicit
'********************************************************************************
'* synop: this is a demo to show how to create and use an activex exe *
'* *
'********************************************************************************
we now need to add the code for the client application to call, paste in the
following code:
'********************************************************************************
'* args: *
'* synop: for demonstration purposes, we are going to simulate this exe *
'* said in the tutorial, we need to create a timer here too so the *
'* sub will return right away and the timer will call the function *
'* we want. *
'* *
'********************************************************************************
with proctimer
.interval = 500
.enabled = true
end with
lloops = numberofloops
end sub
as you can see, we are setting the timer and setting the class level var to hold
the value the client is passing.
lets now add the code for when the timer fires, this code will call our hidden
internal code to do the job the client wants.
with proctimer
.interval = 0
.enabled = false
end with
intmakemeprocess lloops ' when the timer fires, call the internal function
that the user thought they were calling
end sub
'********************************************************************************
'* args: *
'* synop: as you can see, this sub is private and can only be called from *
'* within this class. it is only called when the proctimer fires *
'* and passes the same value as makemeprocess(). this allows the *
'* *
'********************************************************************************
' do some processing, in this case we are just going to loop to simulate some
lengthy process.
doevents
next
raiseevent finished("all done") ' let the client know we are done
end sub
end sub
that is it, just compile your exe, then right click the project to bring up the
properties, click on the component tab and select �binary compatibility� to make
sure every time you compile it will still work with your client applications.
click the run button and we are all set to create our client.
open a new instance of vb, no, you can not do it like an activex dll and create a
project group, remember, an exe runs in its own process and you must debug it the
same way, in its own vb instance.
select standard exe as the project template, you should now have a project called
project1 and one form called form1, these are fine, you do not need to change the
names. add a reference to �myfirstexe� in the project -> references menu, you
also need to add a button to your form.
we now need to declare an instance of our activex exe class, remember, this class
has events that we want to fire so we must declare it as such. paste the
following code into the declaration section (the top) of your form.
option explicit
now we need to initialize the object, paste this code in the form load event:
set myexe = new myfirstexe.clsmain
msgbox message
we now need code to make it all come together, paste this code into your
command_click event:
myexe.makemeprocess (1000) ' make the activex simulate processing by doing 1000
loops
end sub
now, run the standard exe, press the command button and within a second or two,
you should have a message box pop up telling you the exe has finished.
to prove that the exe is running on it�s own thread, set the number you pass to
the exe well past the 1000 number i have set as the default, say 10 or 20k. while
you are waiting for the message box, click on the title bar of your standard exe
and move the window around. if this was an activex dll, you would not be allowed
to do this.
you can set break points in your activex exe and step from your standard exe right
into the activex exe, just like it was in a project group.
i hope this helps you out and you at least have a basic understanding on what an
activex exe is and how it differentiates from an activex dll.
you can download the complete project here, but you will still need to download
the timerlite dll from the code section. if you download the code, do not forget
to compile or double click on the activex exe to register it before you try to run
the standard exe, or else the standard exe will not be able to find the active x
exe.