Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object

Status
Not open for further replies.

TSSTechie

Technical User
May 21, 2003
353
0
0
GB
Hi

I was hoping somebody might be able to shed some light on this one for me. The code below compiles fine however, when it runs, it throws an "Object reference not set to an instance of an object" error when trying to run the line highlighted.

Can anyone offer any suggestion as to why this is happening please ?

Many thanks

TSSTechie

Code:
Private Declare Sub FormatExt Lib "fmifs.dll" Alias "FormatEx" (ByRef DriveRoot As String, ByVal MediaFlag As Integer, ByRef Format As String, ByRef Label As String, ByVal QuickFormat As Boolean, ByVal ClusterSize As Integer, ByVal Callback As FormateExDelegateFunction)

Private Sub Format()

	Dim strDriveRoot As String, strFormat As String, strLabel As String, boolQuickFormat As Boolean, intClusterSize As Integer

	strDriveRoot = "F:\"
	strFormat = "FAT"
	strLabel = "CC4BUILD"
	boolQuickFormat = False
	intClusterSize = 0

	[aqua]FormatExt(strDriveRoot, 8, strFormat, strLabel, boolQuickFormat, intClusterSize, AddressOf FMIFSCallback)[/aqua]

End Sub

Private Enum CallbackCommand
        PROGRESS
        DONEWITHSTRUCTURE
        UNKNOWN2
        UNKNOWN3
        UNKNOWN4
        UNKNOWN5
        INSUFFICIENTRIGHTS
        WRITEPROTECTED
        UNKNOWN8
        UNKNOWN9
        UNKNOWNA
        DONE
        UNKNOWNC
        UNKNOWND
        Output
        STRUCTUREPROGRESS
End Enum

Private Delegate Function FormateExDelegateFunction(ByVal Command As Long, ByVal SubAction As Long, ByVal ActionInfo As Long) As Boolean
Private Function FMIFSCallback(ByVal Command As Long, ByVal SubAction As Long, ByVal ActionInfo As Long) As Boolean
        Dim bStatus As Boolean
        Select Case Command
            Case CallbackCommand.DONE
                bStatus = CBool(ActionInfo)
                If bStatus Then
                    MsgBox("Drive Format completed")
                Else
                    MsgBox("Drive Format failed")
                End If
            Case CallbackCommand.WRITEPROTECTED
                MsgBox("Disk is write protected")
        End Select
        FMIFSCallback = True
End Function

[lightsaber] May The Force Be With You [trooper] [yoda]
 
First:
Second: As far as I know you cant use AddressOf with a fucntion in that manner. If you are trying to use the delegate then it would need to be something like:

Code:
Dim del As New FormateExDelegateFunction(AddressOf FMIFSCallback)
FormatExt(strDriveRoot, 8, strFormat, strLabel, boolQuickFormat, intClusterSize, del)
Or something along those lines.

Third: You need to do something with your Enum CallbackCommand. It could be causing the error itself because it is Empty.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen

Thanks for the reply.

Apologies for the colour. I was in a bit of a rush when I posted it and Aqua was the first colour I came across in the TGML examples provided.

I have now got this working. I had to make a couple of adjustments to the 'Delegate Function' and 'Function' declarations as below :

Code:
Private Delegate Function FormateExDelegateFunction(ByVal Command As CallbackCommand, ByVal SubAction As Integer, ByRef ActionInfo As Integer) As Integer
Private Function FMIFSCallback(ByVal Command As CallbackCommand, ByVal SubAction As Integer, ByRef ActionInfo As Integer) As Integer

I'm assuming changing the Command type was what you were refering to in your third option.

For info, I made the change as per your second suggestion but this didn't help. Having got the code working, I changed this back to how it was originally (AddressOf FMIFSCallback directly in the FormatExt call) and this seems to work ok so it seems the AddressOf Function can be used like that.

Thanks again

TSSTechie

[lightsaber] May The Force Be With You [trooper] [yoda]
 
I'm assuming changing the Command type was what you were refering to in your third option.

This is what you had.
Code:
Private Enum CallbackCommand
        PROGRESS
        DONEWITHSTRUCTURE
        UNKNOWN2
        UNKNOWN3
        UNKNOWN4
        UNKNOWN5
        INSUFFICIENTRIGHTS
        WRITEPROTECTED
        UNKNOWN8
        UNKNOWN9
        UNKNOWNA
        DONE
        UNKNOWNC
        UNKNOWND
        Output
        STRUCTUREPROGRESS
End Enum

With this if you tried CallbackCommand.PROGRESS it should be empty and should have caused an error of some type when it tried to use it.

Code:
Private Enum CallbackCommand
        PROGRESS = 1234
        DONEWITHSTRUCTURE = 1234
...
End Enum
Each one has to be set to some value. You would have to find out what the correct value for each of those you have in your Enum actually is. If it is working before that is done then that is extremely strange.

For info, I made the change as per your second suggestion but this didn't help. Having got the code working, I changed this back to how it was originally (AddressOf FMIFSCallback directly in the FormatExt call) and this seems to work ok so it seems the AddressOf Function can be used like that.
Hmmm. New one on me, but if that is how it is working go with it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top