2

I am trying to develop software that connects to a web-server through a network; everything works fine until trying to use a dongle to protect my software. My dongle has some network feature and it's API work under network infrastructure. wWhen I added the Dongle checking code to my program I got this error:

"Either the application has not called WSAStartup, or WSAStartup failed"  

I put the block of code which throw the exception. The scenario which I got the exception is; I logged in into the program (everything works fine) then plug out the dongle and then the program stop and ask for dongle and I plug in the dongle again and try to log in but I got a exception on line

response = (HttpWebResponse)request.GetResponse();

DongleService unikey = new DongleService();

                checkDongle = unikey.isConnectedNow();

                if (checkDongle)
                {
                    isPass = true;
                    this.username = txtbxUser.Text;
                    this.pass = txtbxPass.Text;
                    this.IP = combobxServer.Text;
                    string uri = @"https://" + combobxServer.Text + ":5002num_events=1";
                    request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Proxy = null;
                    request.Credentials = new NetworkCredential(this.username, this.pass);
                    ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
                    response = (HttpWebResponse)request.GetResponse();

                    Properties.Settings.Default.User = txtbxUser.Text;
                    int index = _servers.FindIndex(p => p == combobxServer.Text);
                    if (index == -1)
                    {
                        _servers.Add(combobxServer.Text);
                        Config_Save.SaveServers(_servers);
                        _servers = Config_Save.LoadServers();
                    }

                    Properties.Settings.Default.Server = combobxServer.Text;
                    // also save the password
                    if (checkBox1.CheckState.ToString() == "Checked")
                        Properties.Settings.Default.Pass = txtbxPass.Text;

                    Properties.Settings.Default.settingLoginUsername = this.username;
                    Properties.Settings.Default.settingLoginPassword = this.pass;
                    Properties.Settings.Default.settingLoginPort = "5002";
                    Properties.Settings.Default.settingLoginIP = this.IP;
                    Properties.Settings.Default.isLogin = "guest";

                    Properties.Settings.Default.Save();
                    response.Close();
                    request.Abort();
                    this.isPass = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Please Insert Correct Dongle!", "Dongle Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
MD XF
  • 7,860
  • 7
  • 40
  • 71
Am1rr3zA
  • 7,115
  • 18
  • 83
  • 125
  • I believe this is a problem with UniKey dongles. I'm sort of having a similar problems. I've noticed that when I pull out the dongle my application cannot work with sockets any more. I've submitted a ticket for this to esecutech. If any thing comes up I'll post here. – atoMerz Jun 08 '14 at 10:04

1 Answers1

0

WSAStartup is a first function from socket library, which are executed on start working with net. You may import

 [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
 static extern Int32 WSAGetLastError();

and when exception are raised then execute WSAGetLastError and look at error codes from here. Hope it will help you. In the specific case where you get a Win32Exception, you may use NativeErrorCode from exception as written in comments by @Patrick.

Community
  • 1
  • 1
FLCL
  • 2,445
  • 2
  • 24
  • 45
  • 2
    I believe the [NativeErrorCode](http://msdn.microsoft.com/en-us/library/system.componentmodel.win32exception.nativeerrorcode.aspx) shows the same code, so you don't have to import any native dll.. – Patrick Sep 22 '12 at 09:00