Convert an IP address to a Long Integer for API Calls

Brian Hof

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
I was looking for a way to convert a dotted quad IP address ( to a long integer (and back) when I stumbled upon an old snippet from this very site.

The problem was, the method I found returned a Variant, not a long, and nothing I could seem to do would get the PostMessage API function to accept the return value (I wanted to pass the IP alongside a message to another process via PostMessage's lparam). Also, the method of packing each of the four bytes into the Variant seemed rather fishy to me - as if it weren't actually packing the data bit-for-bit.

Fortunately, I found a few leads elsewhere that helped me construct the following snippet. It makes use of the LSet command and a little bit of trickery.

While some may argue that LSet is not very stable as various platforms may store the basic data types differently, I beg to differ. I've had no problems with this on Windows 2000 and VB 6, and as it makes use of very basic data types I think you'll be safe wherever you go with it. Let me know if you discover otherwise. It serves as a nice bit of VB ingenuity, anyway.

Instructions: Copy the declarations and code below and paste directly into your VB project.