Link to home
Start Free TrialLog in
Avatar of Pber
PberFlag for Canada

asked on

NetShareEnum more problems

Ultimately I'm trying to make a combo box typedown the autopopulates itself by enumerating the shares of the servername that is typed into the box.  (i.e. just like Start, Run, \\Someserver\ <- at this point the shares are enumerated and populated in the combo box and the combo is expanded.)

Anyhow, In my production code I would be getting the servername from a call:

wchar_t szTmp[256];
GetDlgItemText(hDlg,IDC_COMBO2,szTmp,256);

How do I pass the servername to NetShareEnum.  (I've added some Proof of concept code I can't even pass a server name without it giving a error of 1113 or it just plain blowing up.  Any help would be great.


//Preprocessor defs: _UNICODE. UNICODE        //I need to keep these
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <lm.h>

int main(int argc, char* argv[]);
HRESULT EnumShares(char *lpszServer);//,HWND hCbo);
int main(int argc, char* argv[])
{

      EnumShares(NULL);
      return 0;
}

HRESULT EnumShares(char *lpszServer)//,HWND hCbo)
{
   PSHARE_INFO_1 BufPtr,p;
   NET_API_STATUS res;

   LPWSTR sz1 = NULL;

   DWORD er=0,tr=0,resume=0, i;

   do // begin do
   {
      res = NetShareEnum(lpszServer, 1, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
      if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
      {
         p=BufPtr;

         for(i=1;i<=er;i++)
         {
                  
                   if (p->shi1_type == STYPE_DISKTREE)
                   {
                        printf("%S\\%S\n",lpszServer,p->shi1_netname);
                        //swprintf(sz1,L"%S\\%S",&lpszServer,p->shi1_netname);
                                                                //SendMessage(hCbo, CB_ADDSTRING, 0, (LPARAM)sz1);
                   }
                   p++;
         }

         NetApiBufferFree(BufPtr);
      }
      else
      printf("Error: %ld\n",res);
   }
   while (res==ERROR_MORE_DATA); // end do
   return 0;
}
Avatar of jkr
jkr
Flag of Germany image

'NetShareEnum()' takes a 'wchar_t*' as the server name, not a 'char*' - try

HRESULT EnumShares(char *lpszServer)//,HWND hCbo)
{
  PSHARE_INFO_1 BufPtr,p;
  NET_API_STATUS res;
  wchar_t awcServer [ MAX_COMPUTERNAME_LENGTH];

   mbstowcs(awcServer, lpszServer,strlen(lpszServer)); // convert to UNICODE

  LPWSTR sz1 = NULL;

  DWORD er=0,tr=0,resume=0, i;

  do // begin do
  {
     res = NetShareEnum(awcServer, 1, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
     if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
     {
        p=BufPtr;

        for(i=1;i<=er;i++)
        {
               
               if (p->shi1_type == STYPE_DISKTREE)
               {
                   printf("%S\\%S\n",lpszServer,p->shi1_netname);
                   //swprintf(sz1,L"%S\\%S",&lpszServer,p->shi1_netname);
                                                               //SendMessage(hCbo, CB_ADDSTRING, 0, (LPARAM)sz1);
               }
               p++;
        }

        NetApiBufferFree(BufPtr);
     }
     else
      printf("Error: %ld\n",res);
  }
  while (res==ERROR_MORE_DATA); // end do
  return 0;
}
Avatar of Pber

ASKER

Thanks, I was playing with mbstowcs as per link you provided me in another post: http://win32.mvps.org/network/nshe.cpp

But I get the following error:
error C2664: 'NetShareEnum' : cannot convert parameter 1 from 'unsigned short [15]' to 'char *'
Are you on Win9x? In this case, you should use http://msdn.microsoft.com/library/en-us/netmgmt/netmgmt/netshareenum_sample_windows_95_98_me_.asp ("NetShareEnum Sample (Windows 95/98/Me)")
Avatar of Pber

ASKER

I don't even get that far.  It won't even compile.

I'm using XP on a 2003 domain.
Oh, I think I got it - check your 'printf()' code:

HRESULT EnumShares(char *lpszServer)//,HWND hCbo)
{
 PSHARE_INFO_1 BufPtr,p;
 NET_API_STATUS res;

 LPWSTR sz1 = NULL;

 DWORD er=0,tr=0,resume=0, i;

 do // begin do
 {
    res = NetShareEnum(awcServer, 1, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
    if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
    {
       p=BufPtr;

       for(i=1;i<=er;i++)
       {
             
               if (p->shi1_type == STYPE_DISKTREE)
              {
                  printf("%s\\%S\n",lpszServer,p->shi1_netname); // 1st argument is ANSI, thus a lowercase %s
                  //swprintf(sz1,L"%S\\%S",&lpszServer,p->shi1_netname);
                                                              //SendMessage(hCbo, CB_ADDSTRING, 0, (LPARAM)sz1);
              }
              p++;
       }

       NetApiBufferFree(BufPtr);
    }
    else
     printf("Error: %ld\n",res);
 }
 while (res==ERROR_MORE_DATA); // end do
 return 0;
}

Avatar of Pber

ASKER

Evidently that seems to be another bug.  If I comment out the printf line I still get the error.  The only way I've been able to get this code to work is if I replace awcServer with a NULL in the NetShareEnum statement.  (not too useful).
I can also get the NetShareEnum code from the SDK to work if I do a:  

#define UNICODE

If I use that, I can't use the preprocessor defs of: _UNICODE, UNICODE.  If I can't use those, it screws up the rest of my code. ):
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, a Copy&Paste accident on my side:

mbstowcs(awcServer, lpszServer,strlen(lpszServer)); // convert to UNICODE

should be

mbstowcs(awcServer, lpszServer,strlen(lpszServer) + 1); // convert to UNICODE
Avatar of Pber

ASKER

...this is why I like VB.  I still get the same error.  

Funny thing is, it I grab the example from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netshareenum.asp and compile it ... it works.  now if I remove #define UNICODE and place it in preprocessor directives.  I'll get the conversion error.  If I put #define UNICODE back in and remove the preprocessor directives, it will no longer compile...yet it worked initially this way before I changed it.

I tired in VC7 too:

VS6 Ent SP6.
error C2664: 'NetShareEnum' : cannot convert parameter 1 from 'unsigned short [15]' to 'char *'

VC7
error C2664: 'NetShareEnum' : cannot convert parameter 1 from 'wchar_t [15]' to 'LPSTR'
The above MSDN link is for Win9x only.

I compiled the code snippet 'as is' with VC6 and

cl.exe doenum.cpp /link netapi32.lib
Avatar of Pber

ASKER

Thanks for the effort.  I give up.

Thank again.
Hey, don't give up that easily!

Have you tried your original function with just the 'printf()' altered?

HRESULT EnumShares(char *lpszServer)//,HWND hCbo)
{
  PSHARE_INFO_1 BufPtr,p;
  NET_API_STATUS res;

  LPWSTR sz1 = NULL;

  DWORD er=0,tr=0,resume=0, i;

  do // begin do
  {
     res = NetShareEnum(lpszServer, 1, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
     if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
     {
        p=BufPtr;

        for(i=1;i<=er;i++)
        {
               
               if (p->shi1_type == STYPE_DISKTREE)
               {
                   printf("%s\\%S\n",lpszServer,p->shi1_netname);
                   //swprintf(sz1,L"%S\\%S",&lpszServer,p->shi1_netname);
                                                               //SendMessage(hCbo, CB_ADDSTRING, 0, (LPARAM)sz1);
               }
               p++;
        }

        NetApiBufferFree(BufPtr);
     }
     else
      printf("Error: %ld\n",res);
  }
  while (res==ERROR_MORE_DATA); // end do
  return 0;
}
Avatar of Pber

ASKER

Yes that actually complies, but I get a Error: 1113 (No mapping for the Unicode character exists in the target multi-byte code page.)
What (except NULL) are you passing to the function?
Avatar of Pber

ASKER


    EnumShares("someserver");
Avatar of Pber

ASKER

Believe it or not, I got this working by adding: (LPSTR)awcServer

i.e.
res = NetShareEnum((LPSTR)awcServer, 1, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);

Thanks again for the help...