Multidesk program doesn't work

Hello, I wrote a desktop switching program but it doesn't work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// deskman.hpp
#include <string>
#include <windows.h>
#include <vector>



#define ERR_ALREADY_EXISTS   1
#define ERR_NOT_EXISTS       2
#define ERR_CREATE           3
#define ERR_SWITCH           4



struct DESKTOP_DATA
{
HDESK hDesk;
std::string name;
};


class DesktopManager
{
protected:
  friend BOOL CALLBACK fed_clbk(LPSTR name,LPARAM lparam);
  std::vector<DESKTOP_DATA>desktops;
  int active_desktop;

  int add_desktop(const DESKTOP_DATA);
  void find_existing_desktops();

  DESKTOP_DATA &get_desktop_by_name(std::string name);
  DESKTOP_DATA &get_desktop_by_number(int i);

public:
  DesktopManager();
  int AddDesktop(std::string name);

  void RemoveDesktop(int desk);
  void RemoveDesktop(std::string name);

  int SetActive(int desk);
  int SetActive(std::string name);

  DESKTOP_DATA EnumDesktop(int i);

  int NumberOfDesktops();

};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// deskman.cpp
#include "deskman.hpp"
#include <windows.h>
#include <iostream>
using namespace std;

void LastError() ;


DesktopManager::DesktopManager()
{
   active_desktop=0;
   find_existing_desktops();

}

int DesktopManager::AddDesktop(string name)
{
    DESKTOP_DATA tmp;
    tmp.hDesk=CreateDesktop(name.c_str(),NULL,NULL,GENERIC_ALL,DESKTOP_CREATEWINDOW,0);
    if(tmp.hDesk==INVALID_HANDLE_VALUE)
          return ERR_CREATE;

    tmp.name=name;
    return add_desktop(tmp);

}


int DesktopManager::NumberOfDesktops()
{
    return desktops.size();
}


DESKTOP_DATA DesktopManager::EnumDesktop(int i)
{
   if(i>desktops.size())
       throw(ERR_NOT_EXISTS);
   return desktops[i];
}


int DesktopManager::SetActive(int desk)
{
    DESKTOP_DATA dt;
    dt=get_desktop_by_number(desk);
    if(!SwitchDesktop(dt.hDesk))
        return ERR_SWITCH;
    SetThreadDesktop(dt.hDesk);
    return 0;
}


int DesktopManager::SetActive(std::string name)
{
    DESKTOP_DATA dt;
    try
      {
        dt=get_desktop_by_name(name);
      }
    catch(...)
     {
         return ERR_NOT_EXISTS;
     }
    if(!SwitchDesktop(dt.hDesk)) // --- ERROR HERE -----
      {
          if(GetLastError()!=0)
              LastError();

          return ERR_SWITCH;
      }

    SetThreadDesktop(dt.hDesk);
    return 0;
}




// ---------------------------------------------------------------

BOOL CALLBACK fed_clbk(LPSTR name,LPARAM lparam)
{
DesktopManager *dm=reinterpret_cast<DesktopManager*>(lparam);
DESKTOP_DATA dt;

dt.name=name;
dt.hDesk=OpenDesktop(name,0,0,GENERIC_ALL);

dm->add_desktop(dt);

return TRUE;
}


void DesktopManager::find_existing_desktops()
{
EnumDesktops(0,fed_clbk,(LPARAM)this);
}


int DesktopManager::add_desktop(const DESKTOP_DATA dt)
{
    for(int i=0;i<desktops.size();i++)
      {
          if(desktops[i].name==dt.name)
              return ERR_ALREADY_EXISTS;
      }
    desktops.push_back(dt);
    return 0;
}


DESKTOP_DATA &DesktopManager::get_desktop_by_name(std::string name)
{
  for(int i=0;i<NumberOfDesktops();i++)
    {
        if(desktops[i].name==name)
            return desktops[i];
    }
throw(ERR_NOT_EXISTS);
}

DESKTOP_DATA &DesktopManager::get_desktop_by_number(int i)
{
    if(i>NumberOfDesktops())
        throw(ERR_NOT_EXISTS);

    return desktops[i];
}






// ------------------
void LastError()
{
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPSTR) &lpMsgBuf,
        0, NULL );


    cerr <<"GetLastError returned "<<dw<<": "<<(const char*)lpMsgBuf<<endl;


    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// main.cpp
#include <iostream>
#include <windows.h>

#include "deskman.hpp"
using namespace std;




int main()
{
DesktopManager dm;

if(dm.AddDesktop("mydesk")!=0)
  {
    cerr <<"Could not create a desktop"<<endl;
    exit(1);
  }




cout <<"Desktops found on this computer: "<<endl;
DESKTOP_DATA tmp;
for(int i=0;i<dm.NumberOfDesktops();i++)
  {
     tmp=dm.EnumDesktop(i);
     cout <<tmp.name<<endl;
  }

cout <<"--------------------------------"<<endl<<endl;


int ret=dm.SetActive("mydesk"); // This function fails

if(ret==ERR_NOT_EXISTS)
  {
      cerr <<"Desktop doesn't exist"<<endl;
      exit(2);
  }

Sleep(5000);



    return 0;
}


GetLastError from DesktopManager::SetActive always returns 5 (program outputs "GetLastError returned 5: Access is denied.")
What's wrong here?

Thanks for help.
Topic archived. No new replies allowed.