By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Help class function passing variable by reference

P: 2
Hi I've been learning more and more on C/C++ as time goes on. Today I wanted to play around by creating a class that makes it easier to use Windows Tokens. I get stumped though when trying to pass a HANDLE variable by reference to a Win32 Function. It's suppose to set the HANDLE variable to a pointer but it just stays the same as I walk through the code.

This is my code:

Expand|Select|Wrap|Line Numbers
  1. //Security.h
  2. #include <windows.h>
  3.  
  4. class AccessToken
  5. {
  6. public:
  7.     void SetPrivilege(char* lpszPrivilege);
  8.     char* GetPrivilege();
  9.     bool IsPrivilegeEnabled();
  10.     void EnablePrivilege();
  11.     void SetToken();
  12.  
  13. private:
  14.     TOKEN_PRIVILEGES tp;
  15.     LUID luid;
  16.     char* szPrivilege;
  17.     HANDLE hToken;
  18. };
  19.  
Expand|Select|Wrap|Line Numbers
  1. //Method taken from Security.cpp
  2. void AccessToken::SetToken()
  3. {
  4.     OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken);
  5. }
  6.  
Expand|Select|Wrap|Line Numbers
  1. //Main.cpp
  2. #include <cstdio>
  3. #include <windows.h>
  4. #include <tchar.h>
  5. #include "Security.h"
  6.  
  7. int main()
  8. {
  9.     AccessToken at;
  10.     at.SetPrivilege("SeDebugPrivilege");
  11.     at.SetToken();
  12.     at.EnablePrivilege();
  13.  
  14.     DWORD pid = 0;
  15.     HWND wnd_wmp = FindWindowW(NULL, _T("Windows Media Player"));
  16.     GetWindowThreadProcessId(wnd_wmp, &pid);
  17.  
  18.     HANDLE h_wmp = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  19.     printf("Error: %d\n", GetLastError());
  20.  
  21.     system("PAUSE");
  22.     return 0;
  23. }
  24.  
hToken is my HANDLE variable privately declared in my AccessToken class.

Please correct my terminology if I misused and if there is a better way to do this I would really appreciate being told =D. Would really help me understand and get better at programming.

Thanks!

-Mack
May 11 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 849
References are almost never explicitly declared. You simply pass the argument you wish to pass by reference into the function without any special operators or syntax, and if the function prototype says a reference is to be passed, the compiler takes care of the rest.
May 11 '08 #2

P: 2
Ok I feel really silly now. I found out the real problem was, "An attempt was made to reference a token that does not exist."

Thank you for the input though, would you mind explaining to me what is meant by implicit and explicit declaration?
May 11 '08 #3

Expert 100+
P: 849
What I mean is that you almost never use, say, an int& to actually declare a reference in your code that's not a parameter to a function.

Expand|Select|Wrap|Line Numbers
  1. int func(int& x, int& y){
  2.  //Rare (I think this is right)
  3.   int& z = x;
  4.  //Common
  5.   int w = x;
  6. }
  7.  
May 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.