Help class function passing variable by reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fortysixfish
    New Member
    • May 2008
    • 2

    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:

    Code:
    //Security.h
    #include <windows.h>
    
    class AccessToken
    {
    public:
    	void SetPrivilege(char* lpszPrivilege);
    	char* GetPrivilege();
    	bool IsPrivilegeEnabled();
    	void EnablePrivilege();
    	void SetToken();
    
    private:
    	TOKEN_PRIVILEGES tp;
    	LUID luid;
    	char* szPrivilege;
    	HANDLE hToken;
    };
    Code:
    //Method taken from Security.cpp
    void AccessToken::SetToken()
    {
    	OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken);
    }
    Code:
    //Main.cpp
    #include <cstdio>
    #include <windows.h>
    #include <tchar.h>
    #include "Security.h"
    
    int main()
    {
    	AccessToken at;
    	at.SetPrivilege("SeDebugPrivilege");
    	at.SetToken();
    	at.EnablePrivilege();
    
    	DWORD pid = 0;
    	HWND wnd_wmp = FindWindowW(NULL, _T("Windows Media Player"));
    	GetWindowThreadProcessId(wnd_wmp, &pid);
    
    	HANDLE h_wmp = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
    	printf("Error: %d\n", GetLastError());
    
    	system("PAUSE");
    	return 0;
    }
    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
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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.

    Comment

    • fortysixfish
      New Member
      • May 2008
      • 2

      #3
      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?

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        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.

        [CODE=cpp]
        int func(int& x, int& y){
        //Rare (I think this is right)
        int& z = x;
        //Common
        int w = x;
        }
        [/CODE]

        Comment

        Working...