0

I use the declaration below in a console app & a winforms app. Both are 3.5 framework.

    [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern NET_API_STATUS NetUseAdd(
        LPWSTR UncServerName,
        DWORD Level,
        ref USE_INFO_2 Buf,
        out DWORD ParmError);

The console app is OK, the winform app errors:

Unable to find an entry point named 'NetUseAdd' in DLL 'NetApi32.dll'

I have scoured the web & came up with the declaration below. This 1 gives the same error for the winform app.

    [DllImport("NetApi32.dll", 
        SetLastError = true, 
        CharSet = CharSet.Unicode
        )]
    public static extern uint NetUseAdd(
         string UncServerName,
         UInt32 Level,
         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;
    }

I've run dumpbin /export & NetUseAdd is there.

All help is much appreciated.

ninety
  • 19
  • 6
  • Don't you still need to declare the USE_INFO_2 parameter as ref in your second piece of code? – Edwin de Koning Oct 18 '11 at 11:45
  • Either way it fails in the winform app, I'm affraid. – ninety Oct 18 '11 at 12:28
  • The only clean explanation is that it is finding the wrong version of netapi32.dll. Make sure you didn't name your own project "netapi32". Use SysInternals' ProcMon utility to find out where it got loaded from. – Hans Passant Oct 18 '11 at 13:12
  • Doh! you've got it; only gone & named my class just that, netapi32. Lots of thanks yous & I'll make myself a note for next time. – ninety Oct 18 '11 at 14:37

0 Answers0