Context: I am porting some old VB.NET code from x86 to AnyCPU platform.
Problem statement: In a class instance, I noticed a call to the kernel32 CreatePipe function would unexpectedly change, when run as a 64-bit process, the value of a totally unrelated boolean class variable from True
to False
. No exception thrown nor anything else, just that value change I see in the watch when that call to CreatePipe
occurs.
What I did to fix this issue: Based on the pinvoke website (https://www.pinvoke.net/default.aspx/kernel32.createpipe), and then from one of the comments below, I changed the CreatePipe
signature in my class from:
Private Declare Function CreatePipe Lib "kernel32" (ByRef phReadPipe As Integer,
ByRef phWritePipe As Integer,
ByRef lpPipeAttributes As SECURITY_ATTRIBUTES,
ByVal nSize As Integer) As Integer
To:
Private Declare Function CreatePipe Lib "kernel32" (ByRef phReadPipe As IntPtr,
ByRef phWritePipe As IntPtr,
ByRef lpPipeAttributes As SECURITY_ATTRIBUTES,
ByVal nSize As Integer) As Integer
And it works now, I mean, the boolean value is not unexpectedly changed anymore and the rest of the code behaves as intended.
Questions: I am not extremely satisfied by that though, and I won't be until I know what caused this behavior in a 64-bit process. Wrong memory access causing the flag value to change? Why this one variable in particular? Is it due to the incorrect length specification on the variables which may have caused the call to overwrite the next few bytes of data (this may be where the boolean variable lives)? This is just speculation though...
More generally, is there any guidelines out there to port .NET code from an x86 platform to AnyCPU, when there is one or many calls to the Windows API?