I am reusing the code from [here](http://forums.codeguru.com/showthread.php?524835-Send-UDP-Packets-in-VBA) with some changes - added *private* keyword to some definitions because Excel VBA was reporting errors and changed type to IPRAW/ICMP. 

At the end I see garbage in the wireshark *in the data section* of the packet, however data size is correct (3 bytes). I checked that sendBuf is really containing first bytes as three 'a' when sendTo is executed. What is wrong?

Data is sent using `CommandButton1_Click()` sub at the bottom of the listing.

Here's the code

```
'reference
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx
Const INVALID_SOCKET = -1
Const WSADESCRIPTION_LEN = 256

Enum AF
  AF_UNSPEC = 0
  AF_INET = 2
  AF_IPX = 6
  AF_APPLETALK = 16
  AF_NETBIOS = 17
  AF_INET6 = 23
  AF_IRDA = 26
  AF_BTH = 32
End Enum

Enum sock_type
   SOCK_STREAM = 1
   SOCK_DGRAM = 2
   SOCK_RAW = 3
   SOCK_RDM = 4
   SOCK_SEQPACKET = 5
End Enum

Enum Protocol
   IPPROTO_ICMP = 1
   IPPROTO_IGMP = 2
   BTHPROTO_RFCOMM = 3
   IPPROTO_TCP = 6
   IPPROTO_UDP = 17
   IPPROTO_ICMPV6 = 58
   IPPROTO_RM = 113
End Enum

'Type sockaddr
'   sa_family As Integer
'   sa_data(0 To 13) As Byte
'End Type

Private Type sockaddr_in
  sin_family As Integer
  sin_port As Integer
  sin_addr(0 To 3) As Byte
  sin_zero(0 To 7) As Byte
End Type

Private Type socket
   pointer As Long
End Type

Private Type LPWSADATA_Type
   wVersion As Integer
   wHighVersion As Integer
   szDescription(0 To WSADESCRIPTION_LEN) As Byte
   szSystemStatus(0 To WSADESCRIPTION_LEN) As Byte
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   lpVendorInfo As Long
End Type

Private Declare Function WSAGetLastError Lib "Ws2_32.dll" () As Integer
Private Declare Function WSAStartup Lib "Ws2_32.dll" (ByVal wVersionRequested As Integer, ByRef lpWSAData As LPWSADATA_Type) As Long
Private Declare Function sendto Lib "Ws2_32.dll" (ByVal socket As Long, ByRef buf() As Byte, ByVal length As Long, ByVal flags As Long, ByRef toaddr As sockaddr_in, tolen As Long) As Long
Private Declare Function f_socket Lib "Ws2_32.dll" Alias "socket" (ByVal AF As Long, ByVal stype As Long, ByVal Protocol As Long) As Long
Private Declare Function closesocket Lib "Ws2_32.dll" (ByVal socket As Long) As Long
Private Declare Sub WSACleanup Lib "Ws2_32.dll" ()

Sub SendPacket(Message As String, IP As String, Port As Integer)
   Dim ConnectSocket As socket
   Dim wsaData As LPWSADATA_Type
   Dim iResult As Integer: iResult = 0
   Dim send_sock As sock_type: send_sock = INVALID_SOCKET
   Dim iFamily As AF: iFamily = AF_INET
   Dim iType As Integer: iType = SOCK_RAW 'SOCK_UDP
   Dim iProtocol As Integer: iProtocol = IPPROTO_ICMP 'IPPROTO_UDP
   Dim SendBuf(0 To 1023) As Byte
   Dim BufLen As Integer: BufLen = 1024
   Dim RecvAddr As sockaddr_in: RecvAddr.sin_family = AF_INET: RecvAddr.sin_port = Port
   Dim SplitArray As Variant: SplitArray = Split(IP, ".")

   RecvAddr.sin_addr(0) = SplitArray(0)
   RecvAddr.sin_addr(1) = SplitArray(1)
   RecvAddr.sin_addr(2) = SplitArray(2)
   RecvAddr.sin_addr(3) = SplitArray(3)

   For buf = 1 To Len(Message)
      SendBuf(buf - 1) = Asc(Mid(Message, buf, 1))
   Next buf
   SendBuf(buf + 1) = 0

   iResult = WSAStartup(&H202, wsaData)
   If iResult <> 0 Then
      MsgBox ("WSAStartup failed: " & iResult)
      Exit Sub
   End If

   send_sock = f_socket(iFamily, iType, iProtocol)
   If send_sock = INVALID_SOCKET Then
      Errno = WSAGetLastError()
      Exit Sub
   End If

   iResult = sendto(send_sock, SendBuf, Len(Message), 0, RecvAddr, Len(RecvAddr)) ' BufLen, 0, RecvAddr, Len(RecvAddr))
   If iResult = -1 Then
      MsgBox ("sendto failed with error: " & WSAGetLastError())
      closesocket (send_sock)
      Call WSACleanup
      Exit Sub
   End If

   iResult = closesocket(send_sock)
   If iResult <> 0 Then
      MsgBox ("closesocket failed with error : " & WSAGetLastError())
      Call WSACleanup
   End If
End Sub

Private Sub CommandButton1_Click()
    Call SendPacket("aaa", "192.168.1.55", 1000)
End Sub

```

**Update:** As per Remy Lebeau's advice I did the following changes

```
Private Declare Function sendto Lib "Ws2_32.dll" (ByVal socket As Long, ByVal buf As LongPtr, ByVal length As Long, ByVal flags As Long, ByRef toaddr As sockaddr_in, tolen As Long) As Long

```

changing `ByRef buf() As Byte` with `ByVal buf As LongPtr`,

```
Private Declare PtrSafe Function VarPtrArray Lib "VBE7" Alias "VarPtr" (Var() As Any) As LongPtr
...
iResult = sendto(send_sock, VarPtrArray(SendBuf) + 12, Len(Message), 0, RecvAddr, Len(RecvAddr))

```

changing `SendBuf` to `VarPtrArray(SendBuf) + 12`.

but I still get the garbage.

**Update 2:** success:

added

```
Private Declare PtrSafe Function VarPtrArray Lib "VBE7" Alias "VarPtr" (Var() As Any) As LongPtr

```

modified

```
iResult = sendto(send_sock, VarPtr(SendBuf(0)), Len(Message), 0, RecvAddr, Len(RecvAddr))

```