Thursday, November 12, 2015

Decryption of DLGINIT .rc file entry

Visual Studio saves ComboBox item list in DLGINIT entries of .rc files as ASCIIZ strings, in WORD format:

IDD_DIALOG1 DLGINIT
BEGIN
    IDC_STR1, 0x403, 16, 0
0x7241, 0x2063, 0x6c63, 0x636f, 0x776b, 0x7369, 0x0065,

    IDC_STR2, 0x403, 18, 0
0x6c41, 0x6177, 0x7379, 0x6120, 0x2074, 0x6874, 0x2065, 0x6964, 0x0065,
    0
END

Using simple type conversion we can reveal, what do these strings contain.

Thursday, September 17, 2015

Understanding Mutex entity

Studying example from MSDN article Using Mutex Objects some questions arise about mutex entity.
Here are experimentally proved suggestions:
  1. Mutexes are like files, open and create them with CreateMutex, close with CloseHandle (for files you use CreateFile, CloseHandle)
  2. Unnamed mutex is like critical section
  3. Mutexes are deleted after CloseHandle when no application currently have them opened
You can understand these things from running 10 simple programs which in short do the following:
HANDLE h
main(){
  h = CreateMutex("Global\\SuperMutex")
  CreateThread(WriteToDatabase)
  CloseHandle(h)
}

WriteToDatabase()
{
  WaitForSingleObject(h)
  ReleaseMutex(h)
}
Some programs create h = 0x3c, some h = 0x44
Some create mutex via CreateMutex while having ERROR_ALREADY_EXISTS

Pic 1. Running 10 of these programs:


Example sources are here...

Monday, August 31, 2015

Test f() for multithreading

Here is a copy-paste template to test your code for multithreading. Let's say you want to test your function
int f() { ... }
for multithreading. Use this code to test f() running from number of threads simultaneously:

struct TESTPARAM
{
   int threadNo;
   LONG* m_pnCount;
};

DWORD WINAPI _msgTestThread(void* pParam)
{
   TESTPARAM* par = (TESTPARAM*)pParam;
   char buf[1014];

   int tmax = 1000;
   int tstep = 100;
   for (int t = 0; t<tmax; t+=tstep)
   {
      f();
      Sleep(tstep);
   }

   InterlockedDecrement(par->m_pnCount);
   delete par;
   return 0;
};

int Start()
{
   LONG nThreadCount = 0;
   int THREAD_MAX = 10;

   //msg::Message threads
   for (int i = 0; i<THREAD_MAX; i++)
   {
      //new, otherwise TESTPARAM will not reach thread in _msgTestThread,
      //and will be deleted on closing scope }
      TESTPARAM * par = new TESTPARAM;
      par->threadNo = i;
      par->m_pnCount = &nThreadCount;
      InterlockedIncrement(par->m_pnCount);
     
      DWORD dwThreadID = 0;
      CreateThread(0, 0, _msgTestThread, par, 0, &dwThreadID);
   }

   //Call window procedure until threads are finished
   while (nThreadCount > 0)
   {
      MSG msg;
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }
      Sleep(10);
   }
}