Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,
Whenever i debug my application i got this run time error . . . Here itz my Code

VB
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As IntPtr, ByVal pSrc As IntPtr, ByVal ByteLen As Integer)
    Private Sub SingleToIEEE754()

        Dim bBytes(0 To 3) As Byte
        Dim rMyNumber As Single
        'Dim i As Integer

        rMyNumber = 333.7
        Call CopyMemory(bBytes(0), rMyNumber, 4)

        MsgBox(Hex$(bBytes(3)) & " " & Hex$(bBytes(2)) & " " & Hex$(bBytes(1)) & " " & Hex$(bBytes(0)))
    End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

VB
SingleToIEEE754()
    End Sub


Whenever i click on Button2 the run time error: "AccessViolationException was Unhandeld .."

Quote:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.



Can Any body help me .........
Thanks In ADV
Posted

1 solution

Those aren't pointers.
So when you call the external routine, and it thinks they are, it tries to access memory that doesn't exist, or isn't yours, or...
And so it throws an exception.
Why don't you just use the BitConverter class?
VB
Dim f As Single = 333.7F
Dim bytes As Byte() = BitConverter.GetBytes(f)
 
Share this answer
 
Comments
Himachandra 10-Mar-12 7:27am    
Thanks For reply..

How should i see the conversion bytes...?
Himachandra 10-Mar-12 7:32am    
When i see the output of bytes like " MsgBox(bytes) "this i got Error
Argument 'Prompt' cannot be converted to type 'String'.
OriginalGriff 10-Mar-12 7:38am    
If you looked at your own code sample, you might get a clue...

MsgBox(Hex$(bytes(3)) & " " & Hex$(bytes(2)) & " " & Hex$(bytes(1)) & " " & Hex$(bytes(0)))
OriginalGriff 10-Mar-12 7:39am    
Out of interest, what are you trying to achieve? Why are you converting floating point numbers to hex anyway?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900