|
Yes,we can use Macro in C/C++. It seems that you know ASM,if so,there is Macro syntax,too.
And if you have a few ASM lines would be used for several function,if in C/C++,I want to say,you can use Macro too,using it like this
#define ASM_MACRO __asm{ \ //for multiline Macro
... \ //there are some asm code lines
}
I wish it will help to you.
Regards.
|
|
|
|
|
I'm investigating garbage collection methodologies for C++.
I've been reading about smart pointers, and they seem to make sense, except for this one point that I cannot understand:
In the case of a container object, such as a hash table or even just an array of smart pointers, how do the pointers know that they are still "alive" once they are put into the container?
Thanks if someone can help me understand how that works.
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
Independent123 wrote: how do the pointers know that they are still "alive" once they are put into the container?
The objects exist until they go out of scope, at which time their destructors are called.
mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: The objects exist until they go out of scope
That I understand, but, for instance, a member variable in a class instance doesn't really have a scope, does it?
It exists as long as the class instance exists, even though it's not actively being used in any expressions.
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
It is basically the Object lifetime not the scope, when the smart pointer get destroyed the objected it points to is also get destroyed. Scope can lead to end of object lifetime for example when a control leaves a block the local varable gets destroyed. smart pointer in a container class has lifetime as that of container class, it exists as long as class instance exists.
|
|
|
|
|
You have to be more specific on the type of smart pointer you are talking about. 'Smart pointer' is a generic label that is used to describe a number of techniques.
In the context of GC you are likely talking about reference counting.
In this case maintaining a smart pointer in the container ensures that the objects reference counts stays above 0.
...cmk
The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.
- John Carmack
|
|
|
|
|
|
Hi buddy!
How can I get an unique ID for each computer, so that it doesn't change after installing a new OS.
Thanks...
|
|
|
|
|
Microsoft has invested millions of dollars into developing such technology.
Their activation system takes a snapshot of all the hardware in the machine and produces a hash of many different parameters.
It isn't a simple answer.
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
Hello,
I am programming in MS VS c++.
In my application I want to get the information: Is my software is running "as administrator" or not.
On Windows Vista an administrator can be logged in, but it could be, that the application (my software) is not running "as administrator".
If the user starts a software "as administrator" so he has an other access to files, like ini- or txt-files.
Background of my question:
--------------------------
My software application is installed in the folder "c:\Program Files\myapplication\myApp.exe"
In this folder I store some configurations in the file "myconfig.ini".
If I want to change some entries in the ini-File (with myApp.exe), so under Windows Vista the changed file will be saved in the following folder:
C:\Users\...\AppData\Local\VirtualStore\Program Files\myapplication\myconfig.ini
But if the software "myApp.exe" is running "as administrator", so the changed ini-file will be stored in the original folder "c:\Program Files\myapplication\myconfig.ini".
So how I can to get the information (during run time): is myApp.exe is running as administartor or not? I need this information, because I want inform the user that the changed confiuration file will not be stored in the original installed folder, but in an another virtualstore-folder, if the software is not running as administartor.
Does somebody has a solution for this problem?
|
|
|
|
|
BOOL IsUserAdmin(VOID)
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if(b)
{
if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
{
b = FALSE;
}
FreeSid(AdministratorsGroup);
}
return(b);
}
Best Wishes,
-Randor
|
|
|
|
|
Thanks for your help.
I have insert "IsUserAdmin(void)", but I get the error: C2065: 'CheckTokenMembership' : undeclared identifier
I have found an other way to check the administrator status:
int IsAdminRunning(void)<br />
{<br />
bool IsMember = FALSE;<br />
ATL::CAccessToken ProcToken;<br />
ATL::CAccessToken ImpersonationToken;<br />
ATL::CSid UserSid(Sids::Admins());<br />
<br />
ProcToken.GetEffectiveToken(TOKEN_READ | TOKEN_DUPLICATE);<br />
ProcToken.CreateImpersonationToken(&ImpersonationToken);<br />
ImpersonationToken.CheckTokenMembership(UserSid, &IsMember);<br />
<br />
return IsMember;<br />
}<br />
But here I have the problem to use the ATL-Control.
If I insert a new ATL-Object in my project, so how I can use the CAccessToken and CSid?
|
|
|
|
|
Simon1 wrote: I have insert "IsUserAdmin(void)", but I get the error: C2065: 'CheckTokenMembership' : undeclared identifier
The sample code Randor given is here,
CheckTokenMembership[^]
Have you included "windows.h"
IsUserAnAdmin()[^] API is available, the link as hint that this API might be altered or unavailable in subsequent versions of Microsoft Windows.
modified on Monday, March 31, 2008 1:58 AM
|
|
|
|
|
|
Look in the 'Remarks' section:
This function is a wrapper for CheckTokenMembership. It is recommended to call that function directly to determine Administrator group status rather than calling IsUserAnAdmin.
Best Wishes,
-David Delaune
|
|
|
|
|
Interesting - they must have added that caveat when they updated the docs for Vista. I looked in my local MSDN page before posting and there is no remarks section. I got the URL but didn't read the online MSDN page when I posted.
Judy
|
|
|
|
|
See - How to tell if the current user is in administrators group programmatically
http://blogs.msdn.com/junfeng/archive/2007/01/26/how-to-tell-if-the-current-user-is-in-administrators-group-programmatically.aspx[^]
You can use CheckTokenMembership (http://msdn2.microsoft.com/en-us/library/aa376389.aspx) to check whether a user is in administrators group. There is an example in the MSDN document.
The example will not work in Windows Vista. In Windows Vista, even if the user is in administrators group, the OS will create a filtered user token when user log on. The SID of administrators group is not included in the filtered user token. The full token is linked to the filtered user token, and can be retrieved with API GetTokenInformation with the new TokenLinkedToken information type.
The following is the sample code. You need to download Vista SDK to compile it. Please make sure you read the article in the reference section.
HRESULT IsUserAdmin(BOOL *pIsAdmin)
{
int b;
HANDLE hProcess = NULL;
HANDLE hProcessToken = NULL;
HANDLE hLinkedToken = NULL;
BOOL fIsAdmin = FALSE;
DWORD dwLength = 0;
OSVERSIONINFO osver = {sizeof(OSVERSIONINFO)};
HRESULT hr = S_OK;
*pIsAdmin = FALSE;
// get handle to our process token
hProcess = GetCurrentProcess();
if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit;
}
// get admin SID
char AdminSID[SECURITY_MAX_SID_SIZE];
dwLength = sizeof(AdminSID);
if(!CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, &AdminSID, &dwLength))
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit;
}
// check to see if the current token contains admin SID
if (!CheckTokenMembership( NULL, &AdminSID, &fIsAdmin))
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit;
}
if (fIsAdmin)
{
// printf("The user is in admin group, and the process is elevated.\n");
*pIsAdmin = TRUE;
goto Exit;
}
// if the current token does not contain admin SID, it does not mean
// that the current user is not admin. In Vista by default the token of
// users in administrator group has the the admin SID filtered. We nee
// to use the unfiltered token to do the check.
if (!GetVersionEx(&osver))
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit;
}
// XP and below, we are done.
if (osver.dwMajorVersion < 6)
{
// printf("The user is not in admin group.\n");
goto Exit;
}
// get handle to linked token (will have one if we are lua)
if (!GetTokenInformation( hProcessToken,
TokenLinkedToken,
(VOID*) &hLinkedToken,
sizeof(HANDLE),
&dwLength) )
{
b = GetLastError();
if ( b == ERROR_NO_SUCH_LOGON_SESSION
|| b == ERROR_PRIVILEGE_NOT_HELD)
{
// printf("The user is not in admin group.\n");
goto Exit;
}
hr = HRESULT_FROM_WIN32(b); // a real error
goto Exit;
}
if (!CheckTokenMembership( hLinkedToken, &AdminSID, &fIsAdmin))
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit;
}
if (fIsAdmin)
{
// printf("The user is in admin group, and the process is not elevated.\n");
*pIsAdmin = TRUE;
}
else
{
// printf("The user is not in admin group.\n");
}
Exit:
if (hProcess)
{
CloseHandle(hProcess);
}
if (hProcessToken)
{
CloseHandle(hProcessToken);
}
if (hLinkedToken)
{
CloseHandle(hLinkedToken);
}
return hr;
}
|
|
|
|
|
Hello everyone,
Two basic questions about function invocation from STA to MTA.
1.
In my understaning, the owning thread of STA should not touch MTA component, and also in MTA there is no message queue for MTA owning thread to pumping messages.
My question is, if the owning thread of STA calls MTA component (by interface pointer), which thead is responsible for executing in MTA? From my above analysis, seems both STA owner thread (can not touch MTA component) and MTA owner thread (can not receive calls from STA through message queue, snice no message queue in MTA.)
2.
Before function call returns from MTA, will STA thread waiting or it could execute in asynchronous way?
thanks in advance,
George
|
|
|
|
|
I need to get the color from a specific pixel on the screen.
this pixel is not a part of my console application.
my basic method is:
keybd_event(VK_SNAPSHOT,0x9e, 0, 0);
keybd_event(VK_SNAPSHOT,0x9e, KEYEVENTF_KEYUP, 0);
if(OpenClipboard(NULL))
{
HBITMAP hClipboardBmp = (HBITMAP) GetClipboardData(CF_BITMAP);
}
but i wonder if theres a way to get that pixel(s) directly from the screen.
thank you very much for your time and effort
|
|
|
|
|
HDC hdc = GetDC(NULL);
COLORREF cr = GetPixel(hdc, x,y);
ReleaseDC(hdc);
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
|
|
|
|
|
it's perfect, thank you very much!
|
|
|
|
|
It seems you need toget pixel of a bmp file that you read of clipboard,right?
|
|
|
|
|
Hello everyone,
I posted the code and related compile error below. My analysis below,
1. I think the reason of compile error is, the copy constructor of Goo accepts const reference as input parameter, so the pf member of input parameter is also treated const?
2. And since auto_ptr does not have a copy constructor which accepts const reference auto_ptr as input parameter, the compile error occurs.
My analysis (1) and (2) are both correct?
What makes me confused is it seems the compile error message does not reflect my analysis (1) and (2) above. Any ideas?
#include <memory>
using namespace std;
class Foo {
};
class Goo {
public:
auto_ptr<Foo> pf;
Goo (const Goo& input)
{
this -> pf = input.pf;
}
};
1>d:\visual studio 2008\projects\test0330\test0330\main.cpp(18) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const std::auto_ptr<_Ty>' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=Foo
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\memory(689): could be 'std::auto_ptr<_Ty> &std::auto_ptr<_Ty>::operator =<Foo>(std::auto_ptr<_Ty> &) throw()'
1> with
1> [
1> _Ty=Foo
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\memory(701): or 'std::auto_ptr<_Ty> &std::auto_ptr<_Ty>::operator =(std::auto_ptr<_Ty> &) throw()'
1> with
1> [
1> _Ty=Foo
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\memory(707): or 'std::auto_ptr<_Ty> &std::auto_ptr<_Ty>::operator =(std::auto_ptr_ref<_Ty>) throw()'
1> with
1> [
1> _Ty=Foo
1> ]
1> while trying to match the argument list '(std::auto_ptr<_Ty>, const std::auto_ptr<_Ty>)'
1> with
1> [
1> _Ty=Foo
1> ]
thanks in advance,
George
|
|
|
|
|
IMHO your analysis is correct (both point 1 and 2. However, with regard to point 1, my interpretation is: since Goo copy constructor declares its argument const , then its content cannot be modified, but assigment operator of auto_ptr modify its argument, hence the problem).
George_George wrote: What makes me confused is it seems the compile error message does not reflect my analysis (1) and (2) above. Any ideas?
I think that (it's just an hypothesis), since no auto_ptr copy constructor can match the this -> pf = input.pf; , the compiler tries to search for an assignment operator match, but it fails again and gives such error.
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
|
|
|
|
|
Cool, CPallini!
1.
CPallini wrote: I think that (it's just an hypothesis), since no auto_ptr copy constructor can match the this -> pf = input.pf;, the compiler tries to search for an assignment operator match, but it fails again and gives such error.
You mean compiler search for all forms of assignment operator of auto_ptr, then no one matches so the compile errors come?
2.
The source of error is because the basic C++ ruls is violated -- can not assign const reference (input.pf) to non-const reference (input parameter of assignment operator of class auto_ptr)?
regards,
George
|
|
|
|
|