Solved

Getting a mapped drive's name

Posted on 1998-07-07
9
382 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
[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
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

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!

Question has a verified solution.

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

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.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

624 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