Hi all,
I've been running the static Code Analysis Tool (aka FxCop) on some code I've "inherited", in order to try to improve it. One of the warnings it gives me is as follows (my emphasis added):
CA1901: Microsoft.Portability: As it is declared in your code, parameter 'PathName' of P/Invoke 'NativeMethods.NetDfsMove(string, string, int)' will be 8 bytes wide on 64-bit platforms. This is not correct, as the actual native declaration of this API indicates it should be 4 bytes wide on 64-bit platforms.
This is one of the more concerning warnings, since looking over the code the function must get called at some point and the application is running on 64-bit Windows, so it must surely cause a problem somewhere down the line.
I've searched and searched the web, but all references talk about how to deal with integers or IntPtr (
MSDN[
^] and various forums) and I cannot find to what or how I should change the marshalling of the string in order to correct this.
The declaration of the method is currently as follows:
[DllImport("netapi32.dll", EntryPoint = "NetDfsMove", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern int NetDfsMove
(
[MarshalAs(UnmanagedType.LPWStr)]string PathName,
[MarshalAs(UnmanagedType.LPWStr)]string NewPathName,
int Flags
);
(Originally it was defined with CharSet.Auto, and didn't have the MarshalAs statements, but I had the same warning. It seems that the declaration presented on
pinvoke.net[
^] is incorrect for 64-bit systems).
Note, the problem explicitly mentions the string parameter "PathName". The marshalling of "Flags" isn't in question here; changing it to unsigned int doesn't solve the main issue.
The Native declaration is:
NET_API_STATUS NetDfsMove(
_In_ LPWSTR Path,
_In_ LPWSTR NewPath,
_In_ ULONG Flags
);
I don't understand why this is given me a warning; other functions of netapi32 (such as
NetDfsAdd[
^],
NetDfsRemove[
^], etc) are similarly declared with LPWSTR parameters and yet I don't get this warning for them.
Any ideas? Can anyone shed any light on this, please?
Many thanks,
Ian.