Alternative Method for WebClient.OpenRead - c#

As WebClient.OpenRead(url) is blocking main thread until it opens url, In my case when server is not responding then it blocks user from activity until it throws timeout exception that's why I need alternative code that should have time out or method that would get data asynchronously.
Here is my code
Public Function IsUpdateAvailable(ByVal isAutoUpdate As Boolean) As String
Dim client As WebClient
Try
Dim configUrl As String = GetURL()
Dim configFile As String = GetFileName()
If String.IsNullOrEmpty(configUrl) Then
If Not isAutoUpdate Then
Return "Unable to check for updates"
End If
End If
If String.IsNullOrEmpty(configFile) Then
If Not isAutoUpdate Then
Return "Unable to check for updates"
End If
End If
configUrl = configUrl & configFile
If Not configUrl.ToUpper.StartsWith("HTTP") Then
configUrl = "http://" & configUrl
End If
client = New WebClient
Dim gfStream As Stream = client.OpenRead(configUrl)
Dim gfStreamReader As New StreamReader(gfStream)
Dim line As String = String.Empty
Dim version As String = String.Empty
Dim stringList As New Collections.Generic.List(Of String)
While Not gfStreamReader.EndOfStream
line = gfStreamReader.ReadLine
If Not String.IsNullOrEmpty(line) Then
stringList.Add(line)
End If
End While
gfStreamReader.Close()
gfStream.Close()
If stringList.Count > 0 Then
version = stringList(0)
If stringList.Count > 1 Then
_downloadFilePath = stringList(1)
End If
If isGreaterVersion(version) Then
Return "True"
Else
If Not isAutoUpdate Then
Return "Current version is up-to-date."
End If
End If
Else
If Not isAutoUpdate Then
Return "Current version is up-to-date."
End If
Return False
End If
Catch ex As WebException
Return "Unable to check for updates"
Catch ex As Exception
Return "Unable to check for updates" ''
End Try
End Function

WebClient has an Async version of OpenRead. OpenReadAsync.
Documenation: https://msdn.microsoft.com/en-us/library/system.net.webclient.openreadasync(v=vs.110).aspx
See Yuval's answer for an example: how to wait for webclient OpenReadAsync to complete

Related

SSL_connect() fail on WinCE 5.0

I used OpenSSL 1.0.2j to develop a desktop email client application with no
problems. I copied the code from my desktop to a WinCE 5.0 device .NET CF 2.0.
The connection SSL_connect() alway fails with a value of 5 (SSL_ERROR_SYSCALL).
A call to CE's GetLastError() on gives me an error 10038 (WSAENOTSOCK Socket operation on nonsocket).
Do you have any suggestions? what might be causing this problem?
Thanks in advance
Here's the code:
SSL_library_init()
OPENSSL_add_all_algorithms_noconf()
Dim sslCtx As IntPtr = SSL_CTX_new(SSLv23_client_method())
Dim ssl_socket As IntPtr = SSL_new(sslCtx)
Dim sbioPtr As IntPtr = BIO_new_socket(mySocket.Handle, 0)
SSL_set_bio(ssl_socket, sbioPtr, sbioPtr)
Dim connOK As Integer = SSL_connect(ssl_socket)
If connOK = 1 Then
.....
Else
MsgBox("OpenSSL's SSL_connect() failed")
Dim errcode As Integer = SSL_get_error(ssl_socket, connOK)
MsgBox("SSL Error: " + errcode.ToString)
If errcode = 5 Then
MsgBox("GLE = " + Runtime.InteropServices.Marshal.GetLastWin32Error.ToString)
End If
End If
mySocket.Close()
Else
MsgBox("Socket's connect() failed")
End If
Socket's handle create using CompactFramework on WinCE has always value greater than Int (max value expected on ' BIO_new_socket ')

How do I change connection properties in my ssis in C#?

I have this code to execute a package. What the package does is just export very large table data into excel file.
[HttpPost]
public ActionResult TableToFile()
{
try
{
var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SSIS"].ConnectionString);
var integrationServices = new IntegrationServices(connection);
var package = integrationServices
.Catalogs["SSISDB"]
.Folders["MyFolder"]
.Projects["MyProject"]
.Packages["MyPackage.dtsx"];
long executionIdentifier = package.Execute(true, null);
ExecutionOperation eo = integrationServices.Catalogs["SSISDB"].Executions[executionIdentifier];
while (!eo.Completed)
{
eo.Refresh();
System.Threading.Thread.Sleep(5000);
}
return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json(new { result = "error" }, JsonRequestBehavior.AllowGet);
}
}
However, the package has hard-coded connections like sql server source and excel file destination filename. That we can see in connection manager in the package.
I have this code in VB.Net and using a different library it sets also the connection of the package
Dim source As String = System.Windows.Forms.Application.StartupPath() & "\export.dtsx"
Dim p As Microsoft.SqlServer.Dts.Runtime.Package = app.LoadPackage(source, Nothing)
Dim exec As Executables = p.Executables
For Each config As ConnectionManager In p.Connections
Select Case config.Name
Case "SourceConnectionOLEDB"
config.ConnectionString = "..... some connection string here"
Case "DestinationConnectionFlatFile"
config.ConnectionString = file
Dim inner As Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerFlatFile90 = CType(config.InnerObject, Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerFlatFile90)
For i As Integer = 0 To inner.Columns.Count - 1
Dim innerColumn As Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerFlatFileColumn90 = inner.Columns(i)
If i < inner.Columns.Count - 1 Then
innerColumn.ColumnDelimiter = delimiter
Else
innerColumn.ColumnDelimiter = vbNewLine
End If
Next
End Select
Next
For Each taskhost As TaskHost In exec
taskhost.Execute(Nothing, Nothing, Nothing, Nothing, Nothing)
Next
My problem is how do I change connection string or some settings before I execute the package in my C# code above like my VB.Net does?
since you seem to be using the SSIS catalog, you should also be able to use project environment variables. Basically your package would reference these variables and you would set the values before execution.
There is an explanation on this at http://www.sqlchick.com/entries/2015/1/4/parameterizing-connections-and-values-at-runtime-using-ssis-environment-variables

Disable JIT debugging

This topic has been asked and answered in many questions and I did my due diligence but I just can't figure out why I am having the issue I have.
In testfailure.exe:
namespace testfailture
{
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception("I want to report this in the parent window");
}
catch (Exception ex)
{
throw ex;
}
}
}
In test.exe:
internal void Execute(string packageVersion)
{
Process exeProcess = new Process();
exeProcess.StartInfo.FileName = "testfailure.exe";
exeProcess.StartInfo.RedirectStandardOutput = true;
exeProcess.StartInfo.UseShellExecute = false;
exeProcess.StartInfo.CreateNoWindow = true;
try
{
exeProcess.Start();
Console.WriteLine(exeProcess.StandardOutput.ReadToEnd());
}
catch (Exception ex)
{
throw ex;
}
}
When I run the program, I get the pop-up and wouldn't let the program proceed until an action is taken.
I thought this was due to JIT debugging so I did everything from:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a52eb0ae-bcd8-4043-9661-d5fc3aa5167c/getting-rid-of-justintime-debugger?forum=vsdebug
That is one problem I have but ultimate what I want to do is subprocess reporting back the error (not console.writeline because I want to use that for reporting status) to the parent and display in parent's window.
Any thoughts?
By the way, I am using Visual Studio Professional 2012. Your help is much appreciated.
Thanks!
Edit #1----------------------------
My process fully expects everything to work but what I am trying to do is to code for unexpected failures. When fails, I want to have a good dialogue so I can easily and quickly detect and debug.
Throwing an unhandled exception cannot be passed to a parent process.
What you CAN do, however, is to write data to StandardError, and capture that in the parent process.
The example below is from MSDN:
Public Sub Main()
Dim args() As String = Environment.GetCommandLineArgs()
Dim errorOutput As String = ""
' Make sure that there is at least one command line argument.
If args.Length <= 1 Then
errorOutput += "You must include a filename on the command line." +
vbCrLf
End If
For ctr As Integer = 1 To args.GetUpperBound(0)
' Check whether the file exists.
If Not File.Exists(args(ctr)) Then
errorOutput += String.Format("'{0}' does not exist.{1}",
args(ctr), vbCrLf)
Else
' Display the contents of the file.
Dim sr As New StreamReader(args(ctr))
Dim contents As String = sr.ReadToEnd()
sr.Close()
Console.WriteLine("***** Contents of file '{0}':{1}{1}",
args(ctr), vbCrLf)
Console.WriteLine(contents)
Console.WriteLine("*****{0}", vbCrLf)
End If
Next
' Check for error conditions.
If Not String.IsNullOrEmpty(errorOutput) Then
' Write error information to a file.
Console.SetError(New StreamWriter(".\ViewTextFile.Err.txt"))
Console.Error.WriteLine(errorOutput)
Console.Error.Close()
' Reacquire the standard error stream.
Dim standardError As New StreamWriter(Console.OpenStandardError())
standardError.AutoFlush = True
Console.SetError(standardError)
Console.Error.WriteLine("{0}Error information written to ViewTextFile.Err.txt",
vbCrLf)
End If
End Sub

SMTP Repeater loger

I'm trying to do a repeater (as a proxy) for logging the data exchange between a SMTP client and a server. I thought it was only easy as:
Listening the client;
Listening client IP connection;
On call, connect to the server;
Send back server message;
Send the client messages to the server and return the server feedback to the client;
But, as I saw, some servers as MS exchange send multiple feedback witch are breaking the handshaking.Like:
250-SIZE 41943040
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-AUTH LOGIN
250-8BITMIME
250-BINARYMIME
250 CHUNKING
Here is the class
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class SmtpProxy
Private client As TcpClient
Private cltstream As NetworkStream
Private cltreader As System.IO.StreamReader
Private cltwriter As System.IO.StreamWriter
Private smtpserver As New TcpClient
Private srvstream As NetworkStream
Private srvreader As System.IO.StreamReader
Private srvwriter As System.IO.StreamWriter
Private Shared mHostSrv As String
Public Shared Property HostSrv() As String
Get
Return mHostSrv
End Get
Set(ByVal value As String)
mHostSrv = value
End Set
End Property
Private Shared mServerPort As Integer = 25
Public Shared Property ServerPort() As Integer
Get
Return mServerPort
End Get
Set(ByVal value As Integer
)
mServerPort = value
End Set
End Property
Private Shared mUserNm As String
Public Shared Property UserNm() As String
Get
Return mUserNm
End Get
Set(ByVal value As String)
mUserNm = value
End Set
End Property
Private Shared mPassword As String
Public Shared Property Password() As String
Get
Return mPassword
End Get
Set(ByVal value As String)
mPassword = value
End Set
End Property
Public Sub New(ByVal client As TcpClient)
Me.client = client
cltstream = client.GetStream()
cltreader = New System.IO.StreamReader(cltstream)
cltwriter = New System.IO.StreamWriter(cltstream)
cltwriter.NewLine = vbCr & vbLf
cltwriter.AutoFlush = True
End Sub
Public Shared Sub Start() 'ByVal args As String())
Dim listener As New TcpListener(IPAddress.Loopback, 2525)
listener.Start()
While True
Dim handler As New SmtpProxy(listener.AcceptTcpClient())
Dim thread As Thread = New System.Threading.Thread(New ThreadStart(AddressOf handler.Run))
thread.Start()
End While
End Sub
Public Sub Run()
If mHostSrv Like "*.*.*.*" Then
Dim IpS As IPAddress = Nothing
IpS = IPAddress.Parse(mHostSrv)
smtpserver.Connect(IpS, mServerPort)
Else
smtpserver.Connect(mHostSrv, mServerPort)
End If
srvstream = smtpserver.GetStream
srvreader = New System.IO.StreamReader(srvstream)
srvwriter = New System.IO.StreamWriter(srvstream)
srvwriter.NewLine = vbCrLf
srvwriter.AutoFlush = True
Dim srvline As String = srvreader.ReadLine
cltwriter.WriteLine(srvline)
Debug.Print("Server sent: {0}", srvline & vbCrLf)
Try
Dim line As String = cltreader.ReadLine
While line IsNot Nothing
Debug.Print("Read line {0}", line)
srvwriter.WriteLine(line)
Application.DoEvents()
srvline = srvreader.ReadLine()
Debug.Print("Server sent: {0}", srvline)
cltwriter.WriteLine(srvline.Replace("-", " "))
Application.DoEvents()
line = cltreader.ReadLine()
End While
Catch ex As Exception
client.Close()
smtpserver.Close()
End Try
End Sub
End Class
I tried to read multiple server lines at a time but in the meantime the client return and error if I send the whole returned server lines...
Private Function ReadSrvLines() As String
Dim strRet As String = ""
Do
If strRet Like "220*" OrElse srvreader.EndOfStream Then Exit Do
Dim strTmp As String = srvreader.ReadLine
If strTmp Is Nothing Then Exit Do
strRet &= strTmp.Replace("-", " ") & vbCrLf
cltwriter.WriteLine(strTmp.Replace("-", " "))
Debug.Print("Server sent: {0}", strTmp.Replace("-", " ") & vbCrLf)
Loop
Return strRet
End Function
Then is anyone have any solutions to propose?
Thanks all!!
Frank
You will need to perform simultaneous asynchronous reading/writing between client and server to make this work properly. SMTP responses can contain any number of lines and client or server can return a message without the need for a command (to say bye for example). Parsing the data is also unnecessary to achieve a basic log.
Edit: If it helps, if the 3 digit message number if followed by a (minus) rather than a (space) it means that there is another line to follow.

How to dial phone from a .NET website? Very old solution works, need to upgrade to server control

Currently, we have a website that relies on a Microsoft TAPI interface to dial a phone from within a .NET web site. It uses VBScript and tags, and it is bound to a master page. What we are looking for is a server control that would encompass all of this code and only be ran when it is included on a webpage.
The old page does the following:
<object classid="clsid:21D6D48E-A88B-11D0-83DD-00AA003CCABD" id="TAPIOBJ"></object>
<object classid="clsid:E9225296-C759-11d1-A02B-00C04FB6809F" id="MAPPER"></object>
After these lines of code are tags that contains VBScript to initialize the Microsoft TAPI 3.0 library and a few functions to dial. A Dialer control creates a call to one of the functions to dial in an onclick event.
Essentially, we want to create the same type of control without having tags embedded into the HTML of a page directly. We also do not want VBScript in there. Ideally, we would like a server control that works with the TAPI 3.0 API and gains access to the client's phone. Is this possible? Since we are talking about a "server" control, I'm skeptical. I could just as easily create a user control within the project, but we'd like to have this in a controls framework for use elsewhere instead of copying it.
I've been looking at this article on how to create a server control for injection of Client ActiveX controls, but is this down the right path?
UPDATE: Here's the VBScript:
This is what sits in the tag:
<script type="text/vbscript" LANGUAGE="VbScript">
'Constants section
'These constants are copied from tapi3if.idl
Const TAPIMEDIATYPE_AUDIO = &H08&
Const TAPIMEDIATYPE_VIDEO = &H8000&
Const S_MEDIA_AUDIOVIDEO = &H8008&
Const TD_CAPTURE = 0
Const TD_RENDER = 1
Const QSL_NEEDED = 1
Const AS_INSERVICE = 0
Const DC_NORMAL = 0
Const TE_CALLSTATE = 8
Const TE_CALLNOTIFICATION = 4
Const CS_DISCONNECTED = 3
Const CS_IDLE = 0
Const CS_OFFERING = 4
Const CS_CONNECTED = 2
Const CNE_OWNER = 0
Const CIS_CALLERIDNAME = 0
Const CIS_CALLERIDNUMBER = 1
'Interface IDs for casting
'Note: you can find the following IID-s in tapi3.h, tapi3if.idl or rend.idl
Const IID_String_ITMediaSupport = "{B1EFC384-9355-11D0-835C-00AA003CCABD}"
Const IID_String_ITTerminalSupport="{B1EFC385-9355-11D0-835C-00AA003CCABD}"
Const IID_String_ITBasicCallControl = "{B1EFC389-9355-11D0-835C-00AA003CCABD}"
'Const IID_String_ITCallInfo = "{B1EFC390-9355-11d0-835C-00AA003CCABD}"
'New interface
Const IID_String_ITCallInfo = "{350F85D1-1227-11D3-83D4-00C04FB6809F}"
Const IID_String_ITStreamControl= "{EE3BD604-3868-11D2-A045-00C04FB6809F}"
Const IID_String_ITDirectoryObjectConference= "{F1029E5D-CB5B-11D0-8D59-00C04FD91AC0}"
Const IID_String_ITCallStateEvent = "{62F47097-95C9-11d0-835D-00AA003CCABD}"
Const IID_String_ITCallNotificationEvent = "{895801DF-3DD6-11d1-8F30-00C04FB6809F}"
' IID of IVideoWindow
' Note: you can find this IID defined in control.h (from your sdk\inc directory),
' which contains the interface to type library QuartzTypeLib for quartz.dll;
' (search for the interface IVideoWindow)
Const IID_String_IVideoWindow = "{56A868B4-0AD4-11CE-B03A-0020AF0BA770}"
' The following CLSID is defined in tapi3.h
'(and it's used for creating a terminal of class "video window terminal")
Const CLSID_String_VideoWindowTerm = "{F7438990-D6EB-11d0-82A6-00AA00B5CA1B}"
'****************************************************************************
'Global variable section
'****************************************************************************
Dim CallStatus
Dim pICallState
pICallState = 0
'Set on True when we are unable to complete the connecting phase, to skip rest of processing
DIM sUnableToComplete
DIM sbNeedToExit
sUnableToComplete = False
sbNeedToExit = False
' If we want to receive incoming calls, we have to register on the corresponding addresses.
'We don't really use the values returned by registration (they are supposed to be used
'for unregistration), because Unregistration is performed automatically when we shutdown the TAPI object
'The variable pRegisteredCallNotification is an array that contains cookies returned by RegisterCallNotifications;
'these would normally be used to call UnregisterNotifications.
'The variable pRegisteredName holds correspondent AddressName
DIM pRegisteredCallNotification(50)
DIM pRegisteredName(50)
DIM iQtaRegistered
DIM callFrom
iQtaRegistered = 0
'Set by radio button "Select Address Type"
DIM sCurrentAddressType
'sCurrentAddressType = -1
sCurrentAddressType = 1
' This variable will hold a reference to the currently established call
DIM spITCall
spITCall = Empty
DIm pVideoWindow1
DIm pVideoWindow2
'Simplest error processing
Sub CheckError(strMsg)
if not Err.number = 0 Then
MsgBox strMsg & ":" & Err.number & ";"&Err.description
sbNeedToExit = True
Err.Clear
End If
End Sub
Function IsComponentInstalled(ProgId)
Dim tmpObject
On Error Resume Next
Set tmpObject = Server.CreateObject(ProgId)
If Err.Number = 0 Then
IsComponentInstalled = True
Else
IsComponentInstalled = False
End If
Set tmpObject = Nothing
End Function
</script>
Below the end body tag after the tags is:
<script type="text/vbscript" LANGUAGE="vbscript">
' Be sure that you call TAPIOBJ.Initialize before window_onload, otherwise you'll
' never receive events from tapi...
On Error Resume Next
call TAPIOBJ.Initialize
sUnableToComplete = False
TAPIOBJ.EventFilter = &H1FFFF&
if Not Err.number = 0 Then
MsgBox "TAPI software has not been installed on your workstation.",0,"Init"
sUnableToComplete = True
End If
For Each pITAddress in TAPIOBJ.Addresses
if left(pITAddress.AddressName,4) <> "Line" and left(pITAddress.AddressName,29) <> "Shoreline Multi-Line Monitor:" _
and pITAddress.MediaTypes = 8 then
callFrom = pITAddress.AddressName
end if
next
'This section shows how to override Application Priority:
'after the execution of the following lines, our app will always receive incoming calls
'even if there are other running tapi apps that had registered for receiving calls before our app.
call TAPIOBJ.SetApplicationPriority("IEXPLORE.EXE",TAPIMEDIATYPE_AUDIO,TRUE)
call TAPIOBJ.SetApplicationPriority("IEXPLORE.EXE",TAPIMEDIATYPE_VIDEO,TRUE)
' Check parameters of a call before connecting it
Sub PressConnect(pNumber,Status)
On Error Resume Next
'MsgBox (pNumber & "," & Status)
DIM iAddressType
DIM pConnectTo
DIM addressFrom
DIM selStr
'If not IsEmpty(spITCall) Then
' MsgBox "You are currently in call. Disconnect first",0,"connect"
'End If
pConnectTo = pNumber
set CallStatus=Status
addressFrom = callFrom
If addressFrom = "" Then
callStatus.innerHTML = "Feature Unavailable"
MsgBox "The TAPI Feature has not been setup on your phone line.",0,"COnnect"
else
sUnableToComplete = False
callStatus.innerHTML = "Connecting to " & pConnectTo & " ...."
'Create new internal call representation
For Each pITAddress in TAPIOBJ.Addresses
if pITAddress.AddressName = addressFrom Then
'Obtain ITMediaSupport
Set pITAddress_Connect = pITAddress
Exit For
End If
Next
Set pITAddress = Nothing
'Create a Call
DIM MediaTypes
MediaTypes = TAPIMEDIATYPE_AUDIO
Set pCall = pITAddress_Connect.CreateCall(pConnectTo,1,MediaTypes)
Set spITCall = pCall
if sUnableToComplete Then
Call DisconnectCall(1)
callStatus.innerHTML = "Call to "& pConnectTo & " failed."
End If
Call pCall.Connect(false)
' Check for error "invalid address" (see in tapi3err.h TAPI_E_INVALADDRESS=(HRESULT)0x8004000C)
if Err.Number = &H8004000C Then
Err.Clear
Call DisconnectCall(1)
callStatus.innerHTML = "Call to "& pConnectTo & " failed: Address is invalid"
Set pCall = Nothing
Else
if not Err.Number = 0 Then
Err.Clear
Call DisconnectCall(1)
callStatus.innerHTML = "Call to "& pConnectTo & " failed: error " & Hex(Err.number)
Set pCall = Nothing
Else
Set spITCall = pCall
End if
End If
end if
Set pCall = Nothing
end sub
' Disconnect current call
Sub HangUp(callDisc)
'On Error resume Next
if not IsEmpty(spITCall) Then
if not callDisc = 8 and not callDisc = 0 Then
' We need some kind of message pump here. The following call to MsgBox does exactly this:
'MsgBox "A call is disconnected",0,"Disconnect"
End If
Set pVideoWindow1 = Nothing
Set pVideoWindow2 = Nothing
' ConnANN.innerHTML = "Disconnected"
if callDisc=0 Then
spITCall.Disconnect(DC_NORMAL)
End If
Set spITCall = Nothing
spITCall = Empty
callStatus.innerHTML = "Disconnected"
'btnDisconnect.disabled = true
'source.visible = false
End If
End Sub
'*****************************************************************************
' Tapi events processing:
' - call state events ("connected", "disconnected")
' - and call notification events (these calls will be in "offering" state)
Sub TAPIOBJ_Event(event_type, tapi_event)
'On Error Resume Next
'Check For disconnected call
if event_type = TE_CALLSTATE Then
DIM pITCallStateEvent
Set pITCallStateEvent = MAPPER.QueryDispatchInterface(_
IID_String_ITCallStateEvent,tapi_event)
iCallState = pITCallStateEvent.State
if iCallState= CS_DISCONNECTED or iCallState= CS_IDLE Then
cause = pITCallStateEvent.Cause
'pICallState=iCallState
strinnerHTML = ""
Select Case cause
Case 1 ' CEC_DISCONNECT_NORMAL - Normal disconnect
strinnerHTML = "Disconnected"
Case 2 ' CEC_DISCONNECT_BUSY
strinnerHTML = "Your Party is busy.Try Later."
Case 3 ' CEC_DISCONNECT_BADADDRESS
strinnerHTML = "Address is invalid"
case 4 ' CEC_DISCONNECT_NOANSWER
strinnerHTML = "No answer from your party."
case 0 'CEC_NONE
strinnerHTML = "No answer from your party."
Case Else
strinnerHTML = "Your call is cancelled, rejected or failed"
End Select
'Call DisconnectCall(1)
'btnDisconnect.disabled = true
End If 'Call is disconnected
if iCallState = CS_CONNECTED Then 'Call is connected
callStatus.innerHTML = "Call is connected."
'btnDisconnect.disabled = False
End If 'Call is connected
End If ' event: call state
'Check only for incoming calls
if event_type = TE_CALLNOTIFICATION Then ' We have an incoming call (an "offering" call)
DIM pITCallNotificationEvent
Set pITCallNotificationEvent = MAPPER.QueryDispatchInterface(_
IID_String_ITCallNotificationEvent,tapi_event)
Call CheckError("TAPIOBJ_Event:query for pITDirectoryObjectUser")
CallOwnership = pITCallNotificationEvent.Event
DIM pITCallInfo
Set pITCallInfo = pITCallNotificationEvent.Call
Call CheckError("TAPIOBJ_Event:get pITCallInfo")
if not blnShowOnlyOnce and pITCallInfo.CallState = CS_OFFERING and not ( CallOwnership = CNE_OWNER) Then
MsgBox "Unable to accept incoming calls: is other instance of this app running?",0,"Info"
blnShowOnlyOnce = True
Exit Sub
End IF
if CallOwnership = CNE_OWNER Then 'We are the owner!
if not IsEmpty(spITCall) Then
MsgBox "Already in call, disconnect first",0,"Incoming Call"
Exit Sub
End if
if pITCallInfo.CallState = CS_OFFERING Then 'Offering
'-- CIS_CALLERIDNAME Wasn't working so I switched to NUMBER
sCalleeName = pITCallInfo.CallInfoString(CIS_CALLERIDNAME)
if not Err.number = 0 then ' Caller ID name is not supported
sCalleeName = "Unknown Name"
Err.Clear
End if
sCalleeNumber = pITCallInfo.CallInfoString(CIS_CALLERIDNUMBER)
if not Err.number = 0 then ' Caller ID name is not supported
sCalleeNumber = "Unknown Number"
Err.Clear
End if
DIM pITCallOffer
Set pITCallOffer = MAPPER.QueryDispatchInterface( _
IID_String_ITBasicCallControl, pITCallInfo)
Call CheckError("TAPIOBJ_Event:query for pITCall")
response = MsgBox("A call from '" & sCalleeNumber & " " & sCalleeName & "' has arrived. Do you want to accept it?",4,"Incoming call")
if not response = 7 Then 'the did not press "NO", so he pressed "YES"
Call AcceptIncomingCall(pITCallOffer, pITCallInfo)
End If
End If 'Call is offering
End If 'We are owner
End If 'Call Notification has arrived
End Sub
</script>
Is it possible to use the ITAPI3 Managed Library to get rid of this and do this in the code-behind?
Hopefully, someone knows of a cleaner, more modern way of doing this, but here's what I did.
I embedded the VBScript into a script server-side, and registered it as a client script. Before I registered it, I added the tags into Literal controls and added them to the Page header. The server control itself was nothing but a link around a phone icon image.
Since I could call VBScript functions inside javascript, I created a GetPhoneNumber function that accepted the controlID of the control where I would get my phone number. This was set on the ControlID property of the Dialer control. In this instance, it was a textbox. The function parses the number for any bad data, then called PressConnect... done. It works, but I'm not really all that pleased with using VBScript in this manner.
If anyone has any idea how to interact with the client (local) TAPI of a user (Actual examples would be appreciated), post them here.

Categories

Resources