C# - NetUseAdd from NetApi32.dll on Windows Server 2008 and IIS7
Posted
by Jack Ryan
on Stack Overflow
See other posts from Stack Overflow
or by Jack Ryan
Published on 2009-07-09T08:27:59Z
Indexed on
2010/03/22
22:01 UTC
Read the original article
Hit count: 692
I am attemping to use NetUseAdd to add a share that is needed by an application. My code looks like this.
[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern uint NetUseAdd(
string UncServerName,
uint Level,
IntPtr Buf,
out uint ParmError);
...
USE_INFO_2 info = new USE_INFO_2();
info.ui2_local = null;
info.ui2_asg_type = 0xFFFFFFFF;
info.ui2_remote = remoteUNC;
info.ui2_username = username;
info.ui2_password = Marshal.StringToHGlobalAuto(password);
info.ui2_domainname = domainName;
IntPtr buf = Marshal.AllocHGlobal(Marshal.SizeOf(info));
try
{
Marshal.StructureToPtr(info, buf, true);
uint paramErrorIndex;
uint returnCode = NetUseAdd(null, 2, buf, out paramErrorIndex);
if (returnCode != 0)
{
throw new Win32Exception((int)returnCode);
}
}
finally
{
Marshal.FreeHGlobal(buf);
}
This works fine on our server 2003 boxes. But in attempting to move over to Server 2008 and IIS7 this doesnt work any more. Through liberal logging i have found that it hangs on the line Marshal.StructureToPtr(info, buf, true);
I have absolutely no idea why this is can anyone shed any light on it for tell me where i might look for more information?
© Stack Overflow or respective owner