Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable value has wrong value after returning from a DLL call
    text
    copied!<p>I have a situation that seems odd to me. I have a WCF Web Service in C# that I have some debugging code I wrote into it that opens a file and writes some lines about the value of the current variables and closes the file. This is a feature I turn off and on depending if I need to use it to debug something at a customer's location.</p> <p>For all the reading and writing functions, it is interacting with a DLL that executes COBOL code in the background to access a legacy data storage system. All the variables are passed in by reference and the returned data is returned through those same referenced variables. Most of the time the variables are just initialized to the correct size and passed in and return data to me. The return types of the functions are all statuses of the execution in the COBOL environment that tell you if the program wasn't loaded/initialized ok, if the execution was ok, etc. All writes return 0 success and -1 failure.</p> <p>Before and after all of my calls to this DLL I have my debug code so I can see the values before and after. The debug function just does a write line into a FileStream and returns. I do pass in the same variables by value to the debug function that I'm passing to my worker function as references.</p> <p>When I turn on my debugging switch, the odd stuff starts happening. After 1-2 calls, the worker function just continues to have the value 0 in the success/failure code. Of course my logic believes a 0 is a successful write to the data file so I'm falsely reporting back the write worked fine. Debugging the dll, you can specifically see it returning -1, attempting to inform the web service that the write failed, which in my case is correct because a duplicate key violation occurred. When the value shows up in the Visual Studio watch, it somehow turned into a 0, indicating success.</p> <p>The thing that I can't wrap my brain around is that if I turn off my debugging switch. This turns off my FileStream writer, my passing of the variables by value that is then passed by reference to my worker function, everything works the way it should. The value from the Dll shows up correctly for success/failures as it should. It makes no sense to me why the return value would be corrupted.</p> <p>I haven't tried to copy all the variables to temp holding variables and passing different variables to the debug and worker function yet. The thing is, I don't understand why that value is being corrupted. It is being changed, because I initialize the value to -99, but it comes back always as 0, when it should be returning -1 in my test case.</p> <p>Thanks in advance, hopefully someone can either suggest how to trace the variable between the Dll return and being displayed in the Visual Studio watch, which seems to be the place the value is getting corrupted or knows why my debug operation might be somehow corrupting the returned value.</p> <pre><code>private UploadStatus SetRecord(int remoteId, string xmlDate, string val1, string val2, int val3, int recSeq, string name, int cat1, int cat, int seq, string val5, double val6) { UploadStatus status = new UploadStatus(); status.id = remoteId; int RtnCode = -99; int countRecSeq = Convert.ToInt16(ConfigurationManager.AppSettings["cycle-value"]); // value to cycle through for unique key // Pad variables appropriately val1 = val1 .PadRight(6, ' '); val2 = val2 .PadRight(3, ' '); val3 = val3 .PadRight(30, ' '); xmlDate = xmlDate.PadRight(25, ' '); val5 = val5.PadLeft(2, ' '); errorTypes ErrCode; try { if (debug) { string input = "remoteId: " + remoteId + "||\n" + "xmlDate: " + xmlDate + "||\n" + "val1: " + val1 + "||\n" + "val2: " + val2+ "||\n" + "val3: " + val3 + "||\n" + "recSeq: " + recSeq + "||\n" + "name: " + name + "||\n" + "cat1: " + cat1 + "||\n" + "cat: " + cat + "||\n" + "val4: " + val4 + "||\n" + "val5: " + val5 + "||\n" + "val6: " + val6 + "||\n" + "RtnCode: " + RtnCode + "||\n"; writeDebug("\n\n*** Start UploadStatus SetRecord(int remoteId, string xmlDate, string val1, string val2, int val3, int recSeq, string name, int cat1, int cat, int val4, string val5, double val6) ***\n" + input); } ErrCode = COBOL.SET_RECORD(ref xmlDate, ref val1, ref val2, ref val3, ref recSeq, ref name, ref cat1, ref cat, ref val4, ref val5, ref val6, " ", ref RtnCode); // Add to recSeq looking for a unique key to insert while (ErrCode == errorTypes.CS_OK &amp;&amp; recSeq &lt; countRecSeq &amp;&amp; RtnCode == -1) { ... private void writeDebug(string input) { string logText = DateTime.Now.ToShortDateString() + " " + DateTime.Now.TimeOfDay.ToString() + ": " + input; try { if (!File.Exists(debugPath)) { using (StreamWriter sw = File.CreateText(debugPath)) { sw.Write(logText); } } else { using (StreamWriter sw = File.AppendText(debugPath)) { sw.Write(logText); } } } catch (Exception ex) { writeException(ex); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload