Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
C#
[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:
C++
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.
Posted
Updated 17-Aug-13 0:52am
v2
Comments
Maarten Kools 16-Aug-13 11:56am    
Think your Flags should be an unsigned int. Not sure if that's causing the error though, can't test it myself right now.
Ian A Davidson 16-Aug-13 13:05pm    
Thanks for comment, and that may be the case as well, but no - the problem is with the string (PathName).
Ian.
ledtech3 18-Aug-13 1:05am    
Look at this page and see if that helps.
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

And looks down at the LPWStr

Or this one
http://msdn.microsoft.com/en-us/library/s9ts558h.aspx
Ian A Davidson 18-Aug-13 4:08am    
Thanks for taking time to reply.
I'm afraid neither of those pages mention this situation or what to do about it; I did already do my research ;)
I was hoping someone here might have come across this problem before and known what to do about it. But never mind.
Thanks again,
Ian.
ledtech3 18-Aug-13 10:03am    
Have you tried to run the code to see if it even matters ?
Does it crash or throw errors on that value ?
Unless I'm not seeing something that seems to be the correct way to do the call.
It is a string.
Perhaps the program that showed the error is Wrong ?
You may drop them an email to ask them why their program showed it as an error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900