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

    Listing All Virtual Smart Cards

    Hello I got some help here some time ago and now I am hoping to repeat that success with my new problem.
    I have this code for creating a virtual smart card:
    Code:
    #include "Tpmvscmgr.h"
    //#include "stdafx.h"
    #include  "StrSafe.h"
    #pragma comment (lib, "Vscmgr.lib")
    #include <Windows.h>
    #include<comdef.h> 
    #include <unknwn.h>
    #include <iostream>
    #include <sstream>
    #include <iostream>
    #include <time.h>
    #include <string.h>
    #include <atlstr.h>
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    
    
    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(int argc, char *argv[])
    {
    
    	std::string p = (argv[1]);
    	std::vector<BYTE> pin(p.size());
    	for (size_t i = 0; i < p.size(); i++)
    	{
    		pin[i] = static_cast<BYTE>(p[i]);
    	}
    
    	std::string dir = (argv[2]);
    	std::vector<BYTE> directory(dir.size());
    	for (size_t i = 0; i < dir.size(); i++)
    	{
    		directory[i] = static_cast<BYTE>(dir[i]);
    	}
    
    	std::string directoryStr(reinterpret_cast<const char *>(&directory[0]), directory.size());
    
    	HRESULT hr = S_OK;
    	HWND hwnd = NULL;
    	ITpmVirtualSmartCardManager *pObj = NULL;
    	CoInitialize(NULL);
    	hr = CoCreateInstanceAsAdmin(
    		hwnd,
    		CLSID_TpmVirtualSmartCardManager,
    		IID_ITpmVirtualSmartCardManager,
    		(void**)&pObj);
    
    
    	//ITpmVirtualSmartCardManager *pObj = NULL;
    	LPWSTR friendly = L"Friendly";
    	LPWSTR out = L"";
    	BYTE adminPin[] = "123456789";
    	DWORD size = sizeof(adminPin);
    	BOOL boot;
    	 
    	hr = pObj->CreateVirtualSmartCard(friendly, TPMVSC_DEFAULT_ADMIN_ALGORITHM_ID, adminPin, 24, NULL, 0, adminPin, size, &pin[0], pin.size(), true, NULL, &out, &boot);
    	std::ofstream myfile;
    	myfile.open(directoryStr + "CardCreated.txt");//print the card number in a file. Last char in out-return.	
    	std::wstring someParam = std::wstring(out);
    	char cardNr = someParam.back();
    	myfile << cardNr;
    	myfile.close();
    	return 0;
    }
    This works now and it returns the number of the created smarcard, but I also need to list all the existing virtual smart cards, with it's "friendly name" + id.
    It feels like there should be some way to do that but I cant figure out how.
    I know there is something called QueryInterface but not sure how to call that or if it has anything to do with what I want really...
    There are ways to do this with uwp-apps but I need to stay with desktop app. Any help would be appreciated!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Listing All Virtual Smart Cards


  3. #3
    Join Date
    Jan 2017
    Posts
    12

    Re: Listing All Virtual Smart Cards

    Hello
    No had not seen that, I went a different way and did it in c# for now, but might look into that.
    Thanks

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