Click here to Skip to main content
15,923,845 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch20-Sep-19 5:06
mvahoney the codewitch20-Sep-19 5:06 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
TheGreatAndPowerfulOz20-Sep-19 5:59
TheGreatAndPowerfulOz20-Sep-19 5:59 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch20-Sep-19 6:00
mvahoney the codewitch20-Sep-19 6:00 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Dewey18-Sep-19 12:14
Dewey18-Sep-19 12:14 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch18-Sep-19 13:26
mvahoney the codewitch18-Sep-19 13:26 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. PinPopular
Richard Andrew x6418-Sep-19 14:25
professionalRichard Andrew x6418-Sep-19 14:25 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Steven121819-Sep-19 7:33
professionalSteven121819-Sep-19 7:33 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Gerardo Orozco19-Sep-19 4:48
Gerardo Orozco19-Sep-19 4:48 
+1 for this suggestion. Performance is much, much better than using the managed classes for memory mapped files. (I remember we clocked this being around 5x faster). We used this approach to store multimedia fingerprint data.

Here is a small snippet of code showing how we initialized the memory mapped file using C++/CLI - The data is accessible through the _pData (UInt32*) member.

cli
Int32 StationHashStorage::Open() {
   msclr::lock lock(_syncRoot);
   if( _isOpen )
      return 0;
   String^ fileName = GetFullFileName();
   
   _szInBytes = ComputeFileSizeInBytes(fileName);
   String^ mapExtension = GetFileExtension();
   String^ mapName = String::Format("{0}{1}_{2}", _stationId, _date.ToString("yyyyMMdd"), mapExtension);

   marshal_context context;
   LPCTSTR pMapName = context.marshal_as<const TCHAR*>(mapName);

   {
      msclr::lock lock( _openLock );
         // Try to see if another storage instance has requested the same memory-mapped file and share it
         _hMapping = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, pMapName);
         if( !_hMapping ) {
            // This is the first instance acquiring the file
            LPCTSTR pFileName = context.marshal_as<const TCHAR*>(fileName);
            // Try to open the existing file, or create new one if not exists
            _hFile = CreateFile(pFileName, 
                                GENERIC_READ | GENERIC_WRITE, 
                                FILE_SHARE_READ,
                                NULL,
                                OPEN_ALWAYS,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);
            if( !_hFile )
               throw gcnew IOException(String::Format(Strings::CreateFileFailed, GetLastError(), _stationId));
            _hMapping = CreateFileMapping(_hFile, 
                                          NULL,
                                          PAGE_READWRITE | SEC_COMMIT,
                                          0,
                                          _szInBytes,
                                          pMapName);
            if( !_hMapping ) 
               throw gcnew IOException(String::Format(Strings::CreateMappingFailed, GetLastError(), _stationId));
            _usingSharedFile = false;
         } else {
            _usingSharedFile = true;
         }
      }


   _pData = (UInt32*)::MapViewOfFile(_hMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);

   if( !_pData ) 
      throw gcnew IOException(String::Format(Strings::MapViewOfFileFailed, ::GetLastError(), _stationId));
   
   // warm up the view by touching every page
   Int32 dummy = 0;
   for( int i = 0; i < _szInBytes / sizeof(Int32); i+= 1024 ) {
      dummy ^=  _pData[i];
   }
   // return the dummy value to prevent the optimizer from removing the loop
   _isOpen = true;
   return 0;
}


cli
void StationHashStorage::Cleanup() {
     if( !_disposed ) {
      // dispose unmanaged resources here
      if( _pData ) {
         if( !UnmapViewOfFile(_pData) ) 
            LOG_ERROR(Strings::UnmapViewOfFileFailed, ::GetLastError(), _stationId);
         _pData = NULL;
      }

      if( _hMapping ) {
         if( !CloseHandle(_hMapping) ) 
            LOG_ERROR(Strings::CloseMappingFailed, ::GetLastError(), _stationId);
         _hMapping = NULL;
      }


      if( _hFile ) {
         if( !CloseHandle(_hFile) ) 
            LOG_ERROR(Strings::CloseFileFailed, ::GetLastError(), _stationId);
         _hFile = NULL;
      }
      _disposed = true;
   }
}


modified 19-Sep-19 12:37pm.

GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
#realJSOP18-Sep-19 5:24
professional#realJSOP18-Sep-19 5:24 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch18-Sep-19 5:25
mvahoney the codewitch18-Sep-19 5:25 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
#realJSOP18-Sep-19 5:34
professional#realJSOP18-Sep-19 5:34 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch18-Sep-19 5:36
mvahoney the codewitch18-Sep-19 5:36 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian18-Sep-19 20:18
professionalRajesh R Subramanian18-Sep-19 20:18 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch18-Sep-19 21:25
mvahoney the codewitch18-Sep-19 21:25 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian18-Sep-19 22:21
professionalRajesh R Subramanian18-Sep-19 22:21 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch19-Sep-19 3:04
mvahoney the codewitch19-Sep-19 3:04 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian19-Sep-19 3:20
professionalRajesh R Subramanian19-Sep-19 3:20 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
honey the codewitch19-Sep-19 3:31
mvahoney the codewitch19-Sep-19 3:31 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian19-Sep-19 3:33
professionalRajesh R Subramanian19-Sep-19 3:33 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
TheGreatAndPowerfulOz20-Sep-19 4:23
TheGreatAndPowerfulOz20-Sep-19 4:23 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian20-Sep-19 5:55
professionalRajesh R Subramanian20-Sep-19 5:55 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
TheGreatAndPowerfulOz20-Sep-19 6:01
TheGreatAndPowerfulOz20-Sep-19 6:01 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian20-Sep-19 6:10
professionalRajesh R Subramanian20-Sep-19 6:10 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
TheGreatAndPowerfulOz20-Sep-19 7:11
TheGreatAndPowerfulOz20-Sep-19 7:11 
GeneralRe: I thought .NET was supposed to make things easier, if anything, than unmanaged code. Pin
Rajesh R Subramanian20-Sep-19 7:22
professionalRajesh R Subramanian20-Sep-19 7:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.