We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Getting a mapped drive's name

Dex_Man
Dex_Man asked
on
Medium Priority
409 Views
Last Modified: 2013-11-19
Lets say I have my E: mapped to \\CBS01\Dex, so in Windows Explorer is looks like:

Dex on 'CBS01' (E:)

How can I get the name of the machine (ie. CBS01) from within a VC++ 5.0 program on NT 4.0/95 machines?
Comment
Watch Question

Commented:
I am not quite clear about your first half statement, but to get the name of the machine, you can use NetWkstaGetInfo function. The following code display the computer name and the LAN name it connected to.

#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>

#define _UNICODE
#pragma hdrstop
#define MAXLEN 256


int main( int argc, char *argv[] )
{
      DWORD rc;
      WKSTA_INFO_100 *buf;
      wchar_t server[MAXLEN];
      if ( argc > 2 )      {
            puts( "usage: nwgi [\\\\server]" );      
            return 1;
      }
      if ( argc >= 2 )
            mbstowcs( server, argv[1], MAXLEN );      
      else            
            server[0] = L'\0';
      buf = NULL;
      rc = NetWkstaGetInfo( (char *) server, 100, (BYTE **) &buf );
      if ( rc != ERROR_SUCCESS )
            printf( "NWGI() returned %lu\n", rc );
      else      
      {
            printf( "Computer name: \"%S\"\n", buf->wki100_computername );
            printf( "LAN group:     \"%S\"\n", buf->wki100_langroup );      
            if ( buf != NULL )
                  NetApiBufferFree( buf );      
}
      return 0;
}

Author

Commented:
Great code, but not quite what I needed.

You can map a drive, which means connect a network drive or share to your computer, from within Explorer. This 'maps' the drive/share to a local drive. So, if out there on my network, I have a machine called CBS01, and a share on that machine called Dex, then it's \\CBS01\Dex for a complete name to that share, OR you can map it to a local drive, and it takes on a drive letter. You then access this directly from the drive letter. You can map a drive from Explorer->Tools->Map Network Drive

I think I found the function, but I can't get it to work right (I'm getting unresolved external errors when linking). Here's a snippet of code:

#include <winnetwk.h>

CString  FileName = "", server = "";
DWORD dwEC, size;
char remoteName[256];
CString place = "F:";

dwEC = WNetGetConnection((LPTSTR) place, (LPSTR)remoteName,   &size);

I've left out everything that didn't have to do with the last line of code (ie. I've got all my braces, declarations, etc etc).

As far as I can tell, I'm having trouble with the last parameter (size), which is a LPDWORD. It could be either of the other terms, or something else entirely, but I'm guessin something is wrong with size.

Any ideas?

-Dex Man
You need to initialize size to the SIZE of remoteName *before* calling WNetGetConnection().

char remoteName[MAX_PATH];
DWORD size = sizeof(remoteName);

WNetGetConnection("I:", (LPSTR)remoteName,   &size);

Author

Commented:
While the code you proposed, piano_boxer, is correct (I found an example in help to back you up...heh...I should have looked harder), I'm still getting the linker error.

The actual error is this:

Linking...
MyProgam.obj : error LNK2001: unresolved external symbol _WNetGetConnectionA@12
Debug/MyProgram.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


Help on this linker error says a whole lot of stuff...none of which I'm sure of how to track down. Any ideas?

-Dex Man

Author

Commented:
In addition to what I said above, I also cut and pasted directly from the example I found in help and tried that. Gave me the exact same error.

The help for this Linker error says: "Code will generate this error message if it references something (like a function, variable, or label) that the linker can't find in all the libraries and object files it searches. In general, there are two reasons this error occurs: what the code asks for doesn't exist (the symbol is spelled incorrectly or uses the wrong case, for example), or the code asks for the wrong thing (you are using mixed versions of the libraries some from one version of the product, others from another version)."

I've done a Find in all files for the WNetGetConnection function, and found references to it (4 of them) in the winnetwk.h file, but no where else (besides my own code, of course). Where is the code for this function at?

-Dex Man
The lib you need to link with is "Mpr.lib"
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Go to link tab in project setting option. Choose input in category combobox. Put the lib name in the Object/Library modules edit box, the library is mpr.lib. This should solve your link problem.

Author

Commented:
Yeaa....it works. I was hoping it was some setting, but I don't know enough about compile/link/build settings to fix many problems.

Guys, thanks you all for your help! You've all been of tremendous assistance on this little problem of mine!

Excellent directions from you all, btw.

-Dex Man
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.