titbits from the world less travelled

Archive for July, 2010

Implementing simple multithreading in C++

Bare steps to implement multi threading in C++:

a. Declare a global variable, to hold the reference counts.

b. Declare CRITICAL SECTION

code:

CRITICAL_SECTION cs_test;
int loadCnt=0;

c. Now everytime you access the code you want to be protected when multiple threads access them

InitializeCriticalSection(&cs_test);
EnterCriticalSection(&cs_test);

if(loadCnt >0)
{
loadCnt++;
return
}else{

//your code here

}

LeaveCriticalSection(&cs_test);

return;

d. you could do the same for de initializing also.

e. But the best way to do the above is to implement them using class and objects.

posted by admin in c++ and have No Comments

Windows developer tools you need to have

Some tools which I really find helpful are:

a. Multimon – Support for multiple monitors

b. Process Monitor

c. Process Explorer

d. Desktops – Desktops allows you to organize your applications on up to four virtual desktops.

e. OleView – For COM purposes

f. Spy++ – For windows GUI handling

g. Resource hunter – Reading resource files. It will be helpful when your working on multiple languages.

posted by admin in c++, windows and have No Comments

Tip 1: Optimize code to run faster

1. Use constants with reference when passing values in between methods.

Advantage: It will reduce space and will be faster. If you use a new variable then an additional copy is made and there is a small memory hit.

Ex: method(string val)

optimal usage: method(const string &val)

Here we are passing the value by reference.

posted by admin in c++ and have No Comments

ACL: Denying read access to a file in VC++

Snippet of the code to set the permissions and create a file while not granting read access to it:

DWORD dwRes;
PSID pEveryoneSID = NULL, pAdminSID = NULL;
PACL pACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea[2];
SID_IDENTIFIER_AUTHORITY SIDAuthWorld =
SECURITY_WORLD_SID_AUTHORITY;
SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
SECURITY_ATTRIBUTES sa;

// Create a well-known SID for the Everyone group.
if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0,
&pEveryoneSID))
{
printf(“AllocateAndInitializeSid Error %u\n”, GetLastError());
goto Cleanup;
}

// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow Everyone read access to the key.
ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
ea[0].grfAccessPermissions = KEY_READ;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance= NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;

// Create a SID for the BUILTIN\Administrators group.
if(! AllocateAndInitializeSid(&SIDAuthNT, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdminSID))
{
printf(“AllocateAndInitializeSid Error %u\n”, GetLastError());
goto Cleanup;
}

// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow the Administrators group full access to
// the key.
ea[1].grfAccessPermissions = KEY_ALL_ACCESS;
ea[1].grfAccessMode = SET_ACCESS;
ea[1].grfInheritance= NO_INHERITANCE;
ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
ea[1].Trustee.ptstrName = (LPTSTR) pAdminSID;

// Create a new ACL that contains the new ACEs.
dwRes = SetEntriesInAcl(2, ea, NULL, &pACL);
if (ERROR_SUCCESS != dwRes)
{
printf(“SetEntriesInAcl Error %u\n”, GetLastError());
goto Cleanup;
}

// Initialize a security descriptor.
pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);
if (NULL == pSD)
{
printf(“LocalAlloc Error %u\n”, GetLastError());
goto Cleanup;
}

if (!InitializeSecurityDescriptor(pSD,
SECURITY_DESCRIPTOR_REVISION))
{
printf(“InitializeSecurityDescriptor Error %u\n”,
GetLastError());
goto Cleanup;
}

// Add the ACL to the security descriptor.
if (!SetSecurityDescriptorDacl(pSD,
TRUE, // bDaclPresent flag
pACL,
FALSE)) // not a default DACL
{
printf(“SetSecurityDescriptorDacl Error %u\n”,
GetLastError());
goto Cleanup;
}

// Initialize a security attributes structure.
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;

// Use the security attributes to set the security descriptor
// when you create a file.
HANDLE hFile = CreateFile( L”C:\\readAccessDenied.txt”,
GENERIC_READ,//FILE_READ_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&sa,
CREATE_NEW,
0,
NULL );
CloseHandle(hFile);

Cleanup:

if (pEveryoneSID)
FreeSid(pEveryoneSID);
if (pAdminSID)
FreeSid(pAdminSID);
if (pACL)
LocalFree(pACL);
if (pSD)
LocalFree(pSD);

posted by admin in Testing, c++ and have No Comments