Monday, May 19, 2025

Retrieve Local IP Addresses

I received a question today if I knew how to retrieve the local machine's IP address, so they can use it for an audit trail log. I didn't have a ready-made solution, so I had to search for one. Luckily, I found VBA - Determine Your PC's IP Address created by Daniel Pineault at DevHut. I was very happy to pass this info along to the person who asked me the question as I am sure it would really help them in their project.

Later on, I began to wonder, just for fun, how I might modify Daniel's code to use regular expressions. I am not saying using regular expressions would make it better. I was just curious to see how differently it would look like. The below function is what I came up with after a quick search for a good regex pattern.

I am not trying to create a replacement function, so I merely concentrated on grabbing the IPv4 addresses and did not include any error checks in the code. If you decide to use this, please test it thoroughly and let me know how it can be improved. Thank you!

Public Function GetMyIPs() As String
'thedbguy@gmail.com
'5/19/2025
'https://www.devhut.net/vba-determine-your-pcs-ip-address/
'https://ihateregex.io/expr/ip/

Dim regEx As Object
Dim regExMatch As Object
Dim var As Variant
Dim strIPInfo As String
Dim strResult As String

'use ipconfig to get IP info
CreateObject("Wscript.Shell").Run "cmd /c ipconfig | findstr /R /C:""IPv4 Address"" | clip", 0, True
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'use clipboard
    .GetFromClipboard
    strIPInfo = .GetText
End With

'parse IP info
Set regEx = CreateObject("VBScript.RegExp")
With regEx
    .Global = True
    .IgnoreCase = True
    .Pattern = "(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}"
    Set regExMatch = .Execute(strIPInfo)
    For Each var In regExMatch
        strResult = strResult & ";" & var
    Next
    
End With

If strResult <> "" Then
    GetMyIPs = Mid(strResult, 2)
End If

Set regExMatch = Nothing
Set regEx = Nothing

End Function

Wednesday, January 1, 2025

SHA256 Hashing Function

I know this is not a new topic, but I just thought I would post it here for my own future reference.

I have used other SHA256 hashing functions before, but they were long and complicated with multiple loops.

I discovered that the same thing can be accomplished by taking advantage of some built-in .Net Framework methods.

The below function should work in Access VBA without any need for additional or external references.

Please let me know if you run into any issues or if you have any suggestions for improvements.

Happy New Year!

Function dbgSHA256(PlainText As String) As String
'thedbguy@gmail.com
'1/1/2025

Dim encoder As Object
Dim hasher As Object
Dim textToHash() As Byte
Dim hash() As Byte
Dim cypher() As String
Dim x As Long

' Create objects for encoding and hashing
Set encoder = CreateObject("System.Text.UTF8Encoding")
Set hasher = CreateObject("System.Security.Cryptography.SHA256Managed")

' Convert the input string to bytes
textToHash = encoder.GetBytes_4(PlainText)

' Compute the hash
hash = hasher.ComputeHash_2(textToHash)

' Convert the hash to a hexadecimal string
ReDim cypher(UBound(hash))
For x = 0 To UBound(hash)
    cypher(x) = Hex$(hash(x))
Next

' Return the result
dbgSHA256 = Join(cypher, "")

' Clean up objects
Set hasher = Nothing
Set encoder = Nothing

End Function