Solved

Checking CD on serial port with API

Posted on 1997-07-13
6
178 Views
Last Modified: 2012-06-27
Hello, I need a piece of _working_ VB4 (32 bit) code (related declarations included) to check if there's CD (carrier detect) on COM1, without using MSComm control: I need to check the state without disturbing (or locking) a Remote Access procedure from getting control of the port, and I think I need to use Win32 API.
Ie, I need to _spy_ the connection to check when data exchange begins and ends.

Thank you,
       Paolo
0
Comment
Question by:paolo070697
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:y96andha
ID: 1428337
An alternate way is to use the RasEnumConnections to get the names of the currently active connections. This will not detect the actual modem carrier, but it will detect when there is an active connection, which I believe would fill the purpose?
0
 

Author Comment

by:paolo070697
ID: 1428338
Yes, I think it would fill the purpose, but I've checked and I don't happen to have the necessary VB declarations in Win32api.txt (or the like) that comes with VB. I would consider the question answered (and appreciate very much it, by the way), if you put in ALL the necessary declarations (including constants) and also some lines of working code, or someplace where to get them (declarations, constants, type definitions and samples for VB, of course).
Thank you,
        Paolo
0
 
LVL 5

Accepted Solution

by:
y96andha earned 100 total points
ID: 1428339

Declarations:
--------------------

Private Type rasconn
    dwSize As Long
    hrasconn As Long
    szEntryName As String * 257
    szDeviceType As String * 17
    szDeviceName As String * 130
End Type

Private Declare Function RasEnumConnections Lib "rasapi32" Alias "RasEnumConnectionsA" (buf As rasconn, numbytes As Long, numconns As Long) As Long


Code:
---------

Dim numbytes As Long, numconns As Long, success As Long
Dim rc As rasconn
rc.dwSize = Len(rc)
numbytes = Len(rc)
success = RasEnumConnections(rc, numbytes, numconns)
If numbytes > 0 Then
    ' Connect
    Print "Name of connection: "; rc.szEntryName
Else
    '
    Print "not connected"
End If

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:paolo070697
ID: 1428340
Just one mre, and I'll grade the question: since the RasEnumConnections says ON before the connection is really on, I'd like to use the RasGetConnectStatus, but I can't get it to work, probably because I haven't figured out the right parameters (the main problem is that I don't have the file with declarations nor the C header file), mainly RASCONNSTATE enum and the RASCS_DONE constant. May I impose further on you for the necessary delarations ?

Thank you,

Paolo


0
 
LVL 5

Expert Comment

by:y96andha
ID: 1428341
Sure. If you'd post your e-mail, I'll send you a copy of the full C header files too.

I might as well send you all the VB code I've written as samples for different people. This is really code for connecting and disconnecting, but I use the RasGetConnectStatus function in it too.

You may notice that the structures are bigger than sum of all members described in the API, probably due to alignment.

Private Const RASCS_DONE = &H2000
Private Const RASCS_Connected = RASCS_DONE
Private Const RASCS_Disconnected = RASCS_DONE + 1

Private Type rasconn
    dwSize As Long
    hrasconn As Long
    szEntryName As String * 257
    szDeviceType As String * 17
    szDeviceName As String * 130
End Type
Private Type RASCONNSTATUS
    dwSize As Long
    rasconnstate As Long
    dwError As Long
    szDeviceType As String * 17
    szDeviceName As String * 131
End Type
Private Type RASDIALPARAMS
    dwSize As Long
    szEntryName As String * 257
    szPhoneNumber As String * 129
    szCallbackNumber As String * 129
    szUserName As String * 257
    szPassword As String * 257
    szDomain As String * 19
End Type

Private Declare Function RasEnumConnections Lib "rasapi32" Alias "RasEnumConnectionsA" (buf As rasconn, numbytes As Long, numconns As Long) As Long


Private Declare Function RasDial Lib "rasapi32" Alias "RasDialA" (ByVal extensions As Long, ByVal phonebook As String, params As RASDIALPARAMS, ByVal notifiertype As Long, ByVal notifier As Long, rasconn As Long) As Long
Private Declare Function RasHangUp Lib "rasapi32" Alias "RasHangUpA" (ByVal rasconn As Long) As Long
Private Declare Function RasGetConnectStatus Lib "rasapi32" Alias "RasGetConnectStatusA" (ByVal rasconn As Long, connstatus As RASCONNSTATUS) As Long

Dim rasconn As Long


Private Sub Command1_Click()
Dim numbytes As Long, numconns As Long, success As Long
Dim rc As rasconn
rc.dwSize = Len(rc)
numbytes = Len(rc)
success = RasEnumConnections(rc, numbytes, numconns)
If numbytes > 0 Then
    ' Connect
    Print "Hanging up "; rc.szEntryName
    RasHangUp rc.hrasconn
Else
    '
    Print "not connected"
End If
End Sub

Private Sub Command2_Click()
    Dim status As Long
    Dim rdp As RASDIALPARAMS
    Dim rcs As RASCONNSTATUS
    Dim rasconn As Long
    rdp.dwSize = Len(rdp)
    rdp.szEntryName = "Swipnet" & Chr(0) ' select ras entry to use
    rdp.szPhoneNumber = Chr(0) ' use phone number from ras entry
    rdp.szCallbackNumber = Chr(0) ' use callback number from ras entry
    rdp.szUserName = "UserName" & Chr(0) ' user name for PPP logon
    rdp.szPassword = "Password" & Chr(0) ' password for PPP logon
    rdp.szDomain = "*" & Chr(0) ' use default domain
    status = RasDial(0, vbNullString, rdp, 0, 0, rasconn)
    If status = 0 Then
        rcs.dwSize = Len(rcs)
        status = RasGetConnectStatus(rasconn, rcs)
        If status <> 0 Then
            Debug.Print "RasGetConnectStatus error " & status
            RasHangUp rasconn
            Exit Sub
        Else
            If rcs.dwError Then
                Debug.Print "Connection failed, error " & rcs.dwError
                RasHangUp rasconn
                Exit Sub
            End If
            If rcs.rasconnstate = RASCS_Connected Then
                Debug.Print "Connected"
            Else
                RasHangUp rasconn
            End If
        End If
    Else
        Debug.Print "RasDial error " & status
        If rasconn Then RasHangUp rasconn
    End If
End Sub

Private Sub Command3_Click()
    RasHangUp rasconn
End Sub




0
 

Author Comment

by:paolo070697
ID: 1428342
Sure, thanks ! You can send them at asioli@geocities.com.

Thank you very much,

Paolo


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now