Solved

Getting a mapped drive's name

Posted on 1998-07-07
9
377 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?
0
Comment
Question by:Dex_Man
9 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1318840
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;
}

0
 
LVL 1

Author Comment

by:Dex_Man
ID: 1318841
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
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1318842
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);
0
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.

 
LVL 1

Author Comment

by:Dex_Man
ID: 1318843
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

0
 
LVL 1

Author Comment

by:Dex_Man
ID: 1318844
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
0
 
LVL 1

Expert Comment

by:Andy_Keys
ID: 1318845
The lib you need to link with is "Mpr.lib"
0
 
LVL 8

Accepted Solution

by:
trestan earned 50 total points
ID: 1318846
You need to change your setting of your project.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1318847
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.

0
 
LVL 1

Author Comment

by:Dex_Man
ID: 1318848
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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