[Webinar] Streamline your web hosting managementRegister Today

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

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.  
0
sgerlach
Asked:
sgerlach
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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