Click here to Skip to main content
15,887,822 members
Home / Discussions / C#
   

C#

 
AnswerRe: Curious Bug Pin
User 66588-Aug-07 5:18
User 66588-Aug-07 5:18 
GeneralRe: Curious Bug Pin
JamesBarnes8-Aug-07 5:29
JamesBarnes8-Aug-07 5:29 
GeneralRe: Curious Bug Pin
JamesBarnes8-Aug-07 5:37
JamesBarnes8-Aug-07 5:37 
QuestionGetting a computer name from the IP address(C#) Pin
WvdW8-Aug-07 4:23
WvdW8-Aug-07 4:23 
AnswerRe: Getting a computer name from the IP address(C#) Pin
led mike8-Aug-07 4:46
led mike8-Aug-07 4:46 
AnswerRe: Getting a computer name from the IP address(C#) Pin
Hessam Jalali8-Aug-07 5:06
Hessam Jalali8-Aug-07 5:06 
GeneralRe: Getting a computer name from the IP address(C#) Pin
WvdW8-Aug-07 5:38
WvdW8-Aug-07 5:38 
GeneralRe: Getting a computer name from the IP address(C#) Pin
Hessam Jalali8-Aug-07 5:45
Hessam Jalali8-Aug-07 5:45 
I just had it in my archive maybe simplify your work but it in VB Frown | :(

<br />
<br />
Option Explicit<br />
<br />
'// define constants<br />
Private Const IP_SUCCESS As Long = 0<br />
Private Const SOCKET_ERROR As Long = -1<br />
<br />
Private Const MAX_WSADescription As Long = 256<br />
Private Const MAX_WSASYSStatus As Long = 128<br />
Private Const MIN_SOCKETS_REQD As Long = 1<br />
<br />
Private Const WS_VERSION_REQD As Long = &H101<br />
'Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD / &H100 And &HFF&<br />
Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&<br />
<br />
Private Const WSADescription_Len As Long = 256<br />
Private Const WSASYS_Status_Len As Long = 128<br />
Private Const AF_INET As Long = 2<br />
<br />
'// structures<br />
<br />
Private Type HOSTENT<br />
    hName As Long<br />
    hAliases As Long<br />
    hAddrType As Integer<br />
    hLength As Integer<br />
    hAddrList As Long<br />
End Type<br />
<br />
 <br />
Private Type WSADATA<br />
   wVersion As Integer<br />
   wHighVersion As Integer<br />
   szDescription(0 To MAX_WSADescription) As Byte<br />
   szSystemStatus(0 To MAX_WSASYSStatus) As Byte<br />
   wMaxSockets As Long<br />
   wMaxUDPDG As Long<br />
   dwVendorInfo As Long<br />
End Type<br />
<br />
'// api<br />
'kernel32<br />
Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nBytes As Long)<br />
Private Declare Function apiStrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long<br />
'wsock32<br />
Private Declare Function apiGetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal hostname As String) As Long<br />
Private Declare Function apiWSAStartup Lib "wsock32.dll" Alias "WSAStartup" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long<br />
Private Declare Function apiWSACleanup Lib "wsock32.dll" Alias "WSACleanup" () As Long<br />
Private Declare Function apiInetAddr Lib "wsock32.dll" Alias "inet_addr" (ByVal s As String) As Long<br />
Private Declare Function apiGetHostByAddr Lib "wsock32.dll" Alias "gethostbyaddr" (haddr As Long, ByVal hnlen As Long, ByVal addrtype As Long) As Long<br />
 <br />
'// private functions<br />
Private Function InitializeSocket() As Boolean<br />
    Dim WSAD As WSADATA<br />
   <br />
    'attempt to initialize the socket<br />
    InitializeSocket = apiWSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS<br />
End Function<br />
<br />
Private Sub CloseSocket()<br />
    'try to close the socket<br />
    If apiWSACleanup() <> 0 Then<br />
        MsgBox "Error calling apiWSACleanup.", vbCritical<br />
    End If<br />
<br />
End Sub<br />
<br />
Public Function GetIPFromHostName(ByVal sHostName As String) As String<br />
    'converts a host name to an IP address.<br />
   <br />
    Dim nBytes As Long<br />
    Dim ptrHosent As Long<br />
    Dim hstHost As HOSTENT<br />
    Dim ptrName As Long<br />
    Dim ptrAddress As Long<br />
    Dim ptrIPAddress As Long<br />
    Dim sAddress As String 'declare this as Dim sAddress(1) As String if you want 2 ip addresses returned<br />
   <br />
    'try to initalize the socket<br />
    If InitializeSocket() = True Then<br />
      <br />
        'try to get the IP<br />
        ptrHosent = apiGetHostByName(sHostName & vbNullChar)<br />
       <br />
        If ptrHosent <> 0 Then<br />
                   <br />
            'get the IP address<br />
            apiCopyMemory hstHost, ByVal ptrHosent, LenB(hstHost)<br />
            apiCopyMemory ptrIPAddress, ByVal hstHost.hAddrList, 4<br />
             <br />
            'fill buffer<br />
            sAddress = Space$(4)<br />
            'if you want multiple domains returned,<br />
            'fill all items in sAddress array with 4 spaces<br />
           <br />
            apiCopyMemory ByVal sAddress, ByVal ptrIPAddress, hstHost.hLength<br />
           <br />
            'change this to<br />
            'CopyMemory ByVal sAddress(0), ByVal ptrIPAddress, hstHost.hLength<br />
            'if you want an array of ip addresses returned<br />
            '(some domains have more than one ip address associated with it)<br />
           <br />
            'get the IP address<br />
            GetIPFromHostName = IPToText(sAddress)<br />
            'if you are using multiple addresses, you need IPToText(sAddress(0)) & "," & IPToText(sAddress(1))<br />
            'etc<br />
        End If<br />
    Else<br />
        MsgBox "Failed to open Socket."<br />
    End If<br />
End Function<br />
<br />
Private Function IPToText(ByVal IPAddress As String) As String<br />
    'converts characters to numbers<br />
    IPToText = CStr(Asc(IPAddress)) & "." & _<br />
              CStr(Asc(Mid$(IPAddress, 2, 1))) & "." & _<br />
              CStr(Asc(Mid$(IPAddress, 3, 1))) & "." & _<br />
              CStr(Asc(Mid$(IPAddress, 4, 1)))<br />
End Function<br />
<br />
Public Function GetHostNameFromIP(ByVal sIPAddress As String) As String<br />
   <br />
    Dim ptrHosent As Long<br />
    Dim hAddress As Long<br />
    Dim sHost As String<br />
    Dim nBytes As Long<br />
   <br />
    'try to open the socket<br />
    If InitializeSocket() = True Then<br />
   <br />
        'convert string address to long datatype<br />
        hAddress = apiInetAddr(sIPAddress)<br />
       <br />
        'check if an error ocucred<br />
        If hAddress <> SOCKET_ERROR Then<br />
           <br />
            'obtain a pointer to the HOSTENT structure<br />
            'that contains the name and address<br />
            'corresponding to the given network address.<br />
            ptrHosent = apiGetHostByAddr(hAddress, 4, AF_INET)<br />
           <br />
            If ptrHosent <> 0 Then<br />
               <br />
                'convert address and<br />
                'get resolved hostname<br />
<br />
                apiCopyMemory ptrHosent, ByVal ptrHosent, 4<br />
               <br />
                nBytes = apiStrLen(ByVal ptrHosent)<br />
               <br />
                If nBytes > 0 Then<br />
                    'fill the IP address buffer<br />
                    sHost = Space$(nBytes)<br />
                   <br />
                    apiCopyMemory ByVal sHost, ByVal ptrHosent, nBytes<br />
                    GetHostNameFromIP = sHost<br />
                End If<br />
            Else<br />
                MsgBox "Call to gethostbyaddr failed."<br />
            End If<br />
            'close the socket<br />
            CloseSocket<br />
        Else<br />
            MsgBox "Invalid IP address"<br />
        End If<br />
    Else<br />
        MsgBox "Failed to open Socket"<br />
    End If<br />
End Function<br />
<br />
<br />

GeneralRe: Getting a computer name from the IP address(C#) Pin
WvdW8-Aug-07 6:24
WvdW8-Aug-07 6:24 
QuestionAccessing Windows Form through a Thread of another Thread Pin
ictoane8-Aug-07 3:51
ictoane8-Aug-07 3:51 
AnswerRe: Accessing Windows Form through a Thread of another Thread Pin
Luc Pattyn8-Aug-07 4:13
sitebuilderLuc Pattyn8-Aug-07 4:13 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
ictoane8-Aug-07 4:56
ictoane8-Aug-07 4:56 
AnswerRe: Accessing Windows Form through a Thread of another Thread Pin
pbraun8-Aug-07 4:42
pbraun8-Aug-07 4:42 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
ictoane8-Aug-07 5:08
ictoane8-Aug-07 5:08 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
Luc Pattyn8-Aug-07 5:19
sitebuilderLuc Pattyn8-Aug-07 5:19 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
ictoane8-Aug-07 5:30
ictoane8-Aug-07 5:30 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
Luc Pattyn8-Aug-07 5:45
sitebuilderLuc Pattyn8-Aug-07 5:45 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
ictoane8-Aug-07 6:35
ictoane8-Aug-07 6:35 
GeneralRe: Accessing Windows Form through a Thread of another Thread Pin
pbraun8-Aug-07 8:34
pbraun8-Aug-07 8:34 
QuestionRpc Server Unavailable in Excel Com Object Pin
sakthi dasan8-Aug-07 3:49
sakthi dasan8-Aug-07 3:49 
AnswerRe: Rpc Server Unavailable in Excel Com Object Pin
- Pascal -8-Aug-07 18:27
- Pascal -8-Aug-07 18:27 
GeneralRe: Rpc Server Unavailable in Excel Com Object Pin
sakthi dasan8-Aug-07 23:15
sakthi dasan8-Aug-07 23:15 
Questionqueue of string arrays syntax Pin
Lord_Veralix8-Aug-07 3:43
Lord_Veralix8-Aug-07 3:43 
AnswerRe: queue of string arrays syntax Pin
Hessam Jalali8-Aug-07 4:02
Hessam Jalali8-Aug-07 4:02 
GeneralRe: queue of string arrays syntax Pin
Lord_Veralix8-Aug-07 4:07
Lord_Veralix8-Aug-07 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.