Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:23:44+00:00 2026-05-14T03:23:44+00:00

I am attemping to use NetUseAdd to add a share that is needed by

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:23:45+00:00Added an answer on May 14, 2026 at 3:23 am

    The reason is:

    You pulled the p/invoke signature off pinvoke.net and you didn’t verify it. The fool who initially wrote this p/invoke sample code had no idea what he was doing and created one that “works” on 32-bit systems but does not work on 64-bit systems. He somehow turned a very simple p/invoke signature into some amazingly complicated mess and it spread like wildfire on the net.

    The correct signature is:

        [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;
        }
    

    And your code should read:

    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);   
    }
    

    Hope this was of some help. I just spent half a day knee deep remote debugging someone elses trash code trying to figure out what was going on and it was this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attemping to use this code to call a php file, access the database,
Attempting to use the data series from this example no longer passes the JSONLint
In attempting to use std::select1st from <functional> in a VS2008 project I found that
I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time
Attemping to use jQuery to find a button on a page id=btn. ar using
Im attempting to use Silverlight and MVC together. After creating a simple Silverlight application
I am attempting to use XMLParse against content that is not valid xhtml. In
I am attempting to use Castle Windsor in my automated tests like so: On
I am attempting to use the MediaStore.ACTION_VIDEO_CAPTURE in my app, see code below. However,
Attempting to use asp.net mvc's Action Result of File. So it would seem that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.