500分赠送

ikis 2003-09-29 06:20:58
在各种版本下 RasConn 应该怎么设置?C#的预处理命令怎么用?
请问调用在win2000下,为什么调用API 函数 RasEnumConnections是总是返回632,和调用API函数RasGetErrorString老是抛出ExecutionEngineException 异常。
...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2003-09-30
  • 打赏
  • 举报
回复
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Diagnostics; // Debug, Trace

namespace RAS_Application

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class MainForm : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox txtMain;

private System.Timers.Timer m_Timer;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public IntPtr m_Handle;

public MainForm()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// DoWork();

}

:

:

:

:

private void DoWork()

{

RAS ras =new RAS();

RASCONN lpRasConn =new RASCONN();

int lpcb =0;

int lpcConnections =0;

// lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN));

// lpRasConn->dwSize = sizeof(RASCONN);

// lpcb = sizeof(RASCONN);

// ...SizeOf() ist 'unsafe' Code !!

lpRasConn.dwSize = System.Runtime.InteropServices.Marshal.SizeOf(lpRasConn);

lpcb = lpRasConn.dwSize;

int nRet = RAS.API_RasEnumConnections(ref lpRasConn, ref lpcb, ref
lpcConnections);

if(nRet != 0)

{

Trace.WriteLine("RasEnumConnections failed: Error = " +nRet.ToString());

txtMain.Text +="RasEnumConnections failed: Error = " +nRet.ToString();

}

else

{

Trace.WriteLine("The following RAS connections are currently active\n\n");

for (int i = 0; i < lpcConnections; i++)

{

Trace.WriteLine("Entry name: " +lpRasConn.szEntryName);

txtMain.Text +="Entry name: " +lpRasConn.szEntryName +" ";

// lpRasConn++;

}

}

if(nRet == 0)

{

// Get the connection status.

RASCONNSTATUS RasStatus =new RASCONNSTATUS();

// RasStatus.dwSize = sizeof (RASCONNSTATUS);

RasStatus.dwSize = System.Runtime.InteropServices.Marshal.SizeOf(RasStatus);

int dwReturn = RAS.API_RasGetConnectStatus(lpRasConn.hrasconn, ref
RasStatus);

// If there is an error in getting the connection status

if(dwReturn != 0)

{

Trace.WriteLine("Failed getting connect status. Error : "
+dwReturn.ToString());

txtMain.Text +="Failed getting connect status. Error : "
+dwReturn.ToString();

}

else

{

Trace.WriteLine("Connect status : " +RasStatus.rasconnstate.ToString() +"
Device : " +RasStatus.szDeviceName);

txtMain.Text +="Connect status : " +RasStatus.rasconnstate.ToString() +"
Device : " +RasStatus.szDeviceName;

}

}

}

:

:

:

/// <summary>

/// Holt den Status der RAS-Verbindung

/// </summary>

public RASCONNSTATE GetConnStatus(IntPtr intRasConn)

{

int intRetcode =0;

RASCONNSTATE RASConnState =new RASCONNSTATE();

string strDeviceName =null;

string strDevicetype =null;

RASCONNSTATUS rasconnstatus95 =new RASCONNSTATUS();

rasconnstatus95.dwSize = 160;

try

{

intRetcode = RAS.API_RasGetConnectStatus(intRasConn, ref rasconnstatus95);

}

catch(Exception e)

{

Trace.WriteLine(e.Message.ToString());

txtMain.Text +="\r\nException: " +e.Message.ToString();

}

if(intRetcode != 0)

{

/*strDeviceName = "Not Available"

strDevicetype = "Not Available"

' lngRASErrorNumber = lngRetcode

' strRASDescription = lpRASError.fcnRASErrorString()

' fcnRASGetConnectionStatus = lngRetcode

*/

}

else

{

// success

RASConnState = rasconnstatus95.rasconnstate;

strDeviceName = rasconnstatus95.szDeviceName;

strDevicetype = rasconnstatus95.szDeviceType;

// fcnRASGetConnectionStatus = 0

}

Trace.WriteLine(strDeviceName +" " +strDevicetype);

txtMain.Text +="\r\nDevice-Name: " +strDeviceName +" Device-Typ: "
+strDevicetype;

return RASConnState;

}

private void OnTimerTick(object sender, System.Timers.ElapsedEventArgs e)

{

// int Handle =0;

// Trace.WriteLine("OnTimerTick()");

GetActiveConnection(ref m_Handle);

Trace.WriteLine("OnTimerTick(): " +m_Handle.ToString());

txtMain.Text +="\r\nOnTimerTick(): " +m_Handle.ToString();

GetConnStatus(m_Handle);

}

private string GetActiveConnection(ref IntPtr Handle)

{

RASCONN lprasconn =new RASCONN();

int lpcConnections =0;

int lpcb;

IntPtr hRasConn;

string NameRasConn;

lprasconn.dwSize = 412;

lpcb = 412; //256 * lprasconn[0].dwSize;

string ret = null;

/*

========== Exception Text ==========

System.NullReferenceException: Der Wert 'null' wurde gefunden, als eine
Objektinstanz erforderlich war.

at RAS_Application.RAS.API_RasEnumConnections(RASCONN& lprasconn, Int32&
lpcb, Int32& lpcConnections)

at RAS_Application.MainForm.GetActiveConnection(IntPtr& Handle) in c:\.net
tests\03.11\ras_application\mainform.cs:line 281

at RAS_Application.MainForm.OnTimerTick(Object sender, ElapsedEventArgs e)
in c:\.net tests\03.11\ras_application\mainform.cs:line 260

*/

if(RAS.API_RasEnumConnections(ref lprasconn, ref lpcb, ref lpcConnections)
== 0)

{

if(lpcConnections > 0) // Eine DFÜ-Netzwerkverbindung ist aktiv

{

hRasConn = lprasconn.hrasconn; // Handle der Verbindung

Handle = hRasConn;

NameRasConn = lprasconn.szEntryName.ToString();

NameRasConn = NameRasConn.PadRight(NameRasConn.IndexOf(NameRasConn));

NameRasConn.Trim();

Trace.WriteLine("Name RAS-Conection: " +NameRasConn);

txtMain.Text +="\r\nName RAS-Conection: " +NameRasConn;

if(NameRasConn.IndexOf("Direct Cable Connection") > 0)

{

return null;

}

ret = NameRasConn;

}

else

Handle = IntPtr.Zero;

Trace.WriteLine("in if(): API_RasEnumConnections() !=0");

txtMain.Text +="\r\nin if(): API_RasEnumConnections() !=0";

}

Trace.WriteLine("in if(): API_RasEnumConnections() ==0");

txtMain.Text +="\r\nin if(): API_RasEnumConnections() ==0 Phonebook: "
+lprasconn.szPhonebook;

return ret;

}

}

}
cnhgj 2003-09-30
  • 打赏
  • 举报
回复
一个例子,你参考一下

using System;

using System.Runtime.InteropServices; // [DllImport]

namespace RAS_Application

{

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct LUID

{

int LowPart;

long HighPart;

}

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct RASCONN

{

public int dwSize;

public IntPtr hrasconn;

public string szEntryName;

// if(System.Environment.OSVersion >= 0x400)

//#if (WINVER >= 0x400)

public string szDeviceType;

public string szDeviceName;

//#endif

//#if (WINVER >= 0x401)

public string szPhonebook;

public int dwSubEntry;

//#endif

//#if (WINVER >= 0x500)

public Guid guidEntry;

//#endif

//#if (WINVER >= 0x501)

public int dwSessionId;

public int dwFlags;

public LUID luid;

//#endif

}

public enum RASCONNSTATE

{

RASCS_OpenPort = 0,

RASCS_PortOpened,

RASCS_ConnectDevice,

RASCS_DeviceConnected,

RASCS_AllDevicesConnected,

RASCS_Authenticate,

RASCS_AuthNotify,

RASCS_AuthRetry,

RASCS_AuthCallback,

RASCS_AuthChangePassword,

RASCS_AuthProject,

RASCS_AuthLinkSpeed,

RASCS_AuthAck,

RASCS_ReAuthenticate,

RASCS_Authenticated,

RASCS_PrepareForCallback,

RASCS_WaitForModemReset,

RASCS_WaitForCallback,

RASCS_Projected,

//#if (WINVER >= 0x400)

RASCS_StartAuthentication, // Windows 95 only

RASCS_CallbackComplete, // Windows 95 only

RASCS_LogonNetwork, // Windows 95 only

//#endif

RASCS_SubEntryConnected,

RASCS_SubEntryDisconnected,

RASCS_Interactive = 0x1000, // RASCS_PAUSED,

RASCS_RetryAuthentication,

RASCS_CallbackSetByCaller,

RASCS_PasswordExpired,

//#if (WINVER >= 0x500)

RASCS_InvokeEapUI,

//#endif

RASCS_Connected = 0x2000, //RASCS_DONE,

RASCS_Disconnected

}

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct RASCONNSTATUS

{

public int dwSize;

public RASCONNSTATE rasconnstate;

public int dwError;

public string szDeviceType;

public string szDeviceName;

}

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct RASEAPINFO

{

public int dwSizeofEapInfo;

public byte [] pbEapInfo;

}

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct RASDIALEXTENSIONS

{

public int dwSize;

public int dwfOptions;

public int hwndParent;

public uint reserved;

//#if (WINVER >= 0x500)

public uint reserved1;

public RASEAPINFO RasEapInfo;

//#endif

}

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct RASDIALPARAMS

{

public int dwSize;

public string szEntryName;

public string szPhoneNumber;

public string szCallbackNumber;

public string szUserName;

public string szPassword;

public string szDomain;

//#if (WINVER >= 0x401)

public int dwSubEntry;

public uint dwCallbackId;

//#endif

}

/// <summary>

/// Summary description for RAS.

/// </summary>

public class RAS

{

[DllImport("Rasapi32.dll", EntryPoint="RasEnumConnectionsA",
SetLastError=true)]

internal static extern int API_RasEnumConnections

(

ref RASCONN lprasconn, // buffer to receive connections data

ref int lpcb, // size in bytes of buffer

ref int lpcConnections // number of connections written to buffer

);

[DllImport("Rasapi32.dll", EntryPoint="RasGetConnectStatus",
SetLastError=true)]

internal static extern int API_RasGetConnectStatus

(

IntPtr hrasconn, // handle to RAS connection of interest

ref RASCONNSTATUS lprasconnstatus

// buffer to receive status data

);

[DllImport("Rasapi32.dll", EntryPoint="RasDial", SetLastError=true)]

internal static extern int API_RasDial

(

ref RASDIALEXTENSIONS lpRasDialExtensions,

// pointer to function extensions data

ref string lpszPhonebook, // pointer to full path and file

// name of phone-book file

ref RASDIALPARAMS lpRasDialParams,

// pointer to calling parameters data

int dwNotifierType, // specifies type of RasDial event handler

ref IntPtr lpvNotifier, // specifies a handler for RasDial events

ref IntPtr lphRasConn // pointer to variable to receive

// connection handle

);

public RAS()

{

//

// TODO: Add constructor logic here

//

}

}

}
kuangren 2003-09-29
  • 打赏
  • 举报
回复
呵呵,这个不是很懂,帮你up好了~
kuangsha007 2003-09-29
  • 打赏
  • 举报
回复
关注,帮你顶
daou101 2003-09-29
  • 打赏
  • 举报
回复
建议把你的问题发给 思归 
gz
冷月孤峰 2003-09-29
  • 打赏
  • 举报
回复
Mark
changezhong 2003-09-29
  • 打赏
  • 举报
回复
gz
noahart 2003-09-29
  • 打赏
  • 举报
回复
up~~~~~~~~

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧