This one really has me stumped. I'm working with another developer who called me because he couldn't believe what he was seeing. We stepped through with the debugger together and I saw it too and had no explanation. Here's the scenario. He is writing a method that interacts with a 3rd party COM object through an auto-generated COM wrapper (generated simply by adding the COM component as a reference. Here's the top of his method:
public bool RefolderDocument(ref IManDocument oDoc)
{
string strCustom1 = (string) oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom1);
string strCustom2 = (string) oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom2);
The purpose of the code is to get a project number and subproject number from a "document" object (oDoc).
Here's what happens as you step through. After the first assignment strCustom1 has the expected value "32344" (a project number) and strCustom2 is empty as expected. After the second assignment, strCustom2 gets the subproject number "0002" -- but strCustom1 has been changed to 32334 -- one character has been changed!?
It struck me as some kind of old-school c-language stack-overrun (which I wouldn't expect in a managed application even if it was interoperating with a COM component). We were all baffled. In an attempt to hack around this bizarreness, I tried to copy the contents of the first string to another location like so:
public bool RefolderDocument(ref IManDocument oDoc)
{
string strCustom1 = string.Copy((string)oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom1));
string strCustom2 = string.Copy((string)oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom2));
Same results! We were grasping at straws at this point and dropped the code from .NET 4 to .NET 3.5 (CLR 2) but no change. One perhaps relevant point is that this is a service and we are attaching to the service process. The build is targeting x86 and the service location is definitely in the Debug output build folder.
Is there any logical explanation for this? I'm stumped as to how to proceed.
Make sure your PC (or phone), the external display, and the cable all support DisplayPort or MHL alternate modes. Make sure the device or dongle is connected directly to your PC (or phone). Make sure the device or dongle is connected to the USB-C port on your PC (or phone) that supports the correct Alternate Mode.
The USB-C laptop dock features a DisplayPort and an HDMI port, giving you the flexibility to connect your displays as needed. It supports dual monitors or a single high-resolution 4K monitor.
Looks like strCustom1 and strCustom2 have both been set as references back to the result of GetAttributeValueByID. I know not why, and it would be interesting to see if someone else on here (Paging Dr. Skeet lol...) can.
In the short term though, I think you'll find this will solve the issue for you...
public bool RefolderDocument(ref IManDocument oDoc)
{
string strCustom1 = "" + string.Copy((string)oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom1));
string strCustom2 = "" + string.Copy((string)oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom2));
My idea is basically to have it evaluate an expression rather than working with the referernce directly...
Martin.
Ps. What's with the string.Copy()?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With