Question

I am working on a bigger project, which part is to create software for Motorola MC9596 scanner, but I am stuck with datatransmission trough GPRS mobile connection using FTP server.

For this purpose I am using OpenNETCF.

The problem is that I cannot dial connection with entry I created. That means there are 2 entries in device's phonebook, test1 - generated by device, when I configured connection on device manually and test2, which was created programatically using:

private const string CONNAME = "test2";
private const string PHONENR = "~GPRS!xxx.xxx-xxxxxxxx.eu";
private const string USER = "xx";
private const string PWD = "xx";
private const string DEVICE_TYPE = "modem";
private const string DEVICE_NAME = "Cellular Line";

private void createConnectionEntry()
{
    RasEntry rasEnt = new RasEntry()
    {
        Name = CONNAME,
        CountryCode = 0,
        AreaCode = "",
        PhoneNumber = PHONENR,
        DeviceName = DEVICE_NAME,
        DeviceType = DEVICE_TYPE,
        IPAddress = "0.0.0.0",
        IPAddressDns = "0.0.0.0"
    };

    /*rasEnt.Options |= (int)ConnectionOptions.UseCountryAndAreaCodes;
    rasEnt.Options |= (int)ConnectionOptions.UseLogonCredentials;*/
    rasEnt.Options = 4194304;

    RasDialParams dialParams = new RasDialParams()
    {
        UserName = USER,
        Password = PWD,
    };

    cEntry = Ras.CreateEntry(rasEnt, dialParams);
}

Note "rasEnt.Options = 4194304", what was hardcoded, to have exact copy of settings generated by device by configuring connection manually. Strange is, that if I compare 2 entries in debug mode, both are equal - that means all properties are equal, the only difference is Name. I am sure about this, used also reflection, to compare objects.

I dial connection with:

RasError re = cEntry.Dial(false, new RasDialParams(CONNAME, USER, PWD));

In case Test1 I get "success", in case of Test2 "unknown" error.

Could you please help me with that nasty problem?

Was it helpful?

Solution

For now I ended with adding necessary registry entries manually - just checked difference in registry before and after creating connection. Not a clean solution, but did not find other one. It seems to be working stable, I can dial connection created this way. I will see, if it is OK in productive phase.

OTHER TIPS

The main issue is to dial through code: While i was trying i faced other issues. Though above code from Hogo, helped a lot but there are few tips that would be useful for hassle free coding:

First of all the following tips requires you dial your connection manually and test your internet by running any website on your internet explorer. If internet is working then you are good to go dialing it through code.

To do it manually:

Make a new connection named "GPRS" using "Network and Dial up Connections"(use this link: http://www.e-consystems.com/gprs.asp). If you are using airtel SIM change baudarate from 19200 to 115200. Connect it and check if internet is working.

Dialing through code:

  1. When you create a connection named "GPRS" manually, 3 registers are made under GPRS folder. (HKEY_CURRENT_USER\Comm\RasBook\GPRS) . these folders cannot be viewed normally. Requires application to be installed in PDA that read registers.There is no need to see whats inside, if you do then download them according to the windows versions.

  2. Out of 3 registers created only two of them are relevant (DevCfg and Entry). Once you have dialed and connected to internet manually, copy data from DevCfg and Entry registers to a text file (DevCfg.txt and Entry.txt) so that later you could copy back these values. To copy to/from registers to text file use: RegQueryValueEx(...), RegOpenKeyEx(..),RegSetValueEx(...) and other relevant functions (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx)

For example: Read Register and Write to text file:

public static bool ReadRegString(IntPtr hKey, string lpszSubKey, string lpValueName)
    {
        try
        {
            string str = "";
            byte[] lpData = new byte[684];
            uint lpcbValue = 684;
            uint dwType = Utils.REG_BINARY;
            IntPtr phkResult = new IntPtr();
            Utils.RegOpenKeyEx(hKey, lpszSubKey, 0, 0, ref phkResult);
            if (phkResult != IntPtr.Zero)
            {
                int x = Utils.RegQueryValueEx(phkResult, lpValueName, 0, ref dwType, lpData, ref lpcbValue);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 684; i++)
                {
                    if (i != 683)
                        sb.Append(lpData[i].ToString() + "|");
                    else
                        sb.Append(lpData[i].ToString());
                }
                using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(filePath))
                {
                    outfile.Write(sb.ToString());
                }
                Utils.RegCloseKey(phkResult);
        }
     }

if (Utils.ReadRegString(Utils.HKEY_CURRENT_USER, @"Comm\RasBook\GPRS", "DevCfg"))
        {
            textBoxSysVers.Text = "Succeeded Make Text File.";

        }
  1. Now to dial through code make we use of RasEntry( add neccessary libraries ; check answer of Hogo in Making GPRS Connection programmatically using C# for windows CE 5.0?)

a) create RasEntry object

b) Copy data from text file created "Entry.txt and DevCfg.txt" to registers Entry and DevCfg repectively.

c) Dial RasError re = cEntry.Dial(...)

For exmaple:

 private const string CONNAME = "GPRS";
    private const string PHONENR = "*99#";
    private const string USER = "xx";
    private const string PWD = "xx";
    private const string DEVICE_TYPE = "modem";
    private const string DEVICE_NAME = "Cellular Line";

 { RasEntry cEntry = new RasEntry()
    {
        Name = CONNAME,
        CountryCode = 91,
        AreaCode = "120",
        PhoneNumber = PHONENR,
        DeviceName = DEVICE_NAME,
        DeviceType = DEVICE_TYPE,
        IPAddress = "0.0.0.0",
        IPAddressDns = "0.0.0.0",Options=4194304
    };

        RasDialParams dialParams = new RasDialParams()
        {
            UserName = USER,
            Password = PWD,
            EntryName = CONNAME,
            Domain = " "
        }

        if (Utils.WriteRegValue(Utils.HKEY_CURRENT_USER, @"Comm\RasBook\GPRS", "DevCfg","Entry",3))
        {
            RasError re = cEntry.Dial(false, new RasDialParams(CONNAME, USER, PWD));

            RasError rs = re;
            textBoxInfo.Text = re.ToString() + " : Dial Status";
            if(rs.ToString()=="Success")
            textBoxInfo.Text = cEntry.Status.State.ToString() + " : Dial Status";
        }
  }

public static Boolean WriteRegValue(IntPtr hKey, string lpszSubKey, string lpValueName1, string lpValueName1,uint dwType)
    {
        int iOper = 1;
        filePath = @"`enter code here`\DevCfg.txt";
        IntPtr phkResult = new IntPtr();
        Utils.RegOpenKeyEx(hKey, lpszSubKey, 0, 0, ref phkResult);
        if (phkResult == IntPtr.Zero)
        {
            int iSecurity = 0;
            int dwDisp = 0;
            RegCreateKeyEx(hKey, lpszSubKey, 0, null, 0, 0, ref iSecurity, ref phkResult, ref dwDisp);
        }
        if (phkResult != IntPtr.Zero)
        {
            byte[] bytes = new byte[684];
            string[] text = new string[684];
            using (System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath, Encoding.UTF8))
            {
                text = streamReader.ReadToEnd().Split('|');
            }
            for (int i = 0; i < 684; i++)
            {
                bytes[i] = Convert.ToByte(Convert.ToInt32(text[i]));
            }
            iOper = Utils.RegSetValueEx(phkResult, lpValueName1, 0, dwType, bytes, (uint)bytes.Length);
            Utils.RegCloseKey(phkResult);

} }

// SImilary do it from lpValueName2

}}

The difference between hogo's code and this is that RasEntry is not getting created here. It is fed from Registers. I faced difficulties while creating object and hence the suggestion. Hope it helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top