Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Getting a mapped drive's name

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
Dex_Man
Asked:
Dex_Man
1 Solution
 
trestanCommented:
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
 
Dex_ManAuthor 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
0
 
piano_boxerCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Dex_ManAuthor 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

0
 
Dex_ManAuthor 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
0
 
Andy_KeysCommented:
The lib you need to link with is "Mpr.lib"
0
 
trestanCommented:
You need to change your setting of your project.
0
 
trestanCommented:
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
 
Dex_ManAuthor 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
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now