...( LPCTSTR lpszShareName )
{
NET_API_STATUS res;
SHARE_INFO_502 si;
SHARE_INFO_502 * psi; 
  TCHAR szShareName[255];
WCHAR wszShareName[255];
TCHAR szPathName[255];
CString sPathName;         sprintf(szShareName, lpszShareName);
MultiByteToWideChar(CP_ACP, 0, szShareName, -1, wszShareName, 255); psi = &si;
res = NetShareGetInfo( NULL,
(char *)wszShareName,
502,
(LPBYTE *)psi);
  
WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)si.shi502_path, 255,   szPathName, -1, NULL, NULL);
if(res == NERR_Success)
sPathName = szPathName;    return sPathName;
}

解决方案 »

  1.   

    NetShareGetInfo函数执行完毕后, si 结构中并没有获取到内容.
      

  2.   

    没有出错信息.
    返回给res 的值也是对的.只是 si 中没有数据
      

  3.   

    我找到一个使用SHARE_INFO_2的程序#include <stdio.h>
    #include <windows.h>
    #include <lm.h>
    #pragma hdrstop
    int main( void );int main( void )
    {
    //********************************* change these:
    wchar_t *pServer = L"\\\\tm1"; // server to run on
    wchar_t *pShare  = L"backup"; // sharename to query
    //********************************* NET_API_STATUS rc; // result code
    SHARE_INFO_2 *pSI;
    const char *sharetype;
    byte *pBuf; pBuf = NULL; // note -- LPTSTR cast needed because of bogus header files
    rc = NetShareGetInfo( (LPTSTR) pServer, (LPTSTR) pShare, 2, &pBuf ); if ( ( rc == NERR_Success || rc == ERROR_MORE_DATA ) && pBuf != NULL )
    {
    pSI = (SHARE_INFO_2 *) pBuf;
    switch ( pSI->shi2_type )
    {
    case STYPE_DISKTREE:
    sharetype = "disk";
    break;
    case STYPE_PRINTQ:
    sharetype = "print queue";
    break;
    case STYPE_DEVICE:
    sharetype = "comms queue";
    break;
    case STYPE_IPC:
    sharetype = "IPC";
    break;
    default:
    sharetype = "unknown";
    break;
    }
    printf( "Netname: %S\n", pSI->shi2_netname );
    printf( "Type:    %s\n", sharetype );
    printf( "Re:  %S\n", pSI->shi2_re );
    if ( pSI->shi2_max_uses == (DWORD) (-1L) )
    printf( "Max use: unlimited\n" );
    else
    printf( "Max use: %lu\n", pSI->shi2_max_uses );
    printf( "Cur use: %lu\n", pSI->shi2_current_uses );
    printf( "Path:    %S\n", pSI->shi2_path );
    } if ( pBuf != NULL )
    NetApiBufferFree( pBuf ); if ( rc != NERR_Success )
    printf( "Oops! Error %lu encountered!\n", rc ); return 0;
    }
      

  4.   

    改成SHARE_INFO_502         wchar_t *pServer = L"\\\\tm1"; // server to run on
    wchar_t *pShare  = L"backup"; // sharename to query
    //********************************* NET_API_STATUS rc; // result code
    SHARE_INFO_502 *pSI;
    // const char *sharetype;
    byte *pBuf; pBuf = NULL;
    CString sPathName; // note -- LPTSTR cast needed because of bogus header files
    rc = NetShareGetInfo( (LPTSTR) pServer, (LPTSTR) pShare, 502, &pBuf );
    if ( ( rc == NERR_Success || rc == ERROR_MORE_DATA ) && pBuf != NULL )
    {
    pSI = (SHARE_INFO_502 *) pBuf;

    sPathName.Format("%S",pSI->shi502_path);
    }
      

  5.   

    问题解决.指针问题.
    将res = NetShareGetInfo( NULL,
    (char *)wszShareName,
    502,
    (LPBYTE *)psi);
    改为res = NetShareGetInfo( NULL,
    (char *)wszShareName,
    502,
    (LPBYTE *)&psi);非常感谢mct1025(macuntao) .
    不过,msdn上面说, 那个指针应该是指向SHARE_INFO_502 结构的指针,
    怎么变成指向结构的指针的指针了. 是不是我英文太烂 :)
      

  6.   

    msdn上说,那个指针是指向接收数据的缓冲区
    返回时,缓冲区地址赋值给pBuf,不是SHARE_INFO_502结构赋值给pBuf
    所以要使用指针的指针Windows NT/2000下,缓冲区由系统分配,不需要初始化
    我认为设置psi = &si;根本就没有用
    调用NetShareGetInfo后,si的内容不会改变
    返回的信息存储在psi中
      

  7.   

    完整代码如下
    #include <lm.h> 
    #pragma comment(lib,"NETAPI32.LIB")
    ... NET_API_STATUS res;
    SHARE_INFO_502 * psi = NULL; 
    WCHAR wszShareName[255]; CString sPathName;
    TCHAR szPathName[255]; //转换成宽字符
    MultiByteToWideChar(CP_ACP, 0, lpszShareName, -1, wszShareName, 255); res = NetShareGetInfo( NULL,
    (char *)wszShareName,
    502,
    (LPBYTE *)&psi);
      
    //获取路径
    if(res == NERR_Success)
    {
    wsprintf(szPathName, "%S", psi->shi502_path);
    sPathName = szPathName;
    } //释放缓冲
    NetApiBufferFree(psi);    return sPathName;