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...
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);
Can u please help me out here, exactly where the above code can be taken.ReplyDelete
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.ReplyDelete
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.
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.ReplyDelete
str axAOSName = "02"; // Your AOS Name here
str axConfigName = "DEV"; // Your Config Name here
str axRegAXName = "application"; // Registry object name in Registry path
axRegLocation = #AOSregistryPath + '\\' + axAOSName + '\\' + axConfigName;
contents = Microsoft.Win32.Registry::GetValue(axRegLocation, axRegAXName, null);
I tried to run the code in the job, but it is throwing the same error.ReplyDelete
Error: CLR object cannot be marshaled to Microsoft Dynamics anytype
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.ReplyDelete
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)
str axAOSName = "01"; // Your AOS Name here
str axRegAXName = "InstanceName"; // Registry object name in Registry path
System.Object netObject = new System.Object();
axRegLocation = #AOSregistryPath + '\\' + axAOSName;
//Simple marshaling test...
netContents = 'Test Marshaling .NET string to AX string';
xppContents = netContents;
xppContents = 'Test Marshaling AX string to .NET string';
netContents = xppContents;
//Fun registry test...
netObject = Microsoft.Win32.Registry::GetValue(axRegLocation, axRegAXName, null);
netContents = netObject.ToString();
xppContents = netContents;
Thanks Dax Dude. Am working with Ax 2012.ReplyDelete
Now this is working fine.
Just wanted to thank you for helping to find a workaround for a problem that was starting to become a nuisance!ReplyDelete
That fixed it thank you very much. I am in 2009.ReplyDelete
Hey! I am getting bored, please fchat with me ;) ;) ;) ████████████████████████████████████████████████████████████████████████████████████████████████ReplyDelete