Solved

Getting a mapped drive's name

Posted on 1998-07-07
9
372 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Understanding the meaning of Portability in Embedded System Programming 8 110
Window placement 17 64
pre4 challenge 19 88
FizzBuzz challenge 9 73
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now