How do I call WTSOpenServer in VC++?

A

Adam

This is my first attempt at using the Terminal Services API, and I'm
stumped. I can't get WTSOpenServer to work for the life of me (this is
in VS2005, by the way). Specifically, the argument has me baffled.
It's my understanding that it wants a LPWSTR (at least on this machine,
where UNICODE is defined). My first attempt was:

HANDLE hnd = WTSOpenServer("asdf");

Which yielded:
error C2664: 'WTSOpenServerW' : cannot convert parameter 1 from 'const
char [5]' to 'LPWSTR'

Fine. So I tried:

HANDLE hnd = WTSOpenServer(A2T("asdf"));

Which pukes up:
error LNK2028: unresolved token (0A000415) "extern "C" void * __stdcall
WTSOpenServerW(wchar_t *)" (?WTSOpenServerW@@$$J14YGPAXPA_W@Z)
referenced in function "private: void __clrcall
test::Form1::Form1_Load(class System::Object ^,class System::EventArgs
^)"
(?Form1_Load@Form1@test@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
test.obj

error LNK2019: unresolved external symbol "extern "C" void * __stdcall
WTSOpenServerW(wchar_t *)" (?WTSOpenServerW@@$$J14YGPAXPA_W@Z)
referenced in function "private: void __clrcall
test::Form1::Form1_Load(class System::Object ^,class System::EventArgs
^)"
(?Form1_Load@Form1@test@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
test.obj

HANDLE hnd = WTSOpenServer(A2W("asdf")); gave the same results.

Can anyone tell me what on earth I'm doing wrong? I've been googling
for days, and I'm sure it can't be this difficult. Any help is
appreciated. And if I'm in the wrong group, please accept my
apologies.

Adam
 
W

Willy Denoyette [MVP]

Adam said:
This is my first attempt at using the Terminal Services API, and I'm
stumped. I can't get WTSOpenServer to work for the life of me (this is
in VS2005, by the way). Specifically, the argument has me baffled.
It's my understanding that it wants a LPWSTR (at least on this machine,
where UNICODE is defined). My first attempt was:

HANDLE hnd = WTSOpenServer("asdf");

Which yielded:
error C2664: 'WTSOpenServerW' : cannot convert parameter 1 from 'const
char [5]' to 'LPWSTR'

Fine. So I tried:

HANDLE hnd = WTSOpenServer(A2T("asdf"));

Which pukes up:
error LNK2028: unresolved token (0A000415) "extern "C" void * __stdcall
WTSOpenServerW(wchar_t *)" (?WTSOpenServerW@@$$J14YGPAXPA_W@Z)
referenced in function "private: void __clrcall
test::Form1::Form1_Load(class System::Object ^,class System::EventArgs
^)"
(?Form1_Load@Form1@test@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
test.obj

error LNK2019: unresolved external symbol "extern "C" void * __stdcall
WTSOpenServerW(wchar_t *)" (?WTSOpenServerW@@$$J14YGPAXPA_W@Z)
referenced in function "private: void __clrcall
test::Form1::Form1_Load(class System::Object ^,class System::EventArgs
^)"
(?Form1_Load@Form1@test@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
test.obj

HANDLE hnd = WTSOpenServer(A2W("asdf")); gave the same results.

Can anyone tell me what on earth I'm doing wrong? I've been googling
for days, and I'm sure it can't be this difficult. Any help is
appreciated. And if I'm in the wrong group, please accept my
apologies.

Adam


..... WTSOpenServer(L"asdf");

Willy.
 
B

Ben Voigt

Can anyone tell me what on earth I'm doing wrong? I've been googling
for days, and I'm sure it can't be this difficult. Any help is
appreciated. And if I'm in the wrong group, please accept my
apologies.

As Willy says, you can directly use L"string" to get a unicode string
literal, no point in converting from ANSI at runtime.

But your linker error will only be resolved when you add WtsApi32.lib to
your project properties under "Linker -> Inputs".

See the doc page:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsopenserver.asp
The Requirements section clearly shows the header file and import library
needed.
 
A

Adam

Yes, that was the problem - I had neglected to include the .lib. Thank
you. I feel like an idiot.

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top