Help with Connection Manager PInvoking

  • Thread starter Justin Marshall
  • Start date
J

Justin Marshall

Greetings,

I am having difficulty calling the ConnMgrEstablishConnection function
through PInvoke which I hope somebody can assist with.
I have used the CMHelper project in the PPC2002SDK as a basis for this code.

The call to ConnMgrMapURL works fine, but when I try and call
ConnMgrEstablishConnection (via the EstablishConnection function below), I
get a NotSupportedException.
I suspect that it may be a problem with the ConnMgr_ConnectionInfo structure
(eg. data types). I had tried hWnd and lParam with Integer data type as well
with the same effect.

My call to ConnMgrConnectionStatus also achieves the same result but
possibly because I do not have a valid handle.

Thanks for any help anybody is able to provide.

Regards,
Justin Marshall.



Following is the code I currently have (in VB.Net):

'---------------------------------------------------------------------
Imports System.Runtime.InteropServices

Public Class CConnMgr

Private m_lngConnectionHandle As Long

Private Const CONNMGR_PARAM_GUIDDESTNET = 1 ' @constdefine guidDestNet
field is valid

Private Const CONNMGR_PARAM_MAXCONNLATENCY = 8 ' @constdefine
MaxConnLatency field is valid

Private Const CONNMGR_PRIORITY_USERINTERACTIVE = &H8000

Public Const CONNMGR_STATUS_UNKNOWN = &H0 ' @constdefine Unknown status
Public Const CONNMGR_STATUS_CONNECTED = &H10 ' @constdefine Connection
is up
Public Const CONNMGR_STATUS_DISCONNECTED = &H20 ' @constdefine
Connection is disconnected
Public Const CONNMGR_STATUS_CONNECTIONFAILED = &H21 ' @constdefine
Connection failed and cannot not be reestablished
Public Const CONNMGR_STATUS_CONNECTIONCANCELED = &H22 ' @constdefine
User aborted connection
Public Const CONNMGR_STATUS_CONNECTIONDISABLED = &H23 ' @constdefine
Connection is ready to connect but disabled
Public Const CONNMGR_STATUS_NOPATHTODESTINATION = &H24 ' @constdefine No
path could be found to destination
Public Const CONNMGR_STATUS_WAITINGFORPATH = &H25 ' @constdefine Waiting
for a path to the destination
Public Const CONNMGR_STATUS_WAITINGFORPHONE = &H26 ' @constdefine Voice
call is in progress
Public Const CONNMGR_STATUS_WAITINGCONNECTION = &H40 ' @constdefine
Attempting to connect
Public Const CONNMGR_STATUS_WAITINGFORRESOURCE = &H41 ' @constdefine
Resource is in use by another connection
Public Const CONNMGR_STATUS_WAITINGFORNETWORK = &H42 ' @constdefine No
path could be found to destination
Public Const CONNMGR_STATUS_WAITINGDISCONNECTION = &H80 ' @constdefine
Connection is being brought down
Public Const CONNMGR_STATUS_WAITINGCONNECTIONABORT = &H81 ' @constdefine
Aborting connection attempt

Public Structure ConnMgr_ConnectionInfo
Public Size As Long
Public Params As Long
Public Flags As Long
Public Priority As Long

Public Exclusive As Boolean
Public Disabled As Boolean

Public DestNetGuid As Guid

Public hWnd As IntPtr
Public Msg As Integer
Public lParam As IntPtr

Public MaxCost As Long
Public MinRcvBw As Long
Public MaxConnLatency As Long
End Structure

<DllImport("cellcore.dll")> Private Shared Function
ConnMgrEnumDestinations(ByVal DestIndex As Integer, ByRef DestInfo As
ConnMgr_DestinationInfo) As Integer
End Function
<DllImport("cellcore.dll")> Private Shared Function
ConnMgrEstablishConnection(ByRef ConnInfo As ConnMgr_ConnectionInfo, ByRef
ConnHandle As Long) As Integer
End Function
<DllImport("cellcore.dll")> Private Shared Function
ConnMgrEstablishConnectionSync(ByRef ConnInfo As ConnMgr_ConnectionInfo,
ByRef ConnHandle As Long, ByVal Timeout As Long, ByRef FinalStatus As Long)
As Integer
End Function
<DllImport("cellcore.dll")> Private Shared Function
ConnMgrConnectionStatus(ByVal hConn As Long, ByRef iStatus As Long) As
Integer
End Function
<DllImport("cellcore.dll")> Private Shared Function ConnMgrMapURL(ByVal
URL As String, ByRef NetworkGuid As Guid, ByRef GuidIndex As Long) As
Integer
End Function
<DllImport("cellcore.dll")> Private Shared Function
ConnMgrReleaseConnection(ByVal ConnHandle As Long, ByVal
AllowConnectionToBeCached As Boolean) As Integer
End Function

Public Function GetNetworkFromPath(ByVal URL As String) As Guid

Dim NetworkGuid As Guid
Dim GuidIndex As Long
Dim Result As Integer

Result = ConnMgrMapURL(URL, NetworkGuid, GuidIndex)

Return NetworkGuid

End Function

Public Function EstablishConnection() As Integer

Dim ConnInfo As ConnMgr_ConnectionInfo
Dim ConnHandle As Long
Dim FinalStatus As Long
Dim Result As Integer

ConnInfo.Size = Marshal.SizeOf(ConnInfo)
ConnInfo.Params = CONNMGR_PARAM_GUIDDESTNET Or
CONNMGR_PARAM_MAXCONNLATENCY
ConnInfo.Flags = 0
ConnInfo.MaxConnLatency = 4000
ConnInfo.Disabled = False
ConnInfo.Priority = CONNMGR_PRIORITY_USERINTERACTIVE
ConnInfo.DestNetGuid = GetNetworkFromPath("http://server")

Try
Result = ConnMgrEstablishConnectionSync(ConnInfo, ConnHandle,
4000, FinalStatus)
m_lngConnectionHandle = ConnHandle

MsgBox("Connection handle = " & ConnHandle.ToString)

Catch ex As System.NotSupportedException
MsgBox("NotSupportedError in EstablishConnection")

Catch ex As Exception
MsgBox("Error in EstablishConnection (" & ex.Message & ")")

End Try

End Function

Public Function Status() As Integer

Dim lngCurrentStatus As Long
Dim intResult As Integer

intResult = ConnMgrConnectionStatus(m_lngConnectionHandle,
lngCurrentStatus)

Return lngCurrentStatus

End Function

End Class
 
A

Alex Feinman [MVP]

Replace all instances of Long with Integer. Long is not supported by
marshaller and unlike VB6 it is 64bit integer. YOu need 32-bit, which is
type Integer
 
J

Justin Marshall

Thanks for your prompt and exact help Alex. No more NotSupportedExceptions.
Brilliant.

Now, when I ask for a URL of "http://server" (as per posted code), I get a
status of CONNMGR_STATUS_NOPATHTODESTINATION. ConnMgrMapURL returns a valid
GUID for this address.
Yet when I enter this address into Pocket Internet Explorer, it correctly
dials out.
Same result if I try a URL of "http://www.microsoft.com".

Have you (or others) any experience or thoughts on this one?

Thanks again,
Justin.
 
A

Alex Feinman [MVP]

Alex Yakhnin and I have actually created RAS.NET component that includes
Connection Manager wrapper.
Off the top of my head I don't see a problem with your code - what I use is
very similar although I prefer establishing connection asynchronously
 
Joined
Oct 21, 2005
Messages
2
Reaction score
0
Please send me sample code

Hi,

These days i stuck in the same thing, Can you please send me the code for establishing connection using Connection Manager API.

I mean please send me the final code you changed for making connection.

Thank
 
Joined
Sep 19, 2006
Messages
1
Reaction score
0
Source Code Required

Hi all There,

I am also working on the same project, for connecting to ftp site using RAS connection using pocket pc's internal modem.
Can you please send me the complete code for using connection manager for making RAS connection?

I will be highly thankful to you.

Please reply me at (e-mail address removed)

thanks,

Sachin Vaid
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top