• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 778
  • Last Modified:

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;
}
0
Pber
Asked:
Pber
  • 8
  • 8
1 Solution
 
jkrCommented:
'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;
}
0
 
PberSolutions ArchitectAuthor Commented:
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 *'
0
 
jkrCommented:
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)")
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PberSolutions ArchitectAuthor Commented:
I don't even get that far.  It won't even compile.

I'm using XP on a 2003 domain.
0
 
jkrCommented:
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;
}

0
 
PberSolutions ArchitectAuthor Commented:
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. ):
0
 
jkrCommented:
That's odd.


#define UNICODE
#define _UNICODE
#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("\\\\somemachine");
    return 0;
}

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); // 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;
}

works here.

BTW, using '#define UNICODE' is the same as using them in your preprocessor definitions.

0
 
jkrCommented:
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
0
 
PberSolutions ArchitectAuthor Commented:
...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'
0
 
jkrCommented:
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
0
 
PberSolutions ArchitectAuthor Commented:
Thanks for the effort.  I give up.

Thank again.
0
 
jkrCommented:
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;
}
0
 
PberSolutions ArchitectAuthor Commented:
Yes that actually complies, but I get a Error: 1113 (No mapping for the Unicode character exists in the target multi-byte code page.)
0
 
jkrCommented:
What (except NULL) are you passing to the function?
0
 
PberSolutions ArchitectAuthor Commented:

    EnumShares("someserver");
0
 
PberSolutions ArchitectAuthor Commented:
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...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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