Figures

Figure 1

Denying Everyone Access



ACL acl;
InitializeAcl(&acl, sizeof acl, ACL_REVISION);

SECURITY_DESCRIPTOR sd;
InitializeSecurityDescriptor(&sd,
    SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, TRUE,
                          &acl, FALSE);

SECURITY_ATTRIBUTES sa = { sizeof sa,
                           &sd, FALSE };
HANDLE h = CreateMutex(&sa, FALSE, "MyMutex");
_ASSERT(h);

DWORD result = WaitForSingleObject(h, 0);
_ASSERT(WAIT_OBJECT_0 == result);

HANDLE h2 = OpenMutex(SYNCHRONIZE, FALSE,
                      "MyMutex");
_ASSERT(0 == h2);
_ASSERT(ERROR_ACCESS_DENIED ==
        GetLastError());

ReleaseMutex(h);
CloseHandle(h);

Figure 4

Establishing the Client/Server Communication Link



// code to create the shared section excerpted from tester.cpp
void Tester::_testOOP( /* ... */ ){
    ...
    // create an inheritable section object
    SECURITY_ATTRIBUTES saInherit = { sizeof saInherit, 0, 
        TRUE };
    HANDLE hSection = CreateFileMapping(
        reinterpret_cast<HANDLE>( 0xFFFFFFFF ), &saInherit, 
        PAGE_READWRITE, 0, sizeof SurrogateInfo, 0 );
    _ASSERT( hSection );

    SurrogateInfo* psi = reinterpret_cast<SurrogateInfo*>(
        MapViewOfFile( hSection, FILE_MAP_READ | 
                       FILE_MAP_WRITE, 0, 0, 0 ) );
    _ASSERT( psi );

    // create inheritable event to synch the surrogate process
    psi->hBeginTest = CreateEvent( &saInherit, TRUE, FALSE, 0);
    _ASSERT( psi->hBeginTest );
    ...
}
// code to open and map the shared section in the secondary 
// process excerpted from surrogate.cpp in project Surrogate
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, char*, int ){
    ...
    // inherited handle of shared section is passed on cmd line
    wchar_t szModuleFileName[MAX_PATH];
    long nSectionHandle = 0;
    swscanf( GetCommandLine(), L"%s %d", szModuleFileName,
             &nSectionHandle );
    HANDLE hSection = reinterpret_cast<HANDLE>(nSectionHandle);
    _ASSERT( hSection );

    // map the section into memory
    SurrogateInfo* psi = reinterpret_cast<SurrogateInfo*>(
        MapViewOfFile( hSection, FILE_MAP_READ | 
                       FILE_MAP_WRITE, 0, 0, 0 ) );
    _ASSERTE(( 
       "Surrogate failed to open shared memory section", psi));
    ...
}
// code to send the marshaled interface pointer
// excerpted from surrogate.cpp in project Surrogate
void _sendTestObjectToCreator(SurrogateInfo* psi, 
                              IHandleTest* pht){
    // given a previously mapped block of shared memory,
    // drop a marshaled OBJREF into it
    IStream* pstm = 0;
    HRESULT hr = CreateStreamOnHGlobal( 0, FALSE, &pstm );
    _ASSERTE(( "CreateStreamOnHGlobal", SUCCEEDED( hr ) ));

    hr = CoMarshalInterface( pstm, IID_IHandleTest, pht,
         MSHCTX_LOCAL, 0, MSHLFLAGS_NORMAL );
    _ASSERTE(( "CoMarshalInterface", SUCCEEDED( hr ) ));
    pht->Release(); pht = 0;

    // see how many bytes were written by CoMarshalInterface
    __int64 zero = 0;
    __int64 cb;
    pstm->Seek( *reinterpret_cast<LARGE_INTEGER*>( &zero ),
                STREAM_SEEK_CUR,
                reinterpret_cast<ULARGE_INTEGER*>( &cb ) );

    // copy OBJREF and its length into the shared memory block
    psi->cbObjref = (DWORD) cb;
    HGLOBAL hmem = 0;
    GetHGlobalFromStream( pstm, &hmem );
    pstm->Release();

    CopyMemory(psi->objref, GlobalLock( hmem ), psi->cbObjref);
    GlobalUnlock( hmem );
    GlobalFree( hmem );
}
// code to retrieve the marshaled interface pointer
// excerpted from tester.cpp in project HandleTest
IHandleTest* Tester::_unmarshalInterface( SurrogateInfo& si ){
    // copy the marshaled OBJREF out of the shared memory 
    // block, and stick an IStream on top of it
    HANDLE hmem = GlobalAlloc( GMEM_FIXED, si.cbObjref );
    _ASSERT( hmem );
    CopyMemory( hmem, si.objref, si.cbObjref );
    IStream* pstm = 0;
    HRESULT hr = CreateStreamOnHGlobal( hmem, TRUE, &pstm );
    _ASSERT( SUCCEEDED( hr ) );

    // unmarshal the OBJREF to get a proxy (be sure you've
    // registered the proxy/stub DLL or this step will fail
    // with E_NOINTERFACE)
    IHandleTest* pht = 0;
    hr = CoUnmarshalInterface( pstm, IID_IHandleTest, 
                               (void**)&pht );
    _ASSERT( SUCCEEDED( hr ) );
    pstm->Release();
    return pht;
}
Page view tracker