Get share name of a Shared Drive

Let's say I have a path, c:\mydrive, and I allow it to be shared with the name, TEST, is there an API where I pass it "c:\mydrive" and it will tell me that the share name is TEST?  This would need to work in both Windows 95,98 and NT 4.0 and above.  
LVL 1
sgerlachAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
This is going to be a bit difficult, as the LANManager APIs that serve this purpose are implemented differently on NT and 9x, so here's a short outline:

- use 'WNetEnumResource()' to get all shares on the local machine

- 'NetShareGetInfo()' at infolevel 2 will return the local path... compare it to the one you have, and 'shi2_netname' will give you the share name...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
waseemanisCommented:
Check out the function NetConnectionEnum(). It provides quite a bit of info about net share and stuff. Also it is supported on both NT(3.1 onwards) and 95 onwards.

Luck,
Waseem.
0
sgerlachAuthor Commented:
I looked at the documentation for this function and it said the following:

The NetConnectionEnum function is obsolete. It is provided only for compatibility with LAN Manager and 16-bit versions of Windows. Win32-based applications should use the WNetEnumResource function.

However, I think jkr suggestion will work. I am trying it out now.
0
jkrCommented:
Thanks! But make sure to/aware of that you'll have to use two versions of these APIs - NT and 9x not only differ in the import libs, but also the header files.

That's what I've done for e.g. 'NetServerGetInfo()':

BOOL    NetIsValidServer    (   LPCTSTR pszServer)
{

//  local types for this function...
struct server_info_1
{
    char    ac [ CNLEN +1];
    UCHAR   ucMJ;
    UCHAR   ucMN;
    ULONG   ul;
    char*   pc;
};

typedef NET_API_STATUS  ( NET_API_FUNCTION *NSGINT) ( LPTSTR, DWORD, PVOID*);
typedef NET_API_STATUS  ( NET_API_FUNCTION *NSGI95) ( LPTSTR, DWORD, PVOID*, USHORT, PUSHORT);

    OLECHAR                 atcServerW  [   MAX_COMPUTERNAME_LENGTH];
    struct server_info_1*   psi1;
    SERVER_INFO_101*        psi101;
    NET_API_STATUS          nas =   NERR_Success;

    if  (   lstrlen (   pszServer)  >   MAX_COMPUTERNAME_LENGTH)
            return  (   FALSE);

    // what booby a M$ decided to put 'NetServerGetInfo()' into 'svrapi.dll'
    // on w95 and into 'netapi32.dll' on NT ??? §%$&!!!
    HANDLE                  hDll;
    NSGINT                  pfnNSGINT   =   NULL;
    NSGI95                  pfnNSGI95   =   NULL;
    char*                   pszLib;

    if  (   g_bCrippledOS)  //  95
        {
            pszLib  =   "svrapi.dll";
        }
     else   //  NT
        {  
            pszLib  =   "netapi32.dll";

            //  check UNC naming convention
            //if    (   NET_IS_UNC_NAME (   pszServer))
            //      pszServer   +=  2; // NT LANMAN doesn't like UNC ...

            MultiByteToWideChar (   CP_ACP,
                                    0,  
                                    pszServer,
                                    -1,
                                    atcServerW,
                                    lstrlen (   pszServer) + 1
                                );
        }

    hDll    =   LoadLibrary (   pszLib);

    if  (   INVALID_HANDLE_VALUE    ==  hDll)
            return  (   FALSE);

    if  (   !g_bCrippledOS) // NT
        {
            pfnNSGINT   =   ( NSGINT) GetProcAddress    (   ( HINSTANCE) hDll,  
                                                            "NetServerGetInfo"
                                                        );

            if  (   pfnNSGINT)
                {
                    nas =   ( *pfnNSGINT)   (   ( char*) atcServerW,
                                                101,
                                                ( void**) &psi101
                                            );

                }
        }
     else   // 95
        {
            USHORT  us;

            pfnNSGI95   =   ( NSGI95) GetProcAddress    (   ( HINSTANCE) hDll,  
                                                            "NetServerGetInfo"
                                                        );

            if  (   pfnNSGI95)
                {
                    nas =   ( *pfnNSGI95)   (   ( char*) pszServer,
                                                1,
                                                ( void**) &psi1,
                                                sizeof  (   struct server_info_1),
                                                &us
                                            );
                }
        }

    FreeLibrary (   ( HINSTANCE) hDll);

    if  (   NERR_Success    ==  nas &&  ( pfnNSGINT || pfnNSGI95))
            return  (   TRUE);

    if  (   ERROR_MORE_DATA ==  nas &&  ( pfnNSGINT || pfnNSGI95))
            return  (   TRUE);

    return  (   FALSE);
}

0
sgerlachAuthor Commented:
I did notice that there were two different NetServerGetInfo calls for 95/98 and NT/2000, but I did not realize that there are two different header files.  What a pain.  Thanks for letting me know and especially the code.  I like your little comments:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.