CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Quick questions

  1. #1
    Join Date
    Feb 2000
    Posts
    149

    Quick questions

    Does anyone know of a function to determine the screen resolution of the monitor through code?

    AND/OR

    Does anyone know how to determine the IP address of your own PC through code?

    Thanks,

    Sean


  2. #2
    Join Date
    Oct 2001
    Posts
    4

    Re: Quick questions


    EnumDisplaySettings

    this API-function can list all possible display-modes of a graphics-device. i'm not sure, whether it can tell you the current.


  3. #3
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Quick questions

    In VB4 and above you can use the Screen object i.e.


    Debug.print Screen.Width / Screen.TwipsPerPixelX & " by " & Screen.Height / Screen.TwipsPerPixelY




    HTH,
    D

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Quick questions

    Here's some help for your second question:


    private Const MAX_HOSTNAME_LEN = 132
    private Const MAX_DOMAIN_NAME_LEN = 132
    private Const MAX_SCOPE_ID_LEN = 260
    private Const MAX_ADAPTER_NAME_LENGTH = 260
    private Const MAX_ADAPTER_ADDRESS_LENGTH = 8
    private Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132
    private Const ERROR_BUFFER_OVERFLOW = 111

    private Type IP_ADDR_STRING
    next as Long
    IpAddress as string * 16
    IpMask as string * 16
    Context as Long
    End Type

    private Type IP_ADAPTER_INFO
    next as Long
    ComboIndex as Long
    AdapterName as string * MAX_ADAPTER_NAME_LENGTH
    Description as string * MAX_ADAPTER_DESCRIPTION_LENGTH
    AddressLength as Long
    Address(MAX_ADAPTER_ADDRESS_LENGTH - 1) as Byte
    Index as Long
    Type as Long
    DhcpEnabled as Long
    CurrentIpAddress as Long
    IpAddressList as IP_ADDR_STRING
    GatewayList as IP_ADDR_STRING
    DhcpServer as IP_ADDR_STRING
    HaveWins as Boolean
    PrimaryWinsServer as IP_ADDR_STRING
    SecondaryWinsServer as IP_ADDR_STRING
    LeaseObtained as Long
    LeaseExpires as Long
    End Type

    private Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo as Any, pOutBufLen as Long) as Long
    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as Any, Source as Any, byval Length as Long)
    Sub Form_Load()
    Dim error as Long
    Dim AdapterInfoSize as Long
    Dim AdapterInfo as IP_ADAPTER_INFO
    Dim AdapterInfoBuffer() as Byte

    AdapterInfoSize = 0

    error = GetAdaptersInfo(byval 0&, AdapterInfoSize)

    If error &lt;&gt; 0 then
    If error &lt;&gt; ERROR_BUFFER_OVERFLOW then
    MsgBox "GetAdaptersInfo sizing failed with error " & error
    Exit Sub
    End If
    End If

    ReDim AdapterInfoBuffer(AdapterInfoSize - 1)

    error = GetAdaptersInfo(AdapterInfoBuffer(0), AdapterInfoSize)
    If error &lt;&gt; 0 then
    MsgBox "GetAdaptersInfo failed with error " & error
    Exit Sub
    End If
    CopyMemory AdapterInfo, AdapterInfoBuffer(0), len(AdapterInfo)
    MsgBox "IP Address: " & AdapterInfo.IpAddressList.IpAddress
    MsgBox "Subnet Mask: " & AdapterInfo.IpAddressList.IpMask
    End Sub









  5. #5
    Join Date
    Aug 2001
    Location
    India
    Posts
    12

    Re: Quick questions

    For the second part of your question, you can use the WINSOCK control to find the ip address of the LOCAL HOST....


  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Quick questions

    Answer to QUestion 2

    'In a form
    option Explicit

    private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    MsgBox "IP-address: " + GetIPAddress
    End Sub

    'In a module
    option Explicit

    public Const MIN_SOCKETS_REQD as Long = 1
    public Const WS_VERSION_REQD as Long = &H101
    public Const WS_VERSION_MAJOR as Long = WS_VERSION_REQD \ &H100 And &HFF&
    public Const WS_VERSION_MINOR as Long = WS_VERSION_REQD And &HFF&
    public Const SOCKET_ERROR as Long = -1
    public Const WSADESCRIPTION_LEN = 257
    public Const WSASYS_STATUS_LEN = 129
    public Const MAX_WSADescription = 256
    public Const MAX_WSASYSStatus = 128
    public Type WSAData
    wVersion as Integer
    wHighVersion as Integer
    szDescription(0 to MAX_WSADescription) as Byte
    szSystemStatus(0 to MAX_WSASYSStatus) as Byte
    wMaxSockets as Integer
    wMaxUDPDG as Integer
    dwVendorInfo as Long
    End Type
    Type WSADataInfo
    wVersion as Integer
    wHighVersion as Integer
    szDescription as string * WSADESCRIPTION_LEN
    szSystemStatus as string * WSASYS_STATUS_LEN
    iMaxSockets as Integer
    iMaxUdpDg as Integer
    lpVendorInfo as string
    End Type
    public Type HOSTENT
    hName as Long
    hAliases as Long
    hAddrType as Integer
    hLen as Integer
    hAddrList as Long
    End Type
    Declare Function WSAStartupInfo Lib "WSOCK32" Alias "WSAStartup" (byval wVersionRequested as Integer, lpWSADATA as WSADataInfo) as Long
    Declare Function WSACleanup Lib "WSOCK32" () as Long
    Declare Function WSAGetLastError Lib "WSOCK32" () as Long
    Declare Function WSAStartup Lib "WSOCK32" (byval wVersionRequired as Long, lpWSADATA as WSAData) as Long
    Declare Function gethostname Lib "WSOCK32" (byval szHost as string, byval dwHostLen as Long) as Long
    Declare Function gethostbyname Lib "WSOCK32" (byval szHost as string) as Long
    Declare Sub CopyMemoryIP Lib "kernel32" Alias "RtlMoveMemory" (hpvDest as Any, byval hpvSource as Long, byval cbCopy as Long)
    public Function GetIPAddress() as string
    Dim sHostName as string * 256
    Dim lpHost as Long
    Dim HOST as HOSTENT
    Dim dwIPAddr as Long
    Dim tmpIPAddr() as Byte
    Dim I as Integer
    Dim sIPAddr as string
    If Not SocketsInitialize() then
    GetIPAddress = ""
    Exit Function
    End If
    If gethostname(sHostName, 256) = SOCKET_ERROR then
    GetIPAddress = ""
    MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name."
    SocketsCleanup
    Exit Function
    End If
    sHostName = Trim$(sHostName)
    lpHost = gethostbyname(sHostName)
    If lpHost = 0 then
    GetIPAddress = ""
    MsgBox "Windows Sockets are not responding. " & "Unable to successfully get Host Name."
    SocketsCleanup
    Exit Function
    End If
    CopyMemoryIP HOST, lpHost, len(HOST)
    CopyMemoryIP dwIPAddr, HOST.hAddrList, 4
    ReDim tmpIPAddr(1 to HOST.hLen)
    CopyMemoryIP tmpIPAddr(1), dwIPAddr, HOST.hLen
    for I = 1 to HOST.hLen
    sIPAddr = sIPAddr & tmpIPAddr(I) & "."
    next
    GetIPAddress = mid$(sIPAddr, 1, len(sIPAddr) - 1)
    SocketsCleanup
    End Function
    public Function GetIPHostName() as string
    Dim sHostName as string * 256
    If Not SocketsInitialize() then
    GetIPHostName = ""
    Exit Function
    End If
    If gethostname(sHostName, 256) = SOCKET_ERROR then
    GetIPHostName = ""
    MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name."
    SocketsCleanup
    Exit Function
    End If
    GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
    SocketsCleanup
    End Function
    public Function HiByte(byval wParam as Integer)
    HiByte = wParam \ &H100 And &HFF&
    End Function
    public Function LoByte(byval wParam as Integer)
    LoByte = wParam And &HFF&
    End Function
    public Sub SocketsCleanup()
    If WSACleanup() &lt;&gt; 0 then
    MsgBox "Socket error occurred in Cleanup."
    End If
    End Sub
    public Function SocketsInitialize() as Boolean
    Dim WSAD as WSAData
    Dim sLoByte as string
    Dim sHiByte as string
    If WSAStartup(WS_VERSION_REQD, WSAD) &lt;&gt; 0 then
    MsgBox "The 32-bit Windows Socket is not responding."
    SocketsInitialize = false
    Exit Function
    End If
    If WSAD.wMaxSockets &lt; MIN_SOCKETS_REQD then
    MsgBox "This application requires a minimum of " & CStr(MIN_SOCKETS_REQD) & " supported sockets."
    SocketsInitialize = false
    Exit Function
    End If
    If LoByte(WSAD.wVersion) &lt; WS_VERSION_MAJOR Or (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And HiByte(WSAD.wVersion) &lt; WS_VERSION_MINOR) then
    sHiByte = CStr(HiByte(WSAD.wVersion))
    sLoByte = CStr(LoByte(WSAD.wVersion))
    MsgBox "Sockets version " & sLoByte & "." & sHiByte & " is not supported by 32-bit Windows Sockets."
    SocketsInitialize = false
    Exit Function
    End If
    'must be OK, so lets do it
    SocketsInitialize = true
    End Function






    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured