Solved

DLL Error Handling

Posted on 2002-05-14
8
143 Views
Last Modified: 2010-05-02
I have written a DLL and this has some public properties eg

Public Property Let BorrowAmount(temp As Long)
    intBorrowAmount = temp
End Property

If a client uses this DLL and assigns an illegal value to the BorrowAmount, I wish to trap this error and handle it within the DLL rather than the client.  What is the best way to accomplish this?

Thanks
0
Comment
Question by:daveamour
  • 4
  • 4
8 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7008412
It all depends on what you mean by an illegal amount. Based on this sample you are almost certain to get some errors as you pass the value as a long yet assign it to what appears to be an integer.

From your code I would do the following, change the property to make the value passed by value rather than by reference:

Public Property Let BorrowAmount(ByVal temp As Long)

Then make sure that the value that you are passing has the same data type as you are really interested in. Long/Integer etc.

Then add some error handling in the property procedure and possibly add an event so that this can be passed back to the calling application.

Public Event DllError(ByVal Description As String, ByVal Comments As String)

Public Property Let BorrowAmount(ByVal temp As Integer)
   On Error Goto errBorrow
   intBorrowAmount = temp
   Exit Property

errBorrow:
  RaiseEvent DllError("Invalid borrow amount",Cstr(Temp))
End Property
0
 
LVL 19

Author Comment

by:daveamour
ID: 7008460
intBorrowAmount is a Long actually.  What I meant by illegal was type mismatches, overflows things like that.  Will your suggestion handle that.  If I understand it, it will yes?

Also is it possible to have the errBorrow:
 RaiseEvent DllError("Invalid borrow amount",Cstr(Temp))

in global scope, or do I need one of these for each property?

Many thanks

Dave
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7008485
You would declare the event in the declarations section of each class that you want the event to be applicable to. You can then instantiate the object in the calling application using the withevents keyword:

Dim WithEvents myClass As MyDll.MyClass

You then get the events in the VB IDE as you would for any other control or class.

You should use the RaiseEvent line in any procedure code within that class that you want to return this information to the calling application. So in your form or module where you use this class:

Dim WithEvents myClass As MyDll.MyClass

Private Sub Command1_Click()
  Set myClass = New MyDll.MyClass
  With myClass
    .BorrowAmount = 100
  End With
  Set myClass = Nothing
End Sub

Private Sub myClass_DllError(ByVal Description As String, ByVal Comments As String)
  Msgbox Description & vbLF & Comments,vbOkOnly,App.Title
End Sub

If you ensure that you explicitly declare the type of the variable in your procedure declarations:

Public Property Let BorrowAmount(ByVal temp As Integer)

Then VB will ensure that you are only passing variables with the correct datatype to the procedure so that partially eliminates the problem there. The error handling I show will also help trap anything that gets through this. Of course you can make this as complex as you want. You may have an event called BoundaryConditions or something like that which you raise when any of the values that you are using are outside of your predefined ranges. This may not be a real type mismatch error as such but can be a great way of providing feedback to the calling application and an implementation of business rules within your dll.
0
 
LVL 19

Author Comment

by:daveamour
ID: 7008511
Sorry, one other thing

The RaiseEvent is a Sub/Function which I must write yes?

If so then what is the command for exiting a DLL, I tried End but that doesn't work.

Many thanks

Dave

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Author Comment

by:daveamour
ID: 7008522
And 1 other thing!

My client app in this case is an ASP page, can I use WithEvents in this case?

In ASP my code would be:

Dim myClass
Set myClass = Server.CreateObject("MyDll.MyClass")

so how would I add the WithEvents bit in, if I can do this?

I'll give you the points soon!

Thanks

Dave
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7008587
Dave,

You don't need to write a procedure for RaiseEvent, just use the statement anywhere in a procedure and provide values for the parameters.

You don't actually explicit exit a dll. You just need to destroy any references to it from the calling application.

Set myClass = Nothing

For example, this causes the calling application to release the reference to the object instantiated from the dll and to release any resources associated with that. It also causes the Class_Terminate event within the dll to be fired to ensure that you have the opportunity to cleanup any objects used by the dll before it terminates.

As for ASP, I don't know is the honest answer, I have never done much work with asp so cannot really help on that bit of it. I am sure it is possible but couldn't tell you how.
0
 
LVL 19

Author Comment

by:daveamour
ID: 7008664
Ok thanks very much, you have been very helpful :)

Dave
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7008669
You are welcome Dave.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now