I don't know what errors you might be having are... but I work in both languages simultaneously and constantly, and from what I can see, the exact translation for the C# code, above, is:
<DllImport("urlmon.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, SetLastError:=False)> _
Public Shared Function FindMimeFromData(pBC As IntPtr, _
<MarshalAs(UnmanagedType.LPWStr)>
pwzUrl As String, _
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.U1, SizeParamIndex:=3)>
pBuffer() As Byte, _
cbSize As Integer, _
<MarshalAs(UnmanagedType.LPWStr)>
pwzMimeProposed As String, _
dwMimeFlags As Integer, _
ByRef ppwzMimeOut As IntPtr, _
dwReserved As Integer) As Integer
End Function
Public Shared Function GetMimeFromBytes(data As Byte()) As String
Dim MimeSampleSize As Integer = 256
Dim DefaultMimeType As String = "application/octet-stream"
If (data Is Nothing) Then
Throw New ArgumentNullException("data", "Hey, data is null.")
End If
Dim mimeTypePointer As IntPtr = IntPtr.Zero
Try
FindMimeFromData(IntPtr.Zero, Nothing, data, MimeSampleSize, Nothing, 0, mimeTypePointer, 0)
Dim mime As String = Marshal.PtrToStringUni(mimeTypePointer)
Return If((mime IsNot Nothing) AndAlso (mime <> ""), mime, DefaultMimeType)
Catch ex As AccessViolationException
'Debug.WriteLine(e.ToString())
Return DefaultMimeType
Finally
If (mimeTypePointer <> IntPtr.Zero) Then
Marshal.FreeCoTaskMem(mimeTypePointer)
End If
End Try
End Function
Now. If you run into problems with the LPArray being Null, that might be something to consider, you might want to make that an IntPtr (or ByRef IntPtr, depending on what the case is, here), and then convert the IntPtr to array using a Marshal.Copy or some such...
... however, one thing I can tell you, is that if the function succeeds, Marshal.FreeCoTaskMem might never be called... I'm not sure about the Finally block behavior on Return, but I would never have written it that way, in VB. Because, in VB, you would want to write the code this way:
<DllImport("urlmon.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, SetLastError:=False)> _
Public Shared Function FindMimeFromData(pBC As IntPtr, _
<MarshalAs(UnmanagedType.LPWStr)>
pwzUrl As String, _
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.U1, SizeParamIndex:=3)>
pBuffer() As Byte, _
cbSize As Integer, _
<MarshalAs(UnmanagedType.LPWStr)>
pwzMimeProposed As String, _
dwMimeFlags As Integer, _
ByRef ppwzMimeOut As IntPtr, _
dwReserved As Integer) As Integer
End Function
Public Shared Function GetMimeFromBytes(data As Byte()) As String
Dim MimeSampleSize As Integer = 256
Dim DefaultMimeType As String = "application/octet-stream"
If (data Is Nothing) Then
Throw New ArgumentNullException("data", "Hey, data is null.")
End If
Dim mimeTypePointer As IntPtr = IntPtr.Zero
Try
FindMimeFromData(IntPtr.Zero, Nothing, data, MimeSampleSize, Nothing, 0, mimeTypePointer, 0)
Dim mime As String = Marshal.PtrToStringUni(mimeTypePointer)
GetMimeFromBytes = CType(If((mime IsNot Nothing) AndAlso (mime <> ""), mime, DefaultMimeType), String)
Catch ex As AccessViolationException
'Debug.WriteLine(e.ToString())
GetMimeFromBytes = DefaultMimeType
Finally
If (mimeTypePointer <> IntPtr.Zero) Then
Marshal.FreeCoTaskMem(mimeTypePointer)
End If
End Try
End Function
And this code validates with Option Strict On, so ... Option Infer On is probably not necessary.