-
June 22nd, 2012, 08:01 AM
#1
Send UDP Packets in VBA
First of all, Hello!
Righto, I'm writing a program which will be used as a simple debugger app whilst developing/testing various reports at work. I have the debugger finished which listens for packets then displays the message in a listbox. This was written in VB in Visual Studio 11 Beta and it works fine. I can both send and receive message no problems.
I did some googling and found some code which is supposed to send a UDP packet but for VB6/VBA so I can hook my *shudder* excel macro reports into my debugger too.
According to it's replys, the code is working fine. It's sending the messages out, but my .net app isn't receiving them.
Can anyone help me figure out what I'm doing wrong??
Here's the VB code I'm using: (Converted to VB from the MSDN C++ code in the winsock help files. I didn't convert this, I just fixed any errors which arose and made it work with a message of any size.)
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
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
Type socket
pointer As Long
End Type
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
Public Declare Function WSAGetLastError Lib "Ws2_32.dll" () As Integer
Public Declare Function WSAStartup Lib "Ws2_32.dll" (ByVal wVersionRequested As Integer, ByRef lpWSAData As LPWSADATA_Type) As Long
Public 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
Public Declare Function f_socket Lib "Ws2_32.dll" Alias "socket" (ByVal AF As Long, ByVal stype As Long, ByVal Protocol As Long) As Long
Public Declare Function closesocket Lib "Ws2_32.dll" (ByVal socket As Long) As Long
Public 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_DGRAM
Dim iProtocol As Integer : iProtocol = 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, 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
I hope someone can assist!
-
June 22nd, 2012, 10:16 AM
#2
Re: Send UDP Packets in VBA
If it used any POINTERS to memory locations, that's where it doesn't convert AT ALL. Manual calculation of all POINTER types might be possible, though.
-
June 22nd, 2012, 10:51 AM
#3
Re: Send UDP Packets in VBA
Thanks for replying. I believe it does use pointers as the dll needs the strings of data, and the code conversion was passing the array ByReference.
I'll have a look at manual pointers now anyway, thanks.
-
June 23rd, 2012, 05:40 PM
#4
Re: Send UDP Packets in VBA
VB6 doesn't know anything about pointers, or libraries that implement them.
-
June 25th, 2012, 03:29 AM
#5
Re: Send UDP Packets in VBA
It appears to know about the undocumented StrPtr command, apparently.
-
September 7th, 2017, 01:04 AM
#6
Re: Send UDP Packets in VBA
Hi mobiius,
I know it's a long time since, but did you figure out why .net was not receiving the UDP packet?
Cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|