We already have some articles on using the Entitlement API from Inventor:
- Entitlement API in Inventor
- Entitlement API in Inventor 2018
There is now a change you have to be aware of.
Starting with Inventor 2020, to guarantee the Entitlement API calls work properly to get the UserName, call the Inventor API's Application.Login() function before or after webServiceMgr.Initialize() so the WebServices are fully initialized.
Otherwise, you may see a valid UserId, but an empty UserName when the user is already logged in:
Note: the Application.Login() function is hidden, so IntelliSense in Visual Studio will not list it.
And, if the user attempts to login when you have not used the Login() API call, they may get an error dialog that the service is not available:
You can use the following code to get userId and userName:
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime) { m_inventorApplication = addInSiteObject.Application; CWebServicesManager mgr = new CWebServicesManager(); bool isInitialized = mgr.Initialize(); if (isInitialized) { try { m_inventorApplication.Login(); string userId = ""; mgr.GetUserId(ref userId); string userName = ""; mgr.GetLoginUserName(ref userName); MsgBox.Show( "User ID = '" + userId + "\n" + "User Name = '" + userName + "'"); } catch (Exception ex) { MsgBox.Show(ex.Message); } } }
This solved the problem:
As pointed out in one of the above-referenced articles, there is a GitHub repo with a project showing the usage of the Entitlement API from an Inventor add-in. I updated it with "releases" for Inventor 2016/2017, 2018/2019 and 2020: https://github.com/ADN-DevTech/EntitlementAPI_Inventor
-Adam