Search code examples
visual-studio-2010boostc++-cliclr

boost library inside c++/cli.. exit with "code 0xC0020001: The string binding is invalid"


I am using the boost library for getting the current system time and my code works but visualt studio 2010 exits after the program.the debugger breaks while trying to free the non existing pointer. I know this is because of the boost native code.Since there is no error if I comment the boost portion of code.

Till now I tried using the #pragma as explained in MSDN but with no success.Can someone provide me some suggestions.? (I also tried GetSystemTime function to get the time but i cannot get the microsecond detail like boost.)

MY Code

#pragma managed(push, off)
void GetSystemDateTime(SDateTime& stimeblock);
#pragma managed(pop)

int main()
{

c++/cli code

SDateTime stimestruct[1];
//call to the function having the boost code..
GetSystemDateTime(stimestruct[0]);

}

Function Definition

 #pragma managed(push, off)
  void GetSystemDateTime(SDateTime& timeblock)
    {

   // SYSTEMTIME time;
   // GetSystemTime(&time);
   // WORD millis = (time.wSecond * 1000) + time.wMilliseconds;

    boost::posix_time::ptime now  = boost::posix_time::microsec_clock::local_time();
    std::tm pt_tm = to_tm(now);
    std::cout <<  now << std::endl;
    //std::cout <<  time.wYear<< time.wMonth<<time.wDay //<<time.wHour<<time.wMinute<<time.wSecond<<time.wMilliseconds << std::endl;
    std::string timestring =  to_iso_string(now);
    std::string sYear  = timestring.substr (0,4);
    std::string sMonth = timestring.substr (4,2);
    std::string sDay   = timestring.substr (6,2);
    std::string sHour  = timestring.substr (9,2);
    std::string sMinute = timestring.substr (11,2);
    std::string sSecond = timestring.substr (13,2);
    std::string sUSecond = timestring.substr (16);

    istringstream isYear(sYear);
    istringstream isMonth(sMonth);
    istringstream isDay(sDay);
    istringstream isHour(sHour);
    istringstream isMinute(sMinute);
    istringstream isSec(sSecond);
    istringstream isUSec(sUSecond);
    // use is like an input stream
    int iYear,iMonth,iDay,iHour,iMinute,iSecond,iUSecond;
    isYear >> iYear;
    isMonth >>iMonth;
    isDay >>iDay;
    isHour >>iHour;
    isMinute >>iMinute;
    isSec >>iSecond;
    isUSec >>iUSecond;


    timeblock.uiYear = iYear;
    timeblock.usiMonth = time.wMonth;
    timeblock.usiDay = time.wDay;
    timeblock.usiHour = time.wHour;
    timeblock.usiMinute = time.wMinute;
    timeblock.usiSec = time.wSecond;

    timeblock.udiUSec = time.wMilliseconds;

    // Display version information

    }

Solution

  • I've seen this error caused by using a static variable in native code in a C++/CLI assembly.

    The only workaround I found was to remove the static variable, e.g., by moving it to class or file scope.

    However, if this static variable is in the boost code, doing so may not be easy/possible. In that case, you could create a separate C++ file that's compiled without /clr, use the boost function in that file, and link that into your C++/CLI assembly.

    This error seems to be caused by the compiler generating incorrect code. I filed a bug with Microsoft, which was closed "won't fix", but the compiler team gave some other workarounds in their response.