?
Solved

How to Convert from Local to UNC path?

Posted on 1999-08-02
11
Medium Priority
?
339 Views
Last Modified: 2013-11-20
I can see how to do this using a MAPI command that may or may not be supported in the future. How is this supposed to be handled. I'm getting a file name and path back from a CFileDialog. I want this to be in a UNC format. I'm using MSVC++ 5.
0
Comment
Question by:maknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 280 total points
ID: 1312340
Use 'WNetGetConnection()' to achieve this. From the docs:

"WNetGetConnection  
The WNetGetConnection function retrieves the name of the network resource associated with a local device.

DWORD WNetGetConnection( LPCTSTR lpLocalName,
 // pointer to local name
 
LPTSTR lpRemoteName,
 // pointer to buffer for remote name
 
LPDWORD lpnLength
 // pointer to buffer size, in characters
 
);
 


Parameters
lpLocalName

Points to a null-terminated string that specifies the name of the local device to get the network name for.

lpRemoteName

Points to a buffer that receives the null-terminated remote name used to make the connection.

lpnLength

Points to a variable that specifies the size, in characters, of the buffer pointed to by the lpRemoteName parameter. If the function fails because the buffer is not big enough, this parameter returns the required buffer size.

Return Values
If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is an error code. To get extended error information, call GetLastError. GetLastError may return one of the following error codes: Value
 Meaning
 
ERROR_BAD_DEVICE
 The string pointed to by the lpLocalName parameter is invalid.
 
ERROR_NOT_CONNECTED
 The device specified by lpLocalName is not redirected.
 
ERROR_MORE_DATA
 The buffer is too small. The lpnLength parameter points to a variable that contains the required buffer size. More entries are available with subsequent calls.
 
ERROR_CONNECTION_UNAVAIL
 The device is not currently connected, but it is a persistent connection.
 
ERROR_NO_NETWORK
 No network is present.
 
ERROR_EXTENDED_ERROR
 A network-specific error occurred. To get a description of the error, use the WNetGetLastError function.
 
ERROR_NO_NET_OR_BAD_PATH
 None of the providers recognized this local name as having a connection. However, the network is not available for at least one provider to whom the connection may belong.
 


The WNetGetConnection function returns error codes for compatibility with Windows version 3.1. For compatibility with the Win32 API, the function also sets the error code value returned by GetLastError."

Once you have the network name for a drive/share, you can simply append the relative path of your file to the network name to obtain the full UNC name...

Feel free to ask if you need more information!
0
 

Author Comment

by:maknight
ID: 1312341
According to the docs, this only works on WinNT. I develop on WinNT, but all the systems that will actually use the program run Win95. I saw this one.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1312342
Hmm, that's from the VC5 docs:

WNetGetConnection
 
Windows NT
 Yes
 
Win95
 Yes
 
Win32s
 Yes
 
Import Library
 mpr.lib
 
Header File
 winnetwk.h
 
Unicode
 WinNT
 
Platform Notes
 None
 
It only says that the UNICODE version is available on NT only...
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:maknight
ID: 1312343
I'll give it a shot. Here's what on my VC5 online docs:

WNetGetUniversalName
Windows NT
 Yes
Win95
 No
Win32s
 No
Import Library
 mpr.lib
Header File
 winnetwk.h
Unicode
 WinNT
Platform Notes
 None
0
 
LVL 86

Expert Comment

by:jkr
ID: 1312344
That's REALLY strange - I copied the above from the VC5 online docs, too...

BTW: A 3rd opinion ;-)

http://msdn.microsoft.com/library/sdkdoc/network/networks_84a6.htm

"Remarks
Windows NT/Windows 2000: 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.)

Requirements
  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Windows CE: Requires version 2.0 or later.
  Header: Declared in winnetwk.h.
  Import Library: Use mpr.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000."
0
 

Author Comment

by:maknight
ID: 1312345
OK. How in the world do I use this LPVOID thing?
0
 
LVL 86

Expert Comment

by:jkr
ID: 1312346
Err - which LPVOID?
0
 
LVL 86

Expert Comment

by:jkr
ID: 1312347
Hmm, this API is intended to be used like this:

char acNetName[MAX_PATH];
DWORD dwSize = MAX_PATH;
char* pszLocalName="C:\\";

WNetGetConnection(  pszLocalName, acNetName, &dwSize);
0
 

Author Comment

by:maknight
ID: 1312348
Got It! I increased the points too. Thanks for the assistance.
0
 

Author Comment

by:maknight
ID: 1312349
BTW: I was looking at WNetGetUniversalName, and not WNetGetConnection. That's why we were not synced up very well with the documentation.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1312350
Ooops, this explains a lot ;-)

Thanx!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question