I see that there are nice replies for C code (C#, at least). I hope it's OK for me to give my solution for vbScript in case someone Googles it.
Function AttachmentFileName(ByRef node)
Dim nodeValue, i, filenameSize
Dim HexString
HexString = ""
If(node.text <> "") Then
'typecast the node to base64 as Infopath doesn't do this in node definition
node.DataType = "bin.base64" 'Doing this will prevent the user from deleting the attachment
'Change the data type back to "" when done.
'convert base64 node value to binary and store in nodeValue byte array
nodeValue = node.nodeTypedValue
Else
AttachmentFileName = "Nothing attached. First attach a file."
Exit Function
End If
'Convert ByteArray to HexString
For i = 1 to LenB(nodeValue)
HexString = HexString & Right("0" & Hex(AscB(MidB(nodeValue, i, 1))), 2)
If(i = 300) Then 'Assuming header is no longer than 300 bytes
exit for
end if
Next
AttachmentFileName = ""
filenameSize = 0
'The length of the filenamebuffer is stored on pos 20-24 (byte), which means 41.47 in Hex and 1 as startpos for Arrays
filenameSize = getFilenameSize(HexString,41,47)
'Read the filename from position (49 to filenamebuffer * unicode - "\n") as end char
AttachmentFileName = getFileName(HexString,49,filenameSize*4 + 49 - 2)
node.DataType = "" 'This will allow the user to remove the attachment if desired
'MsgBox filename 'Uncomment for debugging
End Function
Function getFilenameSize(data, startLoop, stopLoop)
dim k
dim size
for k = startLoop to stopLoop step 2
dim y1
dim y2
dim y3
y1 = Mid(data,k,2)
y2 = CLng("&h" & y1)
y3 = y3 + Int(y2)
size = y3
next
getFilenameSize = size
End Function
Function getFileName(data, startLoop, stopLoop)
dim k
dim filename
for k = startLoop to stopLoop step 4
dim h1
dim h2
dim h3
h1 = Mid(data,k,2)
h2 = CLng("&h" & h1)
h3 = Chr(h2)
if(h3 <> "") then
filename = filename & Cstr(h3)
end if
next
getFileName = filename
End Function