6/27/2004

在应用程序中添加宏支持的注意事项(Update)

用笔记本用多了,PC键盘用起来不是很习惯了。

在我的一篇文章脚本化浏览器(http://blog.csdn.net/jiangsheng/archive/2003/11/09/3795.aspx / http://www.csdn.net/develop/Article/21/21702.shtm)中描述了如何在应用程序中添加宏支持。在添加支持的时候需要注意的是,宏的运行环境——VBS脚本引擎——目前只支持变体数据类型。这造成的一个结果就是当你的应用程序触发一个事件的时候,如果其参数并不都是变体数据类型,那么你编写的宏不是总会被调用。解决的方法是总是声明你的事件参数为变体数据类型。

另外,MFC的向导会错误地为VARIANT_BOOL类型的事件参数生成一个BOOL类型的事件处理函数参数。这样的自动化对象在VB这样的宿主中运行的时候会造成访问越界。参见微软知识库文章Q199315 FIX: Method with BOOL* Parameter Type Overwriting Memory in Visual Basic 。解决的方法是手动修改类向导生成的代码。

6/24/2004

跨进程访问共享内存的权限问题

问:我在服务器上用 CreateFileMapping 创建了一段共享内存。让这个exe始终在服务器上跑。
  同时,别的用户在客户端用IE访问服务器,将要查询的数据通过C#制作的网页提交上来,服务器得到网页参数后,建立一个COM对象访问上一个exe的共享内存,然后将在共享内存中的查询结果返回给客户。
 问题是现在这个COM无法用openmapping访问exe的共享内存,提示 访问拒绝 。而我在服务器上随便建议一个工程编译成exe,文件就可访问这段共享内存!!为何在网页中就不成?COM难道要有什么 权限 设置.两个进程之间的权限整合方法是什么?怎么用DACL?

我用ATL写了一个Service,在这个Service中,我创建了一块共享内存(Memory Mapping)和一个Mutex
然后我在另一个普通程序中去访问这块共享内存和Mutex,但是,我用CreateMutex打开Mutex失败,GetLastError()返回5,含义是访问被拒绝!!
同样,我在用MapViewOfFile时,也得到同样的错误!!!!!!

我已经知道原因是因为在创建共享内存和Mutext时,SECURITY_ATTRIBUTES我设为NULL!!!

但是我没有解决的方法,希望各位大虾帮帮忙!

答:检查服务运行所使用的用户的权限。通常,为了安全起见,服务进程的拥有者权限是很低的。为了让服务进程访问对象,你需要在创建共享内存时指定一个更加广泛的的安全描述符,增加一个新的访问控制项目(ACE)给你的ASP进程的拥有者。默认的访问控制列表(ACL)只包含创建者和管理员组。

下列代码创建一个所有用户都可以访问的安全描述符。你可以在创建共享内存时使用这个安全描述符。
 CShareRestrictedSD ShareRestrictedSD;
    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,    // Current file handle.
 ShareRestrictedSD.GetSA(),   // Default security.
//    NULL,                             
    PAGE_READWRITE,                    // Read/write permission.
    0,                                 // Max. object size.
    FileSize,                                 // Size of hFile.
    MapName);            // Name of mapping object.

class CShareRestrictedSD 
{
public:
 CShareRestrictedSD();
 virtual ~CShareRestrictedSD();
 SECURITY_ATTRIBUTES* GetSA();
protected:
 PVOID  ptr;
 SECURITY_ATTRIBUTES sa;
 SECURITY_DESCRIPTOR sd;
};
//如果这家伙起作用,那么它的作者是jiangsheng;
//如果这家伙一点用没有,那我不知道它的作者。
PVOID BuildRestrictedSD(PSECURITY_DESCRIPTOR pSD) {

   DWORD  dwAclLength;

   PSID   psidEveryone = NULL;

   PACL   pDACL   = NULL;
   BOOL   bResult = FALSE;

   PACCESS_ALLOWED_ACE pACE = NULL;

   SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY  ;
  
   SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
  
   __try {

      // initialize the security descriptor
      if (!InitializeSecurityDescriptor(pSD,
            SECURITY_DESCRIPTOR_REVISION)) {
         printf("InitializeSecurityDescriptor() failed with error %d\n",
               GetLastError());
         __leave;
      }

      // obtain a sid for the Authenticated Users Group
      if (!AllocateAndInitializeSid(&siaWorld, 1,
            SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0,
            &psidEveryone)) {
         printf("AllocateAndInitializeSid() failed with error %d\n",
               GetLastError());
         __leave;
      }

      // NOTE:
      //
      // The Authenticated Users group includes all user accounts that
      // have been successfully authenticated by the system. If access
      // must be restricted to a specific user or group other than
      // Authenticated Users, the SID can be constructed using the
      // LookupAccountSid() API based on a user or group name.

      // calculate the DACL length
      dwAclLength = sizeof(ACL)
            // add space for Authenticated Users group ACE
            + sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)
            + GetLengthSid(psidEveryone);

      // allocate memory for the DACL
      pDACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
            dwAclLength);
      if (!pDACL) {
         printf("HeapAlloc() failed with error %d\n", GetLastError());
         __leave;
      }

      // initialize the DACL
      if (!InitializeAcl(pDACL, dwAclLength, ACL_REVISION)) {
         printf("InitializeAcl() failed with error %d\n",
               GetLastError());
         __leave;
      }
     
      // add the Authenticated Users group ACE to the DACL with
      // GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE access
      if (!AddAccessAllowedAce(pDACL, ACL_REVISION,
            GENERIC_ALL,
            psidEveryone)) {
         printf("AddAccessAllowedAce() failed with error %d\n",
               GetLastError());
         __leave;
      }

      // set the DACL in the security descriptor
      if (!SetSecurityDescriptorDacl(pSD, TRUE, pDACL, FALSE)) {
         printf("SetSecurityDescriptorDacl() failed with error %d\n",
               GetLastError());
         __leave;
      }

      bResult = TRUE;
    
   } __finally {

      if (psidEveryone) FreeSid(psidEveryone);
   }

   if (bResult == FALSE) {
      if (pDACL) HeapFree(GetProcessHeap(), 0, pDACL);
      pDACL = NULL;
   }

   return (PVOID) pDACL;
}

// The following function frees memory allocated in the
// BuildRestrictedSD() function
VOID FreeRestrictedSD(PVOID ptr) {

   if (ptr) HeapFree(GetProcessHeap(), 0, ptr);

   return;
}


CShareRestrictedSD::CShareRestrictedSD()
{
 ptr=NULL;
 sa.nLength = sizeof(sa);
 sa.lpSecurityDescriptor = &sd;
 sa.bInheritHandle = FALSE;
 // build a restricted security descriptor
 ptr = BuildRestrictedSD(&sd);
 if (!ptr) {
  TRACE("BuildRestrictedSD() failed\n");
 }
}

CShareRestrictedSD::~CShareRestrictedSD()
{
 if(ptr){
  FreeRestrictedSD(ptr);
 }
}
SECURITY_ATTRIBUTES* CShareRestrictedSD::GetSA()
{
 if(ptr){
  return &sa;
 }
 else
  return NULL;
}

更多信息可以参考

http://www.cnblogs.com/flier/archive/2004/07/15/24299.aspx

6/23/2004

重新启动服务

最近常去的论坛挂了,看起来是IIS进程系统资源占用太多了;服务器管理员又度周末去了,不能重启IIS,郁闷。CSDN服务器的IIS可能重启过于频繁了,分论坛页面经常来不及更新,自己发的帖子出现在列表里面的时候已经沉了,还是郁闷。

微软知识库文章Q194916 Restarting Web Services and Scheduled Tasks with a Batch File(http://support.microsoft.com/?kbid=194916 )中描述了定时用命令行重新启动IIS的方法,有想偷懒的网管可以试试。平台SDK中包含工具的SC.exe可以对服务进行更加详细的配置。

如果用程序来重新启动IIS的话,可以使用Shell对象的IShellDispatch2接口的ServiceStop 和ServiceStart方法。要使用Shell对象,可以调用CoCreateInstance,传递Shell对象的CLSID CLSID_SHELL来创建对象,然后查询其IShellDispatch/IShellDispatch2等接口来进行进一步的操作。

另外,WMI类Win32_ApplicationService 也提供了控制服务的方法StartService和StopService。cideguru上面有一个示例Using WMI to Extract Management Information(http://www.codeguru.com/Cpp/W-P/system/misc/article.php/c5675/)。WMI的优点是可以远程管理;缺点是配置起来比较麻烦。


当然,使用Windows服务API也是可以的——尽管需要OpenService之后再ControlService和StartService,看起来不是很简洁。平台SDK中的示例Sending Control Requests to a Service(http://msdn.microsoft.com/library/en-us/dllproc/base/sending_control_requests_to_a_service.asp)描述了这一点。用Windows 服务APIChangeServiceConfig和ChangeServiceConfig2可以对服务进行更加详细的配置。

这些方法都可以应用于其它服务。要查询服务的短名称的话,可以参考微软知识库文章Q271362 How to Find the Short Names of Services (http://support.microsoft.com/?kbid=271362
参考文档
Browse for a Folder the COM Way(Visual C++ Developer, Aug. 1999)

More Windows 2000 UI Goodies: Extending Explorer Views by Customizing Hypertext Template Fileshttp://msdn.microsoft.com/msdnmag/issues/0600/w2kui2/default.aspx