Thursday, March 24, 2011

Error: CLR object cannot be marshaled to Microsoft Dynamics anytype

I was hitting a problem with marshaling a string from System.String to str datatype (X++). The error was 'CLR object cannot be marshaled to Microsoft Dynamics anytype' when I tried to convert them using the code below.  This is how I fixed it. Hope it helps you out...

System.String clrString;
str                  axString

clrString = Microsoft.Win32.Registry::GetValue(XXXX, XXXX, XXXX);

// This way does not work in AX 2009 despite what you read online
// axString = clrString;

// This is the way the marshaling needs to occur
regValue = System.Convert::ToString(clrString);


  1. Can u please help me out here, exactly where the above code can be taken.

  2. Happy to help but I'm confused with the question. The code should be able to be used anywhere depending on purpose. I encountered this while creating an automated analysis for performance. Not sure where, or if, this code even exists in base AX code. Worth running a find on the AOT if you're curious.

    I would actually recommend you copy and paste the code into an AX job and confirm the two ways operate the way I described above.

    If you are seeing this in a specific area of AX code, please feel free to post the specific questions. I want to make sure everyone sees the threads in case it helps them out too.

    I can test environments ranging from AX 3.0 SP4 to the latest release of AX 2012 so there shouldn't be a problem there.

  3. I think I understand what your asking now that its happy hour... Here is how the code is used in application with AX 2009 variables. I've used this part of code countless times in several different environments. Your variables will be different.

    #define.AOSRegistryPath(@'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dynamics Server\5.0')
    str axAOSName = "02"; // Your AOS Name here
    str axConfigName = "DEV"; // Your Config Name here
    str axRegLocation;
    str axRegAXName = "application"; // Registry object name in Registry path
    str contents;

    axRegLocation = #AOSregistryPath + '\\' + axAOSName + '\\' + axConfigName;

    contents = Microsoft.Win32.Registry::GetValue(axRegLocation, axRegAXName, null);

  4. I tried to run the code in the job, but it is throwing the same error.

    Error: CLR object cannot be marshaled to Microsoft Dynamics anytype

  5. Sathish: if I can guess, you are working in AX 2012. This code was designed and used in AX 2009 so I know this post is solid for 2009. I tested the code in AX 2012 and am receiving the marshaling error for both methods same as you.

    It looks like 2009 and 2012 are different in regards to allowing marshaling on the line:
    regValue = System.Convert::ToString(clrString)

    To everyone: That being said, the above will work for 2009 but not 2012. Below is a job that works for marshaling in AX 2012.

    // December 20, 2011 - DAX Dude
    static void Job1(Args _args)
    #define.AOSRegistryPath(@'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dynamics Server\6.0')
    str axAOSName = "01"; // Your AOS Name here
    str axRegLocation;
    str axRegAXName = "InstanceName"; // Registry object name in Registry path
    //str contents;
    System.String netContents;
    System.Object netObject = new System.Object();
    str xppContents

    axRegLocation = #AOSregistryPath + '\\' + axAOSName;

    //Simple marshaling test...
    netContents = 'Test Marshaling .NET string to AX string';
    xppContents = netContents;
    info (xppContents);
    xppContents = 'Test Marshaling AX string to .NET string';
    netContents = xppContents;
    info (netContents);

    //Fun registry test...
    netObject = Microsoft.Win32.Registry::GetValue(axRegLocation, axRegAXName, null);
    netContents = netObject.ToString();
    info (netContents);
    xppContents = netContents;
    info (xppContents);

  6. Thanks Dax Dude. Am working with Ax 2012.
    Now this is working fine.

  7. Just wanted to thank you for helping to find a workaround for a problem that was starting to become a nuisance!

  8. That fixed it thank you very much. I am in 2009.