TCHAR szShareName[255];
WCHAR wszShareName[255];
TCHAR szShareDir[255];
WCHAR wszShareDir[255];
    SHARE_INFO_502 si502;
    NET_API_STATUS nas;
    SECURITY_DESCRIPTOR sd;    if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
return; sprintf(szShareName, "drivec");
sprintf(szShareDir, "c:\\");
MultiByteToWideChar(CP_ACP, 0, szShareName, -1, wszShareName, 255);
MultiByteToWideChar(CP_ACP, 0, szShareDir, -1, wszShareDir, 255);    si502.shi502_netname = wszShareName;
    si502.shi502_type = STYPE_DISKTREE;
    si502.shi502_re = NULL;
    si502.shi502_permissions = 0;
    si502.shi502_max_uses = SHI_USES_UNLIMITED;
    si502.shi502_current_uses = 0;
    si502.shi502_path = wszShareDir;
    si502.shi502_passwd = NULL;
    si502.shi502_reserved = 0;
    si502.shi502_security_descriptor = &sd;    nas = NetShareAdd(
        NULL,         // share is on local machine
        502,            // info-level
        (LPBYTE)&si502, // info-buffer
        NULL            // don't bother with parm
        );
if(nas != NO_ERROR)
{
AfxMessageBox("Share directory failed.");
}
else
{
SHChangeNotify(SHCNE_NETSHARE, SHCNF_PATH, (LPCVOID)szShareDir, NULL);
}

解决方案 »

  1.   

    我的代码差不多,不过有一些注释:
       short nLevel = 50;
       struct share_info_50* pBuf = NULL;
       unsigned short cbBuffer;
       NET_API_STATUS nStatus;
       char path[]="C:\\SERVER\\";
       
       cbBuffer = sizeof(struct share_info_50);
       pBuf =(share_info_50 *) malloc(cbBuffer);   if (pBuf == NULL)
          printf("No memory\n");   strcpy(pBuf->shi50_netname,"server");//pBuf->shi50_netname是在
       //网络邻居里看到的名字
       pBuf->shi50_type = STYPE_DISKTREE;//共享类型
       pBuf->shi50_flags = SHI50F_ACCESSMASK;
       pBuf->shi50_re = NULL;//注释
       pBuf->shi50_path=path;//本地欲共享的文件夹
       pBuf->shi50_rw_password[0] = '\0'; //没有密码
       pBuf->shi50_ro_password[0] = '\0'; //没有密码   //调用 NetShareAdd 函数,指定用level 50
       nStatus = NetShareAdd(pszServerName,//NULL表示本地文件夹
                             nLevel,//级别50
                             (char FAR *)pBuf,
                             cbBuffer);//share_info_50结构的大小
       //显示结果
       switch(nStatus)
       {
       case NERR_Success:
       printf("Share added successfully.\n");
       break;
       case NERR_UnknownDevDir:
       printf("unknowndevdir.\n");
       break;
       case NERR_DuplicateShare:
       printf("The name has already been shared.\n");
       break;
       }
       
       //释放内存
       if (pBuf != NULL)
          free(pBuf);
      

  2.   

    http://www.csdn.net/expert/topic/674/674083.xml?temp=.6211054