Link to home
Start Free TrialLog in
Avatar of glenh
glenh

asked on

Assertion Fault on CIPAddressCtrl::SetAddress()

This may not be exactly the best approach to converting a string to a unsigned long for the SetAddress method but its the best I could come up with due to my lack of knowledge on bit conversions.

For some reason I am getting an assertion fault everytime I call SetAddress() even though it appears my conversion worked just fine.  Here is a copy of the function that does the conversion.  Is there something else I am possibly missing?

unsigned long EQLP_Utilities::strtodw(string ip) {
   unsigned long ipaddress = 0;
   unsigned long lbyte1, lbyte2, lbyte3, lbyte4;
   string sbyte1, sbyte2, sbyte3, sbyte4;
   string combinedip;
   int patternptr = 0;
   int startptr = 0;
   int length = 0;

   patternptr = ip.find('.', startptr);
   sbyte1 = ip.substr(startptr,patternptr);
   lbyte1 = atoi(sbyte1.c_str());
   bitset<8> byte1(lbyte1);
   startptr = patternptr + 1;

   patternptr = ip.find('.', startptr);
   sbyte2 = ip.substr(startptr,patternptr);
   lbyte2 = atoi(sbyte2.c_str());
   bitset<8> byte2(lbyte2);
   startptr = patternptr + 1;

   patternptr = ip.find('.', startptr);
   sbyte3 = ip.substr(startptr,patternptr);
   lbyte3 = atoi(sbyte3.c_str());
   bitset<8> byte3(lbyte3);
   startptr = patternptr + 1;

   patternptr = ip.find('.', startptr);
   sbyte4 = ip.substr(startptr,patternptr);
   lbyte4 = atoi(sbyte4.c_str());
   bitset<8> byte4(lbyte4);

   combinedip.assign(byte1.to_string());
   combinedip.append(byte2.to_string());
   combinedip.append(byte3.to_string());
   combinedip.append(byte4.to_string());
   bitset<32> finalbits(combinedip);
   ipaddress = finalbits.to_ulong();

   return ipaddress;
}
Avatar of jkr
jkr
Flag of Germany image

Why don't you use "WSAStringToAddress()"? It was designed for that very purpose.

If you would like to use a simpler method, the following will do the job for you:

/* convert address string to value */
    int iLen = sizeof ( SOCKADDR_IN);
    char* pszIP = "192.168.0.1";
    SOCKADDR_IN addr;
    addr.sin_family = AF_INET;
    nRet = WSAStringToAddress (
      pszIP,                    /* address string */
      AF_INET,                  /* address family */
      NULL,                     /* protocol info structure */
      (LPSOCKADDR)&addr,        /* socket address string */
      &iLen);                   /* length of socket structure */

     unsigned long ulIP = addr.in_addr.S_addr;
Avatar of glenh
glenh

ASKER

Thanks jkr.  I am still not 100% familiar with MFC and still learning.  This is my first major go at it as I normally program mainly in MOTIF, Java and C++.

Go ahead and post as your answer and will award ya the points.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial