//strDir为要共享的目录
//strShareName为共享的名
//bShareFlag为TRUE共享目录,FALSE取消共享
//此函数在WIN2K/WINXP下面运行BOOL CreateShare_(CString strDir, CString strShareName, BOOL bShareFlag)
{
//*
    NET_API_STATUS res;
    SECURITY_DESCRIPTOR sd;
    SHARE_INFO_502 si502;  TCHAR szShareName[255];
WCHAR wszShareName[255];
TCHAR szShareDir[255];
WCHAR wszShareDir[255];
 
if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
return FALSE ;    sprintf(szShareName, strShareName);
sprintf(szShareDir, strDir);
MultiByteToWideChar(CP_ACP, 0, szShareName, -1, wszShareName, 255);
MultiByteToWideChar(CP_ACP, 0, szShareDir, -1, wszShareDir, 255);    si502.shi502_netname = (char*)wszShareName;
    si502.shi502_type = STYPE_DISKTREE;
    si502.shi502_re = NULL;
    si502.shi502_max_uses = 100;
    si502.shi502_permissions = ACCESS_ALL;
    si502.shi502_current_uses = 0;
    si502.shi502_path = (char*)wszShareDir;
    si502.shi502_passwd = NULL;
    si502.shi502_reserved = 0;
    si502.shi502_security_descriptor = &sd;    if(!bFlag)
res=NetShareDel(NULL, (char*)wszShareName,NULL);
    else
    res=NetShareAdd(
        NULL,         // share is on local machine
        502,            // info-level
        (LPBYTE)&si502, // info-buffer
        NULL            // don't bother with parm
        );    if(res==NERR_Success)
{
         return TRUE; // Success
 SHChangeNotify(SHCNE_NETSHARE, SHCNF_PATH, (LPCVOID)szShareDir, NULL);
}
      else if(res==ERROR_ACCESS_DENIED)
AfxMessageBox("The user does not have access to the requested information");
  else if(res==ERROR_INVALID_LEVEL)
AfxMessageBox("The value specified for the level parameter is invalid. ");
  else if(res==ERROR_INVALID_NAME)
AfxMessageBox("The character or file system name is invalid.");
      else if(res==ERROR_INVALID_PARAMETER)
      AfxMessageBox("The specified parameter is invalid.");
  else if(res==NERR_DuplicateShare)
AfxMessageBox("The share name is already in use on this server.");
  else if(res==NERR_RedirectedPath)
AfxMessageBox("The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.");
  else if(res==NERR_UnknownDevDir)
AfxMessageBox("The device or directory does not exist. ");
  else
AfxMessageBox("CreateShare Error");
//*/
return FALSE; // Failure
}方法在普通MFC应用程序里面不会出现什么异常,但是一旦封装到MFC dll里面,一大堆错误,非常奇怪。虽然错误一堆,但是共享执行结果一样成功,真不明白原因是什么? 高手指点。错误参考跟帖。

解决方案 »

  1.   

    错误信息如下:
    -------------------------------------------------------
    Debug Assertion Failed!Program: E:\VcDll\TestVodDll\Debug\TestVodDll.exeFile: dbgheap.c
    Line: 1011Expression: _CrtIsValidHeapPointer(pUserData)
    For information on how your program can cause an assertion
    failure, see the Visual C++ documentation on asserts.
    (Press Retry to debug the application)------------------------------------------------
    终止(A)         重试(R)         忽略(I)
    我选择忽略
    ------------------------------------------------           Debug Assertion Failed!            Program: E:\VcDll\TestVodDll\Debug\TestVodDll.exe            File: dbgheap.c
                Line: 1076            Expression: _pFirstBlock == pHead
                For information on how your program can cause an assertion
                failure, see the Visual C++ documentation on asserts.
                (Press Retry to debug the application)
    ------------------------------------------------
    终止(A)         重试(R)         忽略(I)
    我选择忽略
    ------------------------------------------------           Debug Assertion Failed!            Program: E:\VcDll\TestVodDll\Debug\TestVodDll.exe            File: dbgheap.c
                Line: 1011            Expression: _CrtIsValidHeapPointer(pUserData)
                For information on how your program can cause an assertion
                failure, see the Visual C++ documentation on asserts.
                (Press Retry to debug the application)
    ------------------------------------------------
    终止(A)         重试(R)         忽略(I)
    我选择忽略
    ------------------------------------------------
    -----------------------------------------------------------------------------------------------------
    最后蹦出对话框,信息如下:
    "0x10213035"指令引用的"0xddddddf1"的内存。该内存不能为"read"。要终止程序,清单击"确定"
      

  2.   

    放在mfcdll没有问题
    我测试了!
    可能你的导出文件有问题
    ----------------------------
    客户端:
    __declspec(dllimport) BOOL CreateShare_(CString strDir, CString strShareName, BOOL bShareFlag);
    void CTestNetShareDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    CreateShare_("d:\\vc++6.0","TestShareDir",true);}
    ------------------------
    dll部分
    CTestMFCDllApp theApp;//strDir为要共享的目录
    //strShareName为共享的名
    //bShareFlag为TRUE共享目录,FALSE取消共享
    //此函数在WIN2K/WINXP下面运行BOOL CreateShare_(CString strDir, CString strShareName, BOOL bShareFlag)
    {
    //*
        NET_API_STATUS res;
        SECURITY_DESCRIPTOR sd;
        SHARE_INFO_502 si502;  TCHAR szShareName[255];
    WCHAR wszShareName[255];
    TCHAR szShareDir[255];
    WCHAR wszShareDir[255];
     
    if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
    return FALSE ;    sprintf(szShareName, strShareName);
    sprintf(szShareDir, strDir);
    MultiByteToWideChar(CP_ACP, 0, szShareName, -1, wszShareName, 255);
    MultiByteToWideChar(CP_ACP, 0, szShareDir, -1, wszShareDir, 255);    si502.shi502_netname = (char*)wszShareName;
        si502.shi502_type = STYPE_DISKTREE;
        si502.shi502_re = NULL;
        si502.shi502_max_uses = 100;
        si502.shi502_permissions = ACCESS_ALL;
        si502.shi502_current_uses = 0;
        si502.shi502_path = (char*)wszShareDir;
        si502.shi502_passwd = NULL;
        si502.shi502_reserved = 0;
        si502.shi502_security_descriptor = &sd;    if(!bShareFlag)
    res=NetShareDel(NULL, (char*)wszShareName,NULL);
        else
        res=NetShareAdd(
            NULL,         // share is on local machine
            502,            // info-level
            (LPBYTE)&si502, // info-buffer
            NULL            // don't bother with parm
            );    if(res==NERR_Success)
    {
             return TRUE; // Success
     SHChangeNotify(SHCNE_NETSHARE, SHCNF_PATH, (LPCVOID)szShareDir, NULL);
    }
          else if(res==ERROR_ACCESS_DENIED)
    AfxMessageBox("The user does not have access to the requested information");
      else if(res==ERROR_INVALID_LEVEL)
    AfxMessageBox("The value specified for the level parameter is invalid. ");
      else if(res==ERROR_INVALID_NAME)
    AfxMessageBox("The character or file system name is invalid.");
          else if(res==ERROR_INVALID_PARAMETER)
          AfxMessageBox("The specified parameter is invalid.");
      else if(res==NERR_DuplicateShare)
    AfxMessageBox("The share name is already in use on this server.");
      else if(res==NERR_RedirectedPath)
    AfxMessageBox("The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.");
      else if(res==NERR_UnknownDevDir)
    AfxMessageBox("The device or directory does not exist. ");
      else
    AfxMessageBox("CreateShare Error");
    //*/
    return FALSE; // Failure
    }-------------
    ; TestMFCDll.def : Declares the module parameters for the DLL.LIBRARY      "TestMFCDll"
    DESCRIPTION  'TestMFCDll Windows Dynamic Link Library'EXPORTS
        ; Explicit exports can go here
    CreateShare_
      

  3.   

    [email protected]
    不相信的话,将我的测试工程给你!
      

  4.   

    可能你的dll和客户端得编译模式不一样!
    比如一个是debug,而另一个是release
    改成一样的就行了
      

  5.   

    我这儿一切配置正常,都是Debug,我的动态连接库是直接申明为:
    #define VOD_API __declspec(dllexport) VOD_API int CreateShare(CString strDir, CString strShareName);我的机器装有最新的Microsoft SDK,不知道这会不会影响。要么发一个你的测试工程给我吧!非常感谢! 
    我的Email: [email protected]
      

  6.   

    我的做法肯定不会有问题,我是用依奈工程形式实现的,每次dll Mfc一旦编译完毕(Debug编译),会自动拷贝一个dll文件到测试工程的Debug目录。所以不会出错。