CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jan 2017
    Posts
    12

    CoCreateInstanceAsAdmin how initialize com on thread?

    Hello
    I wish to use:
    https://msdn.microsoft.com/en-us/lib...or=-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/lib...or=-2147217396

    I think this is somehow how to use it:
    https://msdn.microsoft.com/en-us/lib...or=-2147217396

    So I tried the following little code:

    Code:
    #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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Looking at the referenced COM Elevation Moniker article, I think that you have to provide the CoCreateInstanceAsAdmin() function similar to the example code shown.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    OK

    This is my new code attempt:

    Code:
    #include "Tpmvscmgr.h"
    #include "stdafx.h"
    #include  "StrSafe.h"
    #pragma comment (lib, "Vscmgr.lib")
    #include <Windows.h>
    
    int main()
    {
    
    	HRESULT hr = S_OK;
    	HWND hwnd; // Initialized with the parent window handle of UAC prompt.
    	ITpmVirtualSmartCardManager *pObj = NULL;
    	CoInitialize(NULL);
    	hr = CoCreateInstanceAsAdmin(
    		hwnd,
    		CLSID_TpmVirtualSmartCardManager,
    		IID_ITpmVirtualSmartCardManager,
    		(void**)&pObj);
    
    
    	ITpmVirtualSmartCardManager *pObj = NULL;
    	LPWSTR friendly = L"Friendly";
    	BYTE admin = 1;
    	DWORD adminsize = 1;
    	
    	HRESULT hr = pObj->CreateVirtualSmartCard(friendly, admin, &admin, adminsize, &admin, adminsize, &admin, adminsize, &admin, adminsize, adminsize, NULL, &friendly, false);
    
        return 0;
    }
    
    HRESULT CoCreateInstanceAsAdmin(HWND hwnd, REFCLSID rclsid, REFIID riid, __out void ** ppv)
    {
    	BIND_OPTS3 bo;
    	WCHAR  wszCLSID[50];
    	WCHAR  wszMonikerName[300];
    
    	StringFromGUID2(rclsid, wszCLSID, sizeof(wszCLSID) / sizeof(wszCLSID[0]));
    	HRESULT hr = StringCchPrintf(wszMonikerName, sizeof(wszMonikerName) / sizeof(wszMonikerName[0]), L"Elevation:Administrator!new:%s", wszCLSID);
    	if (FAILED(hr))
    		return hr;
    	memset(&bo, 0, sizeof(bo));
    	bo.cbStruct = sizeof(bo);
    	bo.hwnd = hwnd;
    	bo.dwClassContext = CLSCTX_LOCAL_SERVER;
    	return CoGetObject(wszMonikerName, &bo, riid, ppv);
    }
    It looks fine in visual studio but fails fast on trying to build.

    1>c:\users\...\consoleapplication4.cpp(14): error C2065: 'ITpmVirtualSmartCardManager': undeclared identifier
    1>c:\users\...\consoleapplication4.cpp(14): error C2065: 'pObj': undeclared identifier
    1>c:\users\...\consoleapplication4.cpp(18): error C2065: 'CLSID_TpmVirtualSmartCardManager': undeclared identifier
    1>c:\users\...\consoleapplication4.cpp(19): error C2065: 'IID_ITpmVirtualSmartCardManager': undeclared identifier

    DO I need to do something to get some import working maybe?
    Last edited by 2kaud; January 8th, 2017 at 01:33 PM. Reason: Added code tags

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    I've made a couple of minor changes and the code below compiles and links OK for me with VS2015.

    Code:
    #include "Tpmvscmgr.h"
    //#include "stdafx.h"
    #include  "StrSafe.h"
    #pragma comment (lib, "Vscmgr.lib")
    #include <Windows.h>
    
    HRESULT CoCreateInstanceAsAdmin(HWND hwnd, REFCLSID rclsid, REFIID riid, __out void ** ppv)
    {
    	BIND_OPTS3 bo;
    	WCHAR  wszCLSID[50];
    	WCHAR  wszMonikerName[300];
    
    	StringFromGUID2(rclsid, wszCLSID, sizeof(wszCLSID) / sizeof(wszCLSID[0]));
    	HRESULT hr = StringCchPrintfW(wszMonikerName, sizeof(wszMonikerName) / sizeof(wszMonikerName[0]), L"Elevation:Administrator!new:%s", wszCLSID);
    	if (FAILED(hr))
    		return hr;
    	memset(&bo, 0, sizeof(bo));
    	bo.cbStruct = sizeof(bo);
    	bo.hwnd = hwnd;
    	bo.dwClassContext = CLSCTX_LOCAL_SERVER;
    	return CoGetObject(wszMonikerName, &bo, riid, ppv);
    }
    
    int main()
    {
    	HRESULT hr = S_OK;
    	HWND hwnd; // Initialized with the parent window handle of UAC prompt.
    	ITpmVirtualSmartCardManager *pObj = NULL;
    	CoInitialize(NULL);
    	hr = CoCreateInstanceAsAdmin(
    		hwnd,
    		CLSID_TpmVirtualSmartCardManager,
    		IID_ITpmVirtualSmartCardManager,
    		(void**)&pObj);
    
    
    	//ITpmVirtualSmartCardManager *pObj = NULL;
    	LPWSTR friendly = L"Friendly";
    	BYTE admin = 1;
    	DWORD adminsize = 1;
    
    	hr = pObj->CreateVirtualSmartCard(friendly, admin, &admin, adminsize, &admin, adminsize, &admin, adminsize, &admin, adminsize, adminsize, NULL, &friendly, false);
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Thank you but I now get

    c:\users\...\consoleapplication4.cpp(46): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

    If I add the import again I get all the same errors again.
    Any idea why this happens? You could build that exact code you pasted? If so there must be some difference in the project somehow? I created my project in as a win32 ConsoleApplication and that seems to add a stdafx.h as default, but I tried a couple different types of projects and added your code but always get either:

    c:\users\...\consoleapplication4.cpp(46): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

    Or the whole list of errors.
    Last edited by Johannes H; January 8th, 2017 at 03:44 PM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    I don't use pre-compiled headers. I have Project/Properties/c++/Precompiled Headers/Precompiled Header set to Not Using Precompiled Headers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Ok thank you.

    I now get:

    c:\...\consoleapplication4.cpp(30): error C4700: uninitialized local variable 'hwnd' used on that code.

    Did you do anything in your project to make any imports or references or anything work?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    You are treating warnings as errors. The compiler is correct in that hwnd hasn't been initialised - and needs to be before the code stands any chance of executing properly. As it stands, the code won't run but if you disable treat warnings as errors, it should at least compile/link.

    Project/Properties/c++/General/Treat Warnings As Errors set to No.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Thank you very much for your help!
    I did have that setting set to no actually so must be something else. But I guess that isn't my big problem anyway.

    If I simply set HWND hwnd= NULL; then it does build.
    Do you think that will ruin the attempt to create the smart card or is the hwnd just for printing output or something you think?

    If I am lucky and the hwnd can be null then the question is how I actually initialize the input for the CreateVirtualSmartCard method.
    There is so many input parameters and I am not sure which are necessary and what they need to be. I think pin is needed at least so I tried this:

    Code:
    LPWSTR friendly = L"Friendly";
    LPWSTR out = L"";
    BYTE admin = 1;
    DWORD adminsize = 1;
    
    BYTE pbpin = 123456789;
    DWORD cbPin = 9;
    
    hr = pObj->CreateVirtualSmartCard(friendly, admin, &admin, adminsize, &admin, adminsize, &admin, adminsize, &pbpin, cbPin, false, NULL, &out, false);
    It does not (of course) create a smartcard. If I have BYTE pbpin = 123456789; do you think it is right that DWORD cbPin = 9; or is size something else? Is there a way to see what the inputs need to be somehow?
    Last edited by 2kaud; January 9th, 2017 at 05:23 AM. Reason: Code tags added

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Details of the requirements for the parameters of CreateVirtualSmartCard() are here https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx - especially what can be specified for the second parameter.

    pbpin is a pointer to type BYTE. So pbpin could be specified as
    Code:
    BYTE pbpin[] = "123456789";
    DWORd cbPin = sizeof(pbpin);
    and then just use pbpin in the function call (rather than &pbpin).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    OK thank you!
    Yes I did look at that page, but have a hard time figuering out what the parameters must be.
    bAdminAlgId for example, I have no clue what to send into that one

    I now have this attempt:

    Code:
    LPWSTR friendly = L"Friendly";
    	LPWSTR out = L"";
    	BYTE admin = 1;
    	
    
    	BYTE pin[] = "123456789";
    	DWORD size = sizeof(pin);
    	
    	hr = pObj->CreateVirtualSmartCard(friendly, admin, pin, size, NULL, size, pin, size, pin, size, false, NULL, &out, false);
    It gives me
    A null reference pointer was passed to the stub.

    Any idea what that might be?

    Another thing, how do I add the code-tag in this forum? cant find it in the editor..
    Last edited by 2kaud; January 9th, 2017 at 06:56 AM. Reason: Code tags added

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    I haven't used CreateVirtualSmartCard(), so I'm going just on the info provided in the referenced article. Consider
    Code:
    	LPWSTR friendly = L"Friendly";
    	LPWSTR out = L"";
    	BYTE pin[] = "123456789";
    	DWORD size = sizeof(pin);
    	BOOL boot;
    
    	hr = pObj->CreateVirtualSmartCard(friendly, TPMVSC_DEFAULT_ADMIN_ALGORITHM_ID, pin, size, NULL, 0, pin, size, pin, size, false, NULL, &out, &boot);
    I'm not sure that the same pin should be used for different purposes?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    I am not sure either but hopefully it could work. Just want anything that creates a smart card to begin with.

    Thank you very much for your help!
    The code runs now but hr is " hr E_INVALIDARG One or more arguments are invalid. HRESULT"

    Is there any way to see which arguemnt it is unhappy about?

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Not that I'm aware of (but I haven't used this before). The documentation says that it either returns S_OK for success or a WIN32 error code - and the WIN32 error code doesn't provide details re the invalid argument, only that there is one.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Jan 2017
    Posts
    12

    Re: CoCreateInstanceAsAdmin how initialize com on thread?

    Well you have been more than helpfull and I actually managed to create one finally.
    Changed it do this:

    hr = pObj->CreateVirtualSmartCard(friendly, TPMVSC_DEFAULT_ADMIN_ALGORITHM_ID, pin, 24, NULL, 0, pin, size, pin, size, false, NULL, &out, &boot);

    and it worked!
    Thank you very much!

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured