Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 24th, 2008, 11:38 AM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default WNetGetUniversalName Error 1201

Hi all,

This is an API question

I'm trying to get the UNC Path from a mapped drive. I call it once and get the ERROR_MORE_DATA code, so I call it a second time and I get the ERROR_CONNECTION_UNAVAIL code, which says "The device is not currently connected but it is a remembered connection." I'm kind of lost on that because I can see that the device is connected.

Code:
[DllImport("mpr.dll")]
private static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);

[DllImport("mpr", CharSet=CharSet.Auto)]
protected static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct UNIVERSAL_NAME_INFO
{
[MarshalAs(UnmanagedType.LPTStr)]
public string lpUniversalName;
}

//
//Constants
//
protected const int NO_ERROR = 0;
protected const int ERROR_MORE_DATA = 234;
protected const int ERROR_NOT_CONNECTED = 2250;
protected const int UNIVERSAL_NAME_INFO_LEVEL = 1;        
protected const int ERROR_CONNECTION_UNAVAIL = 1201;

UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
int bufferSize = Marshal.SizeOf(rni);

int nRet = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,ref rni, ref bufferSize);

if (ERROR_MORE_DATA == nRet)
{
    IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
    try
    {
        nRet = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,pBuffer, ref bufferSize);
//
// RETURNS ERROR 1201 = ERROR_CONNECTION_UNAVAIL
Any ideas? I'm using Windows 2000

 
Old January 25th, 2008, 07:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Have you seen the PInvoke page?

http://www.pinvoke.net/default.aspx/...ersalName.html

Your first call doesn't look quite the same to me.

/- Sam Judson : Wrox Technical Editor -/
 
Old January 25th, 2008, 10:51 AM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 Have you seen the PInvoke page?

http://www.pinvoke.net/default.aspx/...ersalName.html

Your first call doesn't look quite the same to me.

/- Sam Judson : Wrox Technical Editor -/
I had not seen the pinvoke page, thank you. I'm going to make some changes so it's like that.

I should also note that my code WORKS as an windows form app (I click a drive letter and it shows me the unc path). But I want it to work as a service, and that's when I get the error.

 
Old January 25th, 2008, 11:56 AM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bah, I stumbled across the reason on MSDN:

http://msdn2.microsoft.com/en-us/library/aa385453.aspx

If the network connection was made using the Microsoft LAN Manager network, and the calling application is running in a different logon session than the application that made the connection, a call to the WNetGetConnection function for the associated local device will fail. The function fails with ERROR_NOT_CONNECTED or ERROR_CONNECTION_UNAVAIL. This is because a connection made using Microsoft LAN Manager is visible only to applications running in the same logon session as the application that made the connection. (To prevent the call to WNetGetConnection from failing it is not sufficient for the application to be running in the user account that created the connection.)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: syntax error, unexpected T_ELSE in /h vipin k varghese BOOK: XSLT Programmer's Reference, 2nd Edition 4 September 29th, 2011 01:19 AM
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
Ch 4: Parse error: syntax error, unexpected T_SL hanizar77 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 June 23rd, 2008 09:17 PM
Parse error: syntax error, unexpected T_STRING ginost7 Beginning PHP 1 November 9th, 2007 02:51 AM
VB Error: Syntax Error or Access Violation codehappy VB How-To 7 October 3rd, 2007 05:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.