我正在尝试使用NetUseAdd添加应用程序所需的共享.我的代码看起来像这样.[DllImport(NetApi32.dll, SetLastError = true, CharSet = CharSet.Unicode)]internal static extern uint NetUseAdd(string UncServerNam...
                
我正在尝试使用NetUseAdd添加应用程序所需的共享.我的代码看起来像这样.
[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);
}
这在我们的服务器2003盒子上工作正常.但是在尝试转移到Server 2008和IIS7时,这不再起作用了.通过自由日志我发现它挂在Marshal.StructureToPtr(info,buf,true)的行上;
我完全不知道为什么这可以让任何人了解它,告诉我在哪里可以寻找更多信息?
解决方法:
原因是:
你从pinvoke.net上取下了p / invoke签名而你没有验证它.最初编写此p / invoke示例代码的傻瓜不知道他在做什么,并创建了一个在32位系统上“工作”但在64位系统上不起作用的傻瓜.他以某种方式将一个非常简单的p / invoke签名变成了一些非常复杂的混乱,它在网上像野火一样蔓延开来.
正确的签名是:
    [DllImport( "NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode )]
    public static extern uint NetUseAdd(
         string UncServerName,
         UInt32 Level,
         ref USE_INFO_2 Buf,
         out UInt32 ParmError
        );
    [StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode )]
    public struct USE_INFO_2
    {
        public string ui2_local;
        public string ui2_remote;
        public string ui2_password;
        public UInt32 ui2_status;
        public UInt32 ui2_asg_type;
        public UInt32 ui2_refcount;
        public UInt32 ui2_usecount;
        public string ui2_username;
        public string ui2_domainname;
    }
你的代码应该是:
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 = password;
info.ui2_domainname = domainName;      
uint paramErrorIndex;   
uint returnCode = NetUseAdd(null, 2, ref info, out paramErrorIndex);   
if (returnCode != 0)   
{   
    throw new Win32Exception((int)returnCode);   
}
希望这有一些帮助.我只花了半天膝盖深度远程调试别人的垃圾代码试图弄清楚发生了什么,就是这个.
本文标题为:C# – NetUseAdd来自Windows Server 2008和IIS7上的NetApi32.dll
				
        
 
            
        基础教程推荐
- C#执行EXE文件与输出消息的提取操作 2023-04-14
 - C#使用NPOI将excel导入到list的方法 2023-05-22
 - C#中参数的传递方式详解 2023-06-27
 - 如何用C#创建用户自定义异常浅析 2023-04-21
 - Unity虚拟摇杆的实现方法 2023-02-16
 - C#使用SQL DataAdapter数据适配代码实例 2023-01-06
 - C#使用Chart绘制曲线 2023-05-22
 - 浅谈C# 构造方法(函数) 2023-03-03
 - C# TreeView从数据库绑定数据的示例 2023-04-09
 - C#实现归并排序 2023-05-31
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				