473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help class function passing variable by reference

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
3 1460
Laharl
849 Expert 512MB
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
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
Laharl
849 Expert 512MB
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

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

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
2
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.