CoCreateInstanceAsAdmin how initialize com on thread?

Hello
I wish to use:
https://msdn.microsoft.com/en-us/library/windows/desktop/hh707164%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

to create a virtual smart card.
To use this class I need to use something called the The COM Elevation Moniker if I understand things correctly. But I cant figure out how.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms679687%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

I think this is somehow how to use it:
https://msdn.microsoft.com/en-us/library/windows/desktop/hh707171%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

So I tried the following little code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Tpmvscmgr.h"
#include "stdafx.h"


int main()
{

HRESULT hr = S_OK;
HWND hwnd; // Initialized with the parent window handle of UAC prompt.
ITpmVirtualSmartCardManager *pObj = NULL;
hr = CoCreateInstanceAsAdmin(
hwnd,
CLSID_TpmVirtualSmartCardManager,
IID_ITpmVirtualSmartCardManager,
(void**)&pObj);

....


It says "identifier CoCreateInstanceAsAdmin is undefined.
I have read that article on The COM Elevation Moniker several times now but I fail to understand what to do.
It says on the example "It assumes that you have already initialized COM on the current thread."
I guess I have not done this. How do I do it? I use visual studio if that matters.
Last edited on
I think you need to add Vscmgr.lib to your project.
Hello
Yes maybe you are right.
Do you know how to do this? I searched for it and found it in C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64 but not sure if this is the right lib or if it is a c# lib or something...
I did not manage to use it add it to references either.

I tried by setting project property common language runtime support clr and then browse for the lib under references -COM. But it says it is of type version the project cant use.

Not sure if it is because it is the wrong file, or if I try to add it in the wrong way.
Do you know this?
In Visual Studio you can do it like this: #pragma comment (lib, "Vscmgr.lib")
And it will know where to find it automatically or do I need something else?

Anyway this code still has the same error, I need to do something else:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Tpmvscmgr.h"
#include "stdafx.h"
#pragma comment (lib, "Vscmgr.lib")

int main()
{

	HRESULT hr = S_OK;
	HWND hwnd; // Initialized with the parent window handle of UAC prompt.
	ITpmVirtualSmartCardManager *pObj = NULL;

	hr = CoCreateInstanceAsAdmin(
		hwnd,
		CLSID_TpmVirtualSmartCardManager,
		IID_ITpmVirtualSmartCardManager,
		(void**)&pObj);
....
Last edited on
And it will know where to find it automatically or do I need something else?


It's part of the Windows SDK so just the plain name is enough.
What version of Windows do you use?
I am on windows 10.
But if you just create a little project and paste the code do you get the same errors?
Yes I get the same error. After a bit of research I found this page:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms679687(v=vs.85).aspx
CoCreateInstanceAsAdmin is actually not part of Windows, the code is on MSDN.
Hope this helps.
Well that is the same link as in my post. But I do not really understand it I think. Does this mean I cant use it? Do you think it is possible to create a smart card with the class somehow?
Honestly I have no idea. This is far above my pay grade.
Maybe you can try your luck at
http://www.dreamincode.net/forums/forum/15-c-and-c/
http://forums.codeguru.com/
OK thank you.
Are you calling CoInitializeEx()? That is how you load the COM Library. If either CoInitialize() or CoInitializeEx() isn't called, every COM related call will fail. Those calls - as all COM calls, return an HRESULT, which can be checked for success / failure.

On the current MSDN link they talk about Windows::Foundation or something to that effect. Don't know anything about it. That's above my pay grade too. Just CoInitializeEx() ought to work. Note there is a CoUninitialize() too that should be called at clean up.
Last edited on
hey guys, thanks for your help!
For any one interested I got some more help here and managed to create a card:

http://forums.codeguru.com/showthread.php?558241-CoCreateInstanceAsAdmin-how-initialize-com-on-thread

Topic archived. No new replies allowed.