S
sundmah
Hello,
I am developing a dot NET CF (C# ) program to establish a GPRS
connection , using RAS on the WM5.0 mobile device. So the program has
to take three parameters, User Name, password, domain (if any) and
must the GPRS modem connection. This is the program I came up
with....our factory loaded WM 5.0 comes with a default entries on the
MyISP network called 'G20 GPRS' which is the name of the connection
with a phone number (Access POint name) of 'proxy'. This is the C++
program my friend came up with. I am putting both the C++ program and
the C# (program I came up with)..somehow my C# program does not call
the RASGetEntryProperties structure...Heres the C++ program
C++:
// RasTest.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ras.h"
#define RIL_ROOT (HKEY_LOCAL_MACHINE)
#define RIL_KEY (TEXT("Comm\\ConnMgr\\Providers\
\{7C4B7A38-5FF7-4bc1-80F6-5DA7870BB1AA}\\Connections\\MAS CA"))
#define RIL_REQ_PE (TEXT("RequirePw"))
#define RIL_ENABLED (TEXT("Enabled"))
#define RIL_ENT_TYPE (TEXT("EntryType"))
#define RIL_DES_ID (TEXT("DestId"))
bool GetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, DWORD *const pdwValue);
bool SetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const DWORD dwValue);
bool SetRegistrySZ(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const TCHAR *const psValue);
int CreateRasEntry (LPTSTR lpszName);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
CreateRasEntry(_T("MAS CA"));
return 0;
}
int CreateRasEntry (LPTSTR lpszName)
{
DWORD dwSize,
dwError;
TCHAR szError[100];
BYTE bSP[512];
RASENTRY RasEntry;
RASDIALPARAMS RasDialParams;
//HRASCONN g_hRasConn = NULL;
/*
// Validate the format of a connection entry name.
if (dwError = RasValidateEntryName (NULL, lpszName))
{
wsprintf (szError, TEXT("Unable to validate entry name.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
*/
// Initialize the RASENTRY structure.
memset (&RasEntry, 0, sizeof (RASENTRY));
dwSize = sizeof (RASENTRY);
RasEntry.dwSize = dwSize;
unsigned long temp = 512;
// Retrieve the entry properties.
if (dwError = RasGetEntryProperties (NULL, TEXT("G20 GPRS"),
&RasEntry, &dwSize, bSP, &temp))
{
wsprintf (szError, TEXT("Unable to read default entry properties.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
//Access Point Name
bSP[88] = 'M';
bSP[90] = 'A';
bSP[92] = 'S';
bSP[94] = 'C';
bSP[96] = 'A';
bSP[98] = 'N';
bSP[100] = 'A';
bSP[102] = 'D';
bSP[104] = 'A';
bSP[106] = '.';
bSP[108] = 'C';
bSP[110] = 'O';
bSP[112] = 'M';
//Name of the dialer
RasEntry.szLocalPhoneNumber[6]= 'm';
RasEntry.szLocalPhoneNumber[7]= 'a';
RasEntry.szLocalPhoneNumber[8]= 's';
RasEntry.szLocalPhoneNumber[9]= 'c';
RasEntry.szLocalPhoneNumber[10]='a';
RasEntry.szLocalPhoneNumber[11]='n';
RasEntry.szLocalPhoneNumber[12]='a';
RasEntry.szLocalPhoneNumber[13]='d';
RasEntry.szLocalPhoneNumber[14]='a';
RasEntry.szLocalPhoneNumber[15]='.';
RasEntry.szLocalPhoneNumber[16]='c';
RasEntry.szLocalPhoneNumber[17]='o';
RasEntry.szLocalPhoneNumber[18]='m';
// Insert code here to fill the RASENTRY structure.
// ...
// Create a new phone-book entry.
if (dwError = RasSetEntryProperties (NULL, lpszName,
&RasEntry, sizeof (RASENTRY), bSP, temp))
{
wsprintf (szError, TEXT("Unable to create the phonebook entry.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
// Initialize the RASDIALPARAMS structure.
memset (&RasDialParams, 0, sizeof (RASDIALPARAMS));
RasDialParams.dwSize = sizeof (RASDIALPARAMS);
_tcscpy (RasDialParams.szEntryName, lpszName);
// Insert code here to fill up the RASDIALPARAMS structure.
wcscpy(RasDialParams.szCallbackNumber, _T(""));//callback option
disabled
wcscpy (RasDialParams.szUserName, _T("MASCANADA")); //This is
optional
wcscpy (RasDialParams.szPassword, _T("MASCAN35")); //This is optional
// Change the connection data.
if (dwError = RasSetEntryDialParams (NULL, &RasDialParams, FALSE))
{
wsprintf (szError, TEXT("Unable to set the connection information.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
unsigned long val1 = 0;
unsigned long val2 = 1;
unsigned long val3 = 2;
SetRegistryDWORD(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_REQ_PE, val1);
SetRegistryDWORD(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_ENABLED, val2);
SetRegistryDWORD(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_ENT_TYPE, val3);
SetRegistrySZ(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_DES_ID,
_T("{ADB0B001-10B5-3F39-27C6-9742E785FCD4}"));
return TRUE;
}
// Registry get and set helper-function implementations
bool GetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, DWORD *const pdwValue)
{
bool bSuccess = false;
HKEY hKey;
DWORD dwDWORD;
DWORD dwDisposition;
if((0 != pdwValue) && (ERROR_SUCCESS == RegCreateKeyEx(hRoot,
psKeyName, 0, TEXT(""), 0, KEY_READ, 0, &hKey, &dwDisposition)))
{
DWORD dwValueSize = sizeof(dwDWORD);
bSuccess = ((ERROR_SUCCESS == RegQueryValueEx(hKey, psValueName, 0,
0, (BYTE*)(&dwDWORD), &dwValueSize)) && (sizeof(dwDWORD) ==
dwValueSize));
RegCloseKey(hKey);
if(bSuccess)
{
*pdwValue = dwDWORD;
}
}
return bSuccess;
}
bool SetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const DWORD dwValue)
{
bool bSuccess = false;
HKEY hKey;
DWORD dwDisposition;
if(ERROR_SUCCESS == RegCreateKeyEx(hRoot, psKeyName, 0, TEXT(""), 0,
KEY_WRITE, 0, &hKey, &dwDisposition))
{
bSuccess = (ERROR_SUCCESS == RegSetValueEx(hKey, psValueName, 0,
REG_DWORD, (BYTE*)(&dwValue), sizeof(dwValue)));
RegCloseKey(hKey);
}
return bSuccess;
}
bool SetRegistrySZ(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const TCHAR *const psValue)
{
bool bSuccess = false;
HKEY hKey;
DWORD dwDisposition;
if((0 != psValue) && (ERROR_SUCCESS == RegCreateKeyEx(hRoot,
psKeyName, 0, TEXT(""), 0, KEY_WRITE, 0, &hKey, &dwDisposition)))
{
bSuccess = (ERROR_SUCCESS == RegSetValueEx(hKey, psValueName, 0,
REG_SZ, (const BYTE* const)psValue, (_tcslen(psValue)
+1)*sizeof(TCHAR)));
RegCloseKey(hKey);
}
return bSuccess;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ras;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System.Reflection;
namespace TestRas
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
public struct RasFieldSizeConstants
{
public const int RAS_MaxDeviceType = 16;
public const int RAS_MaxPhoneNumber = 128;
public const int RAS_MaxIpAddress = 15;
public const int RAS_MaxIpxAddress = 21;
public const int MAX_PATH = 260;
public const int RAS_MaxEntryName = 20;
public const int RAS_MaxDeviceName = 32;
public const int RAS_MaxCallbackNumber = 48;
public const int RAS_MaxAreaCode = 10;
public const int RAS_MaxPadType = 32;
public const int RAS_MaxX25Address = 200;
public const int RAS_MaxFacilities = 200;
public const int RAS_MaxUserData = 200;
public const int RAS_MaxReplyMessage = 1024;
public const int RAS_MaxDnsSuffix = 256;
public const int UNLEN = 256;
public const int PWLEN = 256;
public const int DNLEN = 15;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASDIALPARAMS
{
public uint dwSize; //=
(uint)Marshal.SizeOf(typeof(RASDIALPARAMS));
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
public string szEntryName;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxPhoneNumber + 1)]
public string szPhoneNumber;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxCallbackNumber + 1)]
public string szCallbackNumber;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.UNLEN + 1)]
public string szUserName;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.PWLEN + 1)]
public string szPassword;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.DNLEN + 1)]
public string szDomain;// = null;
public RASDIALPARAMS(uint adwSize,string
aszEntryName,string aszPhoneNumber,string aszcallbacknumber,string
aszUsername, string aszPassword, string aszdomain )
{
dwSize = 0;
szEntryName = null;// new
string[RasFieldSizeConstants.RAS_MaxEntryName + 1];
szPhoneNumber = null; //new
string[RasFieldSizeConstants.RAS_MaxPhoneNumber + 1];
szCallbackNumber = null;//new
string[RasFieldSizeConstants.RAS_MaxCallbackNumber + 1];
szUserName = null;//new
string[RasFieldSizeConstants.UNLEN + 1];
szPassword = null;//new
string[RasFieldSizeConstants.PWLEN + 1];
szDomain = null;//new
string[RasFieldSizeConstants.DNLEN + 1];
}
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct RASIPADDR
{
byte a;
byte b;
byte c;
byte d;
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct RASENTRY
{
public uint dwSize;
public uint dwfOptions;
public uint dwCountryID;
public uint dwCountryCode;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxAreaCode
+1)]
public string szAreaCode;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxPhoneNumber
+1)]
public string szLocalPhoneNumber;
public uint dwAlternateOffset;
public RASIPADDR ipaddr;
public RASIPADDR ipaddrDns;
public RASIPADDR ipaddrDnsAlt;
public RASIPADDR ipaddrWins;
public RASIPADDR ipaddrWinsAlt;
public uint dwFrameSize;
public uint dwfNetProtocols;
public uint dwFramingProtocol;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PATH
public string szScript;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PATH
public string szAutodialDll;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PATH
public string szAutodialFunc;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxDeviceType
+1)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxDeviceName
+1)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxPadType
+1)]//MAX_PATH
public string szX25PadType;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxX25Address
+1)]//MAX_PATH
public string szX25Address;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxFacilities
+1)]//MAX_PATH
public string szX25Facilities;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxUserData
+1)]//MAX_PATH
public string szX25UserData;
public uint dwChannels;
public uint dwReserved1;
public uint dwReserved2;
public uint dwCustomAuthKey;
}
/****Dll Imports***/
[DllImport("coredll.dll")]
public static extern uint RasGetEntryProperties(string
lpszPhoneBook, string szEntry, ref RASENTRY lpEntry, ref uint dwsize,
char[] lpb, ref uint lpdwSize);
[DllImport("coredll.dll")]
public static extern uint RasSetEntryProperties(string
lpszPhoneBook, string szEntry, ref RASENTRY lpEntry, uint dwEntrySize,
char[] lpb, uint dwSize);
[DllImport("coredll.dll")]
public static extern uint RasSetEntryDialParams(string
lpszPhoneBook, ref RASDIALPARAMS lpRasDialParams, bool
fRemovePassword);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern int RasDial(IntPtr pDialExtensions,
string pPhonebook,
char[] pRasDialParam,
uint pNotifierType,
IntPtr pHwnd,
ref IntPtr pRasConn);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern Int32 RasGetEntryDialParams(string
lpszPhoneBook,
string[]
lpRasDialParams,
ref UInt32
lpfPassword);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern int RasHangUp(IntPtr pSession);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern uint RasGetConnectStatus(IntPtr
pSession, byte[] lpRasConnStatus);
public Form1()
{
InitializeComponent();
}
private void createRasEntry(string lpszName)
{
uint dwSize;
char[] bSP = new char[512];
RASENTRY RasEntry;
RasEntry = new RASENTRY();
RASDIALPARAMS RasDialParams;
RasDialParams = new RASDIALPARAMS();
//dwSize = (uint)RasEntry.Size;
dwSize = (uint)Marshal.SizeOf(RasEntry);
RasEntry.dwSize = dwSize;
//dwSize = (uint)RasEntry.dwSize;
uint temp = 512;
MessageBox.Show("Attempting RASGetEntries");
uint dwGetEntry = 0;
dwGetEntry = RasGetEntryProperties("Proxy", "G20 GPRS",
ref RasEntry, ref dwSize, bSP, ref temp);
if (dwGetEntry >= 1)
{
MessageBox.Show("Unable to retrieve information");
}
bSP[88] = 'm';
bSP[90] = 'a';
bSP[92] = 's';
bSP[94] = 'c';
bSP[96] = 'a';
bSP[98] = 'n';
bSP[100] = 'a';
bSP[102] = 'd';
bSP[104] = 'a';
bSP[106] = '.';
bSP[108] = 'c';
bSP[110] = 'o';
bSP[112] = 'm';
RasEntry.szLocalPhoneNumber = "mascanada.com";
/**NOTE: The Below Code doesnt work**/
// //Name of the dialer
// //RasEntry.szLocalPhoneNumber[6] = 'm';
// //RasEntry.szLocalPhoneNumber[7] = 'a';
// //RasEntry.szLocalPhoneNumber[8] = 's';
// //RasEntry.szLocalPhoneNumber[9] = 'c';
// //RasEntry.szLocalPhoneNumber[10] = 'a';
// //RasEntry.szLocalPhoneNumber[11] = 'n';
// //RasEntry.szLocalPhoneNumber[12] = 'a';
// //RasEntry.szLocalPhoneNumber[13] = 'd';
// //RasEntry.szLocalPhoneNumber[14] = 'a';
// //RasEntry.szLocalPhoneNumber[15] = '.';
// //RasEntry.szLocalPhoneNumber[16] = 'c';
// //RasEntry.szLocalPhoneNumber[17] = 'o';
// //RasEntry.szLocalPhoneNumber[18] = 'm';
uint dwError = 0;
dwError = RasSetEntryProperties("", "MASCA", ref RasEntry,
(uint)RasEntry.dwSize, bSP, temp);
if (dwError >= 1)
{
MessageBox.Show("Unable to create phonebook entry");
// return;
}
dwSize = (uint)Marshal.SizeOf(RasDialParams);
RasDialParams.dwSize = (uint)dwSize;
//RasDialParams.dwSize = sizeof(RasDialParams);
RasDialParams.szEntryName = "MASCA";
RasDialParams.szCallbackNumber = "";
RasDialParams.szUserName = "MASCANADA";
RasDialParams.szPassword = "MASCAN35";
uint dwError2 = 0;
dwError2 = RasSetEntryDialParams("", ref RasDialParams,
false);
if (dwError2 >= 1)
{
MessageBox.Show("Unable to Set Params");
// return;
}
MessageBox.Show("Done");
}
private void Form1_Load(object sender, EventArgs e)
{
createRasEntry("MASCA");
RegistryKey hklm = Registry.LocalMachine;
hklm = hklm.OpenSubKey("Comm");
hklm = hklm.OpenSubKey("ConnMgr");
hklm = hklm.OpenSubKey("Providers");
hklm =
hklm.OpenSubKey("{7C4B7A38-5FF7-4bc1-80F6-5DA7870BB1AA}");
hklm = hklm.OpenSubKey("Connections", true);
RegistryKey MASCA = hklm.CreateSubKey("MASCA");
hklm = hklm.OpenSubKey("MASCA", true);
hklm.SetValue("EntryType", 2, RegistryValueKind.DWord);
hklm.SetValue("Enabled", 1, RegistryValueKind.DWord);
hklm.SetValue("RequirePW", 0, RegistryValueKind.DWord);
hklm.SetValue("DestId",
"{ADB0B001-10B5-3F39-27C6-9742E785FCD4}", RegistryValueKind.String);
}
}//End of class Form1
}
Now RASGetEntryProperties always returns error which is not the case
in C++. Where am I going wrong here?
Thanks alot
Mash
I am developing a dot NET CF (C# ) program to establish a GPRS
connection , using RAS on the WM5.0 mobile device. So the program has
to take three parameters, User Name, password, domain (if any) and
must the GPRS modem connection. This is the program I came up
with....our factory loaded WM 5.0 comes with a default entries on the
MyISP network called 'G20 GPRS' which is the name of the connection
with a phone number (Access POint name) of 'proxy'. This is the C++
program my friend came up with. I am putting both the C++ program and
the C# (program I came up with)..somehow my C# program does not call
the RASGetEntryProperties structure...Heres the C++ program
C++:
// RasTest.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ras.h"
#define RIL_ROOT (HKEY_LOCAL_MACHINE)
#define RIL_KEY (TEXT("Comm\\ConnMgr\\Providers\
\{7C4B7A38-5FF7-4bc1-80F6-5DA7870BB1AA}\\Connections\\MAS CA"))
#define RIL_REQ_PE (TEXT("RequirePw"))
#define RIL_ENABLED (TEXT("Enabled"))
#define RIL_ENT_TYPE (TEXT("EntryType"))
#define RIL_DES_ID (TEXT("DestId"))
bool GetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, DWORD *const pdwValue);
bool SetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const DWORD dwValue);
bool SetRegistrySZ(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const TCHAR *const psValue);
int CreateRasEntry (LPTSTR lpszName);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
CreateRasEntry(_T("MAS CA"));
return 0;
}
int CreateRasEntry (LPTSTR lpszName)
{
DWORD dwSize,
dwError;
TCHAR szError[100];
BYTE bSP[512];
RASENTRY RasEntry;
RASDIALPARAMS RasDialParams;
//HRASCONN g_hRasConn = NULL;
/*
// Validate the format of a connection entry name.
if (dwError = RasValidateEntryName (NULL, lpszName))
{
wsprintf (szError, TEXT("Unable to validate entry name.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
*/
// Initialize the RASENTRY structure.
memset (&RasEntry, 0, sizeof (RASENTRY));
dwSize = sizeof (RASENTRY);
RasEntry.dwSize = dwSize;
unsigned long temp = 512;
// Retrieve the entry properties.
if (dwError = RasGetEntryProperties (NULL, TEXT("G20 GPRS"),
&RasEntry, &dwSize, bSP, &temp))
{
wsprintf (szError, TEXT("Unable to read default entry properties.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
//Access Point Name
bSP[88] = 'M';
bSP[90] = 'A';
bSP[92] = 'S';
bSP[94] = 'C';
bSP[96] = 'A';
bSP[98] = 'N';
bSP[100] = 'A';
bSP[102] = 'D';
bSP[104] = 'A';
bSP[106] = '.';
bSP[108] = 'C';
bSP[110] = 'O';
bSP[112] = 'M';
//Name of the dialer
RasEntry.szLocalPhoneNumber[6]= 'm';
RasEntry.szLocalPhoneNumber[7]= 'a';
RasEntry.szLocalPhoneNumber[8]= 's';
RasEntry.szLocalPhoneNumber[9]= 'c';
RasEntry.szLocalPhoneNumber[10]='a';
RasEntry.szLocalPhoneNumber[11]='n';
RasEntry.szLocalPhoneNumber[12]='a';
RasEntry.szLocalPhoneNumber[13]='d';
RasEntry.szLocalPhoneNumber[14]='a';
RasEntry.szLocalPhoneNumber[15]='.';
RasEntry.szLocalPhoneNumber[16]='c';
RasEntry.szLocalPhoneNumber[17]='o';
RasEntry.szLocalPhoneNumber[18]='m';
// Insert code here to fill the RASENTRY structure.
// ...
// Create a new phone-book entry.
if (dwError = RasSetEntryProperties (NULL, lpszName,
&RasEntry, sizeof (RASENTRY), bSP, temp))
{
wsprintf (szError, TEXT("Unable to create the phonebook entry.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
// Initialize the RASDIALPARAMS structure.
memset (&RasDialParams, 0, sizeof (RASDIALPARAMS));
RasDialParams.dwSize = sizeof (RASDIALPARAMS);
_tcscpy (RasDialParams.szEntryName, lpszName);
// Insert code here to fill up the RASDIALPARAMS structure.
wcscpy(RasDialParams.szCallbackNumber, _T(""));//callback option
disabled
wcscpy (RasDialParams.szUserName, _T("MASCANADA")); //This is
optional
wcscpy (RasDialParams.szPassword, _T("MASCAN35")); //This is optional
// Change the connection data.
if (dwError = RasSetEntryDialParams (NULL, &RasDialParams, FALSE))
{
wsprintf (szError, TEXT("Unable to set the connection information.")
TEXT(" Error %ld"), dwError);
return FALSE;
}
unsigned long val1 = 0;
unsigned long val2 = 1;
unsigned long val3 = 2;
SetRegistryDWORD(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_REQ_PE, val1);
SetRegistryDWORD(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_ENABLED, val2);
SetRegistryDWORD(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_ENT_TYPE, val3);
SetRegistrySZ(HKEY_LOCAL_MACHINE, RIL_KEY, RIL_DES_ID,
_T("{ADB0B001-10B5-3F39-27C6-9742E785FCD4}"));
return TRUE;
}
// Registry get and set helper-function implementations
bool GetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, DWORD *const pdwValue)
{
bool bSuccess = false;
HKEY hKey;
DWORD dwDWORD;
DWORD dwDisposition;
if((0 != pdwValue) && (ERROR_SUCCESS == RegCreateKeyEx(hRoot,
psKeyName, 0, TEXT(""), 0, KEY_READ, 0, &hKey, &dwDisposition)))
{
DWORD dwValueSize = sizeof(dwDWORD);
bSuccess = ((ERROR_SUCCESS == RegQueryValueEx(hKey, psValueName, 0,
0, (BYTE*)(&dwDWORD), &dwValueSize)) && (sizeof(dwDWORD) ==
dwValueSize));
RegCloseKey(hKey);
if(bSuccess)
{
*pdwValue = dwDWORD;
}
}
return bSuccess;
}
bool SetRegistryDWORD(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const DWORD dwValue)
{
bool bSuccess = false;
HKEY hKey;
DWORD dwDisposition;
if(ERROR_SUCCESS == RegCreateKeyEx(hRoot, psKeyName, 0, TEXT(""), 0,
KEY_WRITE, 0, &hKey, &dwDisposition))
{
bSuccess = (ERROR_SUCCESS == RegSetValueEx(hKey, psValueName, 0,
REG_DWORD, (BYTE*)(&dwValue), sizeof(dwValue)));
RegCloseKey(hKey);
}
return bSuccess;
}
bool SetRegistrySZ(const HKEY hRoot, const TCHAR* const psKeyName,
const TCHAR* const psValueName, const TCHAR *const psValue)
{
bool bSuccess = false;
HKEY hKey;
DWORD dwDisposition;
if((0 != psValue) && (ERROR_SUCCESS == RegCreateKeyEx(hRoot,
psKeyName, 0, TEXT(""), 0, KEY_WRITE, 0, &hKey, &dwDisposition)))
{
bSuccess = (ERROR_SUCCESS == RegSetValueEx(hKey, psValueName, 0,
REG_SZ, (const BYTE* const)psValue, (_tcslen(psValue)
+1)*sizeof(TCHAR)));
RegCloseKey(hKey);
}
return bSuccess;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ras;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System.Reflection;
namespace TestRas
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
public struct RasFieldSizeConstants
{
public const int RAS_MaxDeviceType = 16;
public const int RAS_MaxPhoneNumber = 128;
public const int RAS_MaxIpAddress = 15;
public const int RAS_MaxIpxAddress = 21;
public const int MAX_PATH = 260;
public const int RAS_MaxEntryName = 20;
public const int RAS_MaxDeviceName = 32;
public const int RAS_MaxCallbackNumber = 48;
public const int RAS_MaxAreaCode = 10;
public const int RAS_MaxPadType = 32;
public const int RAS_MaxX25Address = 200;
public const int RAS_MaxFacilities = 200;
public const int RAS_MaxUserData = 200;
public const int RAS_MaxReplyMessage = 1024;
public const int RAS_MaxDnsSuffix = 256;
public const int UNLEN = 256;
public const int PWLEN = 256;
public const int DNLEN = 15;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASDIALPARAMS
{
public uint dwSize; //=
(uint)Marshal.SizeOf(typeof(RASDIALPARAMS));
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
public string szEntryName;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxPhoneNumber + 1)]
public string szPhoneNumber;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxCallbackNumber + 1)]
public string szCallbackNumber;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.UNLEN + 1)]
public string szUserName;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.PWLEN + 1)]
public string szPassword;// = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.DNLEN + 1)]
public string szDomain;// = null;
public RASDIALPARAMS(uint adwSize,string
aszEntryName,string aszPhoneNumber,string aszcallbacknumber,string
aszUsername, string aszPassword, string aszdomain )
{
dwSize = 0;
szEntryName = null;// new
string[RasFieldSizeConstants.RAS_MaxEntryName + 1];
szPhoneNumber = null; //new
string[RasFieldSizeConstants.RAS_MaxPhoneNumber + 1];
szCallbackNumber = null;//new
string[RasFieldSizeConstants.RAS_MaxCallbackNumber + 1];
szUserName = null;//new
string[RasFieldSizeConstants.UNLEN + 1];
szPassword = null;//new
string[RasFieldSizeConstants.PWLEN + 1];
szDomain = null;//new
string[RasFieldSizeConstants.DNLEN + 1];
}
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct RASIPADDR
{
byte a;
byte b;
byte c;
byte d;
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct RASENTRY
{
public uint dwSize;
public uint dwfOptions;
public uint dwCountryID;
public uint dwCountryCode;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxAreaCode
+1)]
public string szAreaCode;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxPhoneNumber
+1)]
public string szLocalPhoneNumber;
public uint dwAlternateOffset;
public RASIPADDR ipaddr;
public RASIPADDR ipaddrDns;
public RASIPADDR ipaddrDnsAlt;
public RASIPADDR ipaddrWins;
public RASIPADDR ipaddrWinsAlt;
public uint dwFrameSize;
public uint dwfNetProtocols;
public uint dwFramingProtocol;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PATH
public string szScript;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PATH
public string szAutodialDll;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PATH
public string szAutodialFunc;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxDeviceType
+1)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxDeviceName
+1)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxPadType
+1)]//MAX_PATH
public string szX25PadType;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxX25Address
+1)]//MAX_PATH
public string szX25Address;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxFacilities
+1)]//MAX_PATH
public string szX25Facilities;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxUserData
+1)]//MAX_PATH
public string szX25UserData;
public uint dwChannels;
public uint dwReserved1;
public uint dwReserved2;
public uint dwCustomAuthKey;
}
/****Dll Imports***/
[DllImport("coredll.dll")]
public static extern uint RasGetEntryProperties(string
lpszPhoneBook, string szEntry, ref RASENTRY lpEntry, ref uint dwsize,
char[] lpb, ref uint lpdwSize);
[DllImport("coredll.dll")]
public static extern uint RasSetEntryProperties(string
lpszPhoneBook, string szEntry, ref RASENTRY lpEntry, uint dwEntrySize,
char[] lpb, uint dwSize);
[DllImport("coredll.dll")]
public static extern uint RasSetEntryDialParams(string
lpszPhoneBook, ref RASDIALPARAMS lpRasDialParams, bool
fRemovePassword);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern int RasDial(IntPtr pDialExtensions,
string pPhonebook,
char[] pRasDialParam,
uint pNotifierType,
IntPtr pHwnd,
ref IntPtr pRasConn);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern Int32 RasGetEntryDialParams(string
lpszPhoneBook,
string[]
lpRasDialParams,
ref UInt32
lpfPassword);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern int RasHangUp(IntPtr pSession);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern uint RasGetConnectStatus(IntPtr
pSession, byte[] lpRasConnStatus);
public Form1()
{
InitializeComponent();
}
private void createRasEntry(string lpszName)
{
uint dwSize;
char[] bSP = new char[512];
RASENTRY RasEntry;
RasEntry = new RASENTRY();
RASDIALPARAMS RasDialParams;
RasDialParams = new RASDIALPARAMS();
//dwSize = (uint)RasEntry.Size;
dwSize = (uint)Marshal.SizeOf(RasEntry);
RasEntry.dwSize = dwSize;
//dwSize = (uint)RasEntry.dwSize;
uint temp = 512;
MessageBox.Show("Attempting RASGetEntries");
uint dwGetEntry = 0;
dwGetEntry = RasGetEntryProperties("Proxy", "G20 GPRS",
ref RasEntry, ref dwSize, bSP, ref temp);
if (dwGetEntry >= 1)
{
MessageBox.Show("Unable to retrieve information");
}
bSP[88] = 'm';
bSP[90] = 'a';
bSP[92] = 's';
bSP[94] = 'c';
bSP[96] = 'a';
bSP[98] = 'n';
bSP[100] = 'a';
bSP[102] = 'd';
bSP[104] = 'a';
bSP[106] = '.';
bSP[108] = 'c';
bSP[110] = 'o';
bSP[112] = 'm';
RasEntry.szLocalPhoneNumber = "mascanada.com";
/**NOTE: The Below Code doesnt work**/
// //Name of the dialer
// //RasEntry.szLocalPhoneNumber[6] = 'm';
// //RasEntry.szLocalPhoneNumber[7] = 'a';
// //RasEntry.szLocalPhoneNumber[8] = 's';
// //RasEntry.szLocalPhoneNumber[9] = 'c';
// //RasEntry.szLocalPhoneNumber[10] = 'a';
// //RasEntry.szLocalPhoneNumber[11] = 'n';
// //RasEntry.szLocalPhoneNumber[12] = 'a';
// //RasEntry.szLocalPhoneNumber[13] = 'd';
// //RasEntry.szLocalPhoneNumber[14] = 'a';
// //RasEntry.szLocalPhoneNumber[15] = '.';
// //RasEntry.szLocalPhoneNumber[16] = 'c';
// //RasEntry.szLocalPhoneNumber[17] = 'o';
// //RasEntry.szLocalPhoneNumber[18] = 'm';
uint dwError = 0;
dwError = RasSetEntryProperties("", "MASCA", ref RasEntry,
(uint)RasEntry.dwSize, bSP, temp);
if (dwError >= 1)
{
MessageBox.Show("Unable to create phonebook entry");
// return;
}
dwSize = (uint)Marshal.SizeOf(RasDialParams);
RasDialParams.dwSize = (uint)dwSize;
//RasDialParams.dwSize = sizeof(RasDialParams);
RasDialParams.szEntryName = "MASCA";
RasDialParams.szCallbackNumber = "";
RasDialParams.szUserName = "MASCANADA";
RasDialParams.szPassword = "MASCAN35";
uint dwError2 = 0;
dwError2 = RasSetEntryDialParams("", ref RasDialParams,
false);
if (dwError2 >= 1)
{
MessageBox.Show("Unable to Set Params");
// return;
}
MessageBox.Show("Done");
}
private void Form1_Load(object sender, EventArgs e)
{
createRasEntry("MASCA");
RegistryKey hklm = Registry.LocalMachine;
hklm = hklm.OpenSubKey("Comm");
hklm = hklm.OpenSubKey("ConnMgr");
hklm = hklm.OpenSubKey("Providers");
hklm =
hklm.OpenSubKey("{7C4B7A38-5FF7-4bc1-80F6-5DA7870BB1AA}");
hklm = hklm.OpenSubKey("Connections", true);
RegistryKey MASCA = hklm.CreateSubKey("MASCA");
hklm = hklm.OpenSubKey("MASCA", true);
hklm.SetValue("EntryType", 2, RegistryValueKind.DWord);
hklm.SetValue("Enabled", 1, RegistryValueKind.DWord);
hklm.SetValue("RequirePW", 0, RegistryValueKind.DWord);
hklm.SetValue("DestId",
"{ADB0B001-10B5-3F39-27C6-9742E785FCD4}", RegistryValueKind.String);
}
}//End of class Form1
}
Now RASGetEntryProperties always returns error which is not the case
in C++. Where am I going wrong here?
Thanks alot
Mash