|
The two different ways (you may find they are translated to the same asm, by the compiler).
[update] the first method's asm (SHIFT and AND operator) looks slightly faster)[/update]
struct BitField
{
unsigned int f1:4;
unsigned int f2:1;
unsigned int f3:3;
};
union MyData
{
unsigned char data;
BitField bf;
};
int main(int argc, char *argv[])
{
unsigned char data= rand();
int field1, field2, field3;
field1 = data & 0xF;
field2 = (data >> 4) & 1;
field3 = data >> 5;
printf("data=%d\n", data);
printf("(1) fields: %d, %d, %d\n", field1, field2, field3);
MyData md;
md.data = data;
field1 = md.bf.f1;
field2 = md.bf.f2;
field3 = md.bf.f3;
printf("(2) fields: %d, %d, %d\n", field1, field2, field3);
return 0;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
modified on Wednesday, March 10, 2010 5:31 AM
|
|
|
|
|
field 1.... bit 0-3 = > (data & 3)
and so onPress F1 for help or google it.
Greetings from Germany
|
|
|
|
|
KarstenK wrote: field 1.... bit 0-3 = > (data & 3)
field 1.... bit 0-3 = > (data & 15)
FFY.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I have created 3 modeless child dialogbox dynamically on Parent dialog,
CDialogBoxDlg *m_Sel[3];
for(int i=0;i<3;i++)
{
m_Sel[i]=new CDialogBoxDlg();
m_Sel[i]->Create (IDD_DIALOG,this);
m_Sel[i]->ShowWindow (SW_SHOW);
}
after creating ,if i click on title bar and move the dialog1 or dialog2 or dialog 3,then it will not look clearly,if i Resize the Border then it will looks clearly.
i tried Invalidate(), UpdateWindow() and RedrawWindow() then also it is not working please help.
-- Modified Wednesday, March 10, 2010 4:12 AM
|
|
|
|
|
Did you call UpdateWindow in this way
for(int i=0;i<3;i++)
{
m_Sel[i]=new CDialogBoxDlg();
m_Sel[i]->Create (IDD_DIALOG,this);
m_Sel[i]->ShowWindow (SW_SHOW);
m_Sel[i]->UpdateWindow();
} modified on Wednesday, March 10, 2010 3:59 AM
|
|
|
|
|
Also, you need to have an array of 3 locations - CDialogBoxDlg *m_Sel[3];
The valid locations are now from 0 to 2
|
|
|
|
|
I am creating a blocking API implementation. For that i am using a semaphore which is initialized once in the beginning of the program. A pseudo code of the scenario is given below:
startProgram()
{
initCount = 0; //first call shall wait
semVariable = CreateSemaphore (NULL, initCount, 1, NULL);
}
I am calling the semaphore in my API:
API_my_blocking_func()//thread1
{
//Wait until data is ready in do_unblock()
if(WAIT_TIMEOUT == WaitForSingleObject (semVariable, timeout)
{
return WAIT_TIMEOUT ;
}
continues........
}
meanwhile in another thread, when data is ready, I am releasing semaphore so that API_my_blocking_func() continues.
do_unblock() //trhead2
{
if(data is ready)
ReleaseSemaphore (semVariable, 1, NULL);
}
----
Now I am having a problem : If ReleaseSemaphore (with semVariable) is called two times due to some reason,
the WaitForSingleObject is not waiting!
To my expectation, since the maximum count is set to 1 , releasing the semaphore
shall not increment semVariable above 1, whatever be the number of calls to ReleaseSemaphore . So the very next
WaitForSingleObject shall block.
How to achieve multiple release of semaphore is allowed, and a single WaitForSingleObject on the same semaphore shall block?
|
|
|
|
|
its not clear what do you need. whenever the semaphore is released its count is increased and a WaitForSingleObject() will be unblocked. If you need to wait till a set of flags are signaled, use array of events or mutexes and use WaitForMultipleObjects()
|
|
|
|
|
Since 'maximum count is set to 1' during semaphore initialization, when semaphore is released its count is increased to 1 only. Not beyond (is this true?). So n number of releases shall keep maximum count 1. And next wait shall decrement by 1 and thus block.
What I observed is : If i release same semaphore multiple times, I cannot wait on that semaphore in a single wait call.
|
|
|
|
|
|
Hello All,
I am registering an ActiveX as follows:
CString ocxFile = L" /s \"" + ocxFile + L"\"";
HINSTANCE hInstance = ::ShellExecute(NULL, L"open", L"regsvr32.exe", ocxFile, NULL, SW_HIDE);
It works fine when user has admin rights . If user does not have admin rights then it can not register. Any solution of it ?
I am developing in XP and testing in xp and win7
Thanks,
Rahul
|
|
|
|
|
Registering an OCX is actually creating a few registry entries.
This requires admin rights by default unless the permission for the registry keys have been changed.
This is a good security feature.
|
|
|
|
|
Any way to give those permissions like showing UAC which will ask for an admin password.
If UAC is the solution then how to show UAC ?
Thanks,
Rahul
|
|
|
|
|
Take a look at the link I've provided to you in my other post. You need to specify this in the manifest file.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Thanks Rajesh!
I am looking into it.
|
|
|
|
|
Go to Project Properties -> Configuration Properties -> Linker -> Manifest File .
Change UAC Execution Level entry to requireAdministrator .
|
|
|
|
|
Thanks Superman & Rajesh,
It worked but my applicaiton stores some data in applicationData folder.
So if I use UAC then it uses applicaitonData folder of administrator account.
There should be some way to use it's own application folder.
I want this because my application is user specific so I use application data folder as a storage place. But by using UAC normal user's data is getting store inside administrators application data.
To retrieve application data folder I am using
SHGetSpecialFolderPath(0, strPath, CSIDL_APPDATA, FALSE);
which is giving admin's aplication data folder after using UAC.
|
|
|
|
|
I wish there should be some way to mark more than one answer as a Solution.
Your answer also deserve to be selected as "Solution". Sadly, I could not
|
|
|
|
|
You cannot execute this without running it as an administrator. Your application needs administrator privileges[^].
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Is there any way to change UAC only for few statements of code and after those statements, application will run in normal UAC.
|
|
|
|
|
rahul.kulshreshtha wrote: Is there any way to change UAC only for few statements of code and after those statements, application will run in normal UAC.
Take a look at this article: Riding the Vista UAC elevator, up and down[^]
From the article: If a non-elevated process needs to start an elevated one, all it has to do is call the ShellExecuteEx() API and supply the "runas" verb as one of its parameters. The source code of this article contains the function RunElevated() that does just that...
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Thanks Rajesh,
I studied that example and I used ShellExecuteEx() API and that worked. But still I am surprised why MS does not have UAC at statement level.. they are setting access level at process level. ShellExecuteEx delegate execution to Shell extensions. There may be requirements like "copying some file to restricted area" then how we can handle that only read/write should have a Elevated access and other parts of code should run as a limited access.
After reading Riding the Vista UAC elevator, up and down[^], I found that to change access level, developer was again restarting the application with different verbs in function "RunMyself" and hence changing the access level of whole process. But this can not be a practical scenario because practically we don't want to restart application again and again while we were in middle of some process. For example, I may want to read some registry values from HKLM from a non-admin account. So what can be the solution, I can not use ShellExecuteEx or "runas" verb here. Also I don't want to restart the exe/process in admin mode because other threads may be doing some other work. Please reply me if you know any way for this. I am hungry for more & more knowledge.
Anyways! my work was done in Win7. Now I am going to check if it works for a non-admin user in xp.
|
|
|
|
|
I don't know why but my one PC is showing "RunAs" dialog while other does not show.
I am using XP SP2 in both. Both are having multiple accounts. Both are running "Secondary Logon service". I tested in both users admin and limited. Any guess what can be the problem ?
|
|
|
|
|
Sorry, I could have only guessed that the secondary logon service is disabled in one (which you say is enabled). Look into other things, and let me too know if you figure it out.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Could you just call the register function directly?
HRESULT RegisterDLL(const char *dll_fspec)
{
HRESULT hr=E_FAIL;
HMODULE hMod=LoadLibrary(dll_fspec);
if (hMod)
{
HRESULT (__stdcall *pDllRegisterServer)(VOID);
pDllRegisterServer=(HRESULT (__stdcall *)(VOID))GetProcAddress(hMod,"DllRegisterServer");
if (pDllRegisterServer)
hr=pDllRegisterServer();
FreeLibrary(hMod);
}
return(hr);
}
|
|
|
|