another event question

another event question

Anonymous
Not applicable
515 Views
6 Replies
Message 1 of 7

another event question

Anonymous
Not applicable
from the help file:
Private Sub ACADApp_BeginFileDrop(ByVal FileName As String, Cancel As
Boolean)
' This example intercepts an Application BeginFileDrop event.
'
' This event is triggered when a drawing file is dragged into AutoCAD.
'
' To trigger this example event:
' 1) Make sure to run the example that initializes
' the public variable (named ACADApp) linked to this event.
'
' 2) Drag an AutoCAD drawing file into the AutoCAD application from
either
' the Windows Desktop or Windows Explorer
' Use the "Cancel" variable to stop the loading of the dragged file, and
the "FileName"
' variable to notifiy the user which file is about to be dragged in.
If MsgBox("AutoCAD is about to load " & FileName & vbCrLf & "Do you want
to continue loading this file?", vbYesNoCancel + vbQuestion) <> vbYes Then
Cancel = True
End If
End Sub

My question is this:
As I understand it at this point(which is not very much), assuming all
required subs and classes exist and are initialized or whatever is needed
for this sub to run, at the moment the user drags a file into acad window,
the acad application is going to 'call' this sub. The acad application is
going to pass two arguments to this sub, the first being a byval copy of the
file name and then the second being a variable named "Cancel".
Since the Cancel arg is byRef, when you set it to true in the sub it's
original value is changed.

question 1: So that means that in the context of this sub, Cancel is a
global variable that the acadapplication created behind the scenes when you
created an acadapp variable using the with events keyword. Is that correct?

Question 2: Then after you set the variable to true here in this sub, after
this sub has run apparently the acad application is waiting in the
background with baited breath, waiting to see what you set the value of this
second argument to, and then it uses that value in some invisible sub that
we don't see here and runs that sub (the continuewithfiledrop activity in
this case) and uses that global variable "cancel" as an argument in some
further activity it is considering (such as opening the dragged file). Is
that what's going on here?
So the creation and declaration of the variable Cancel is just something
built into autocad? and when an app is created with events then all of this
mysterious stuff is prepared in the background and used according to however
the events are coded into the autocad application.
I guess this seems like a stupid obvious question but since I don't
understand exactly what's going on in the background and which is not
mentioned in any documentation I have found, I'm having trouble figuring out
by logic alone how to set up events and use them and a lot of time is going
into reading the help and trying to read between the few lines and trying
trial and error trying to get anything beyond the very basic examples to
work.
anyway, as always the wisdom and insight of those of you who "KNOW" all this
stuff is greatly appreciated!!!
Mark
0 Likes
516 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Pfff, a lot of questions. Because I'm not a member of the Autotodeks
programmers team, I don't know when all these ByRefs and variables are
created. Maybe they're always there and used by Acad itself, maybe they're
just there in VBA. I think they're always there and that autocad uses them
itself as well. For example when opening a read-only file, it aks the user
if you want to open it anyway. In other words, you can cancel the open
procedure. Besides, I don't even want to know when they are created and
why... Why should I? This cancel option is great! Because it's available in
a lot of events. You can even prevent acad form closing when the user wants
it. There is an event (I'm not sure of the name rigt now, I think it is
BeginQuit or something like that) which holds a cancel boolean as well. If
you set this to true, the application just won't quit! BTW, it's not stupid
question, it took me quiet some time before I figured your questions out as
well. If people think it's stupid then either we both are stupid or they
are! 🙂

Regards,
Joeri Tuyn



"MP" wrote in message
news:C09E8CEE4D34AA1FA337B15EA6966FDF@in.WebX.maYIadrTaRb...
> from the help file:
> Private Sub ACADApp_BeginFileDrop(ByVal FileName As String, Cancel As
> Boolean)
> ' This example intercepts an Application BeginFileDrop event.
> '
> ' This event is triggered when a drawing file is dragged into AutoCAD.
> '
> ' To trigger this example event:
> ' 1) Make sure to run the example that initializes
> ' the public variable (named ACADApp) linked to this event.
> '
> ' 2) Drag an AutoCAD drawing file into the AutoCAD application
from
> either
> ' the Windows Desktop or Windows Explorer
> ' Use the "Cancel" variable to stop the loading of the dragged file,
and
> the "FileName"
> ' variable to notifiy the user which file is about to be dragged in.
> If MsgBox("AutoCAD is about to load " & FileName & vbCrLf & "Do you
want
> to continue loading this file?", vbYesNoCancel + vbQuestion) <> vbYes Then
> Cancel = True
> End If
> End Sub
>
> My question is this:
> As I understand it at this point(which is not very much), assuming all
> required subs and classes exist and are initialized or whatever is needed
> for this sub to run, at the moment the user drags a file into acad window,
> the acad application is going to 'call' this sub. The acad application is
> going to pass two arguments to this sub, the first being a byval copy of
the
> file name and then the second being a variable named "Cancel".
> Since the Cancel arg is byRef, when you set it to true in the sub it's
> original value is changed.
>
> question 1: So that means that in the context of this sub, Cancel is a
> global variable that the acadapplication created behind the scenes when
you
> created an acadapp variable using the with events keyword. Is that
correct?
>
> Question 2: Then after you set the variable to true here in this sub,
after
> this sub has run apparently the acad application is waiting in the
> background with baited breath, waiting to see what you set the value of
this
> second argument to, and then it uses that value in some invisible sub that
> we don't see here and runs that sub (the continuewithfiledrop activity in
> this case) and uses that global variable "cancel" as an argument in some
> further activity it is considering (such as opening the dragged file). Is
> that what's going on here?
> So the creation and declaration of the variable Cancel is just something
> built into autocad? and when an app is created with events then all of
this
> mysterious stuff is prepared in the background and used according to
however
> the events are coded into the autocad application.
> I guess this seems like a stupid obvious question but since I don't
> understand exactly what's going on in the background and which is not
> mentioned in any documentation I have found, I'm having trouble figuring
out
> by logic alone how to set up events and use them and a lot of time is
going
> into reading the help and trying to read between the few lines and trying
> trial and error trying to get anything beyond the very basic examples to
> work.
> anyway, as always the wisdom and insight of those of you who "KNOW" all
this
> stuff is greatly appreciated!!!
> Mark
>
>
0 Likes
Message 3 of 7

Anonymous
Not applicable
Joeri Tuyn wrote in message
news:D07D3CAEFAEDA0E1457A1DABEB2F3B16@in.WebX.maYIadrTaRb...
> Pfff, a lot of questions.
> BTW, it's not stupid
>

Joeri,

thanks for the info and encouragement. This learning curve is fun but can
get kind of overwhelming at times.
What you confirmed is basically what I was guessing but thats about as far
as I seem to get on most this stuff, just guess and try and guess
again....thank everyone for this ng - it's the life saver!!! especially
knowlegeable and generous people like yourself who spend so much time
helping others...and here I am just taking taking taking....so thanks thanks
thanks!
And kudos to the ng upgrade - fast download!

Mark
0 Likes
Message 4 of 7

Anonymous
Not applicable
MP had this to say:

> question 1: So that means that in the context of this sub, Cancel
> is a global variable that the acadapplication created behind the
> scenes when you created an acadapp variable using the with events
> keyword. Is that correct?

Not really. When you declare an object WithEvents, you are signing up
for event notification (you're sinking events). The way COM triggers
events is a little ham-handed.

Your object's events are actually just methods whose signatures happen
to match those offered by the interface you're connected to. The
addresses of your object's methods get passed to the interface. Later,
when an event is triggered in the interface, it loops through the
addresses of all registered "listeners" and invokes their event
method.

Now that I've taken the long way around, the answer to your question
is "no, it's not a global variable". It is in fact a parameter of the
BeginFileDrop method

> Question 2: Then after you set the variable to true here in this
> sub, after this sub has run apparently the acad application is
> waiting in the background with baited breath, waiting to see what
> you set the value of this second argument to, and then it uses that
> value in some invisible sub that we don't see here and runs that
> sub (the continuewithfiledrop activity in this case) and uses that
> global variable "cancel" as an argument in some further activity it
> is considering (such as opening the dragged file). Is that what's
> going on here?

Yep.

--
http://www.acadx.com
"If you want to be somebody else change your mind"
0 Likes
Message 5 of 7

Anonymous
Not applicable
"MP" wrote in message

> question 1: So that means that in the context of this sub, Cancel is a
> global variable that the acadapplication created behind the scenes when
you
> created an acadapp variable using the with events keyword. Is that
correct?

No. It is a 'byref' parameter. A parameter to a method can
be either 'byref' or 'byval'. A 'byval' parameter is one that
is passed 'by value'.

A byref parameter is a parameter whose address is passed,
rather than its value. This allows the called funciton to
modify the variable in the caller's context. To understand
what 'byref' and 'byval' are try these:

Public Sub Caller()
Dim ANumber As Integer
ANumber = 10
Debug.Print "before MyFunc, ANumber = " & ANumber
Called ANumber
Debug.Print "after MyFunc, ANumber = " & ANumber
End Sub

Function Called(ByRef NumParam As Integer)
NumParam = 99
End Function

Notice that the Called function was able to modify the value
of the local variable 'ANumber' in the Caller(). Also note
that even though NumParam is declared as 'ByRef', all variables
in VB are ByRef by default, making the 'ByRef' modifier optional.

Declaring an object with WithEvents actually causes Visual
Basic to create a separate 'hidden' COM object, and this object
is responsible for 'sinking' the server's events.

The hidden COM object supports the outgoing event interface of
the object whose events you want to handle (in this case, that
would be _DAcadApplicationEvents).

When the server (AutoCAD) fires an event, it does so by calling
this hidden, VB-managed COM object's Invoke() method. To put it
in simple terms, this 'Invoke' method is passed an integer that
represents the method corresponding to the event (called a 'DISPID'
in COM parlance), along with an array of 0 or more variants, each
containing one of the event's parameters (in this case of the event
in question there are two, the second being the Cancel parameter)

The hidden COM object then looks for a method in your module whose
name is equal to the source object's name followed by an underscore
and the event name. If it finds a corresponding event handler for
the event being fired, that event handler is called by Visual Basic.

Because the parameters passed to the event are inside of Variants,
each variant can have a 'BYREF' flag indicating that the value it
contains is not the data itself, but rather the address of the data,
whose actual location is within the address space of the server
that fired the event.

Hence, if your event handler modifies the Cancel parameter, it is
actually modifying the value of a local variable that resides in
the address space of the server (AcadApplication) that is firing
the event.

That should also cover Question 2.
0 Likes
Message 6 of 7

Anonymous
Not applicable
Thanks for that clarification.
I really appreciate your generous sharing of knowlege with us newbies.

"Frank Oquendo" wrote in message
news:D639F89E98405CB6B25828E1453160EA@in.WebX.maYIadrTaRb...
0 Likes
Message 7 of 7

Anonymous
Not applicable
Thank you for responding. Looks like I need to delve into this com thing to
get a better grasp on what's going on behind the scenes. I appreciate your
taking time to go into this explanation, helps to clear up some of the
mysterious hidden workings. By reading many help examples I had roughly an
idea that that was what was happening but obviously none of the details and
no certainty that I was even remotely right. Looks like time for another
trip to the bookstore. If I knew more about COM, maybe I wouldn't be so
dense about some of this stuff.
Thanks again,
Mark

"Tony Tanzillo" wrote in message
news:0D7E357DD55C2B5D0C40ACCBEA6CDB3E@in.WebX.maYIadrTaRb...
0 Likes