Accessing License Information from Code (C#) - Advanced Installer Community
fkus
Posts: 27
Joined: Fri Jan 04, 2019 7:38 pm

Accessing License Information from Code (C#)

Hi,

I am attempting to retrieve the license information to display to the user (e.g. show remaining duration on license, or add a custom license element to modify the bahaviour of my software).

I have followed the instructions on the Trial and Licensing Properties webpage and on the How to Interrogate License Libraries webpage with limited success.

I was able to query the license for properties such as TrialName, TrialLeft and CompId, but what I need are the License Element properties. When I try to use the same methodology to access them, I get the return code 13 (ERROR_INVALID_DATA). Is it possible to access the License Element properties from within my code, and if so how?

Thank you,
Francis
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Accessing License Information from Code (C#)

Hello Francis,

This could happen if you did not register yet a license on the related machine. When no license is registered the "License Elements Properties" are not available at all.

So, you should make sure you first register a license before trying to access the License Elements Properties.

Hope this helped.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
fkus
Posts: 27
Joined: Fri Jan 04, 2019 7:38 pm

Re: Accessing License Information from Code (C#)

Hi Daniel,

I've been registering with the following library function:

Code: Select all

[DllImport("Trial.dll", EntryPoint = "RegisterStr", CharSet = CharSet.Ansi)]
        private static extern uint RegisterLicense([MarshalAs(UnmanagedType.LPStr)] string aKeyCode, string aLicense);
I'm assuming a return value of 0 means a successful registration, but upon further inquiry it would seem the registration is unsuccessful (when I relaunch the program I'm getting the trial window again).

Is there a resource with all the Trial.dll functions and their return codes?

Thanks!
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Accessing License Information from Code (C#)

Hello,

Actually an exit code of 0 means that the license you are trying to register is valid. But if the trial is configured to save the license into a per-machine location, then you should make sure you run the registration process elevated (with Run as admin option), otherwise the registration process fails to write the license data on current machine.

Hope this helped.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
fkus
Posts: 27
Joined: Fri Jan 04, 2019 7:38 pm

Re: Accessing License Information from Code (C#)

Hi Daniel,

When using the built-in registration form from the library function below, I have no issues registering. Does this function not require administrator access, or does the library have a workaround when using this function?

Code: Select all

[DllImport("Trial.dll", EntryPoint = "DisplayRegistrationStr", CharSet = CharSet.Ansi)]

Thanks,
Francis
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Accessing License Information from Code (C#)

Hello Francis,

The "DisplayRegistrationStr" function also needs admin privileges when registering a per-machine license. So there should be no difference.

Maybe you can try to make a diff between what it is written in the registry (when saving the license) when using the "RegisterStr" vs "DisplayRegistrationStr". Just check the license data values written under the registry location you configured into your setup project -> "Licensing" page -> Trial -> "Registration" tab -> "Runtime Key Location" section.

Also, if you could replicate the behavion in a small buildable sample and send it by email to support at advancedinstaller dot com this will be great.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
fkus
Posts: 27
Joined: Fri Jan 04, 2019 7:38 pm

Re: Accessing License Information from Code (C#)

Hi Daniel,

I have send a sample application to the email you provided, with subject Attn Daniel Radu. It includes the Visual Studio C# solution as well as my Advanced Installer project.

Please let me know if you have any difficulty accessing it.

Thanks for the assistance,
Francis
fkus
Posts: 27
Joined: Fri Jan 04, 2019 7:38 pm

Re: Accessing License Information from Code (C#)

As a follow up: I checked the registry when using "RegisterStr" - it does not make any changes to it, whereas using "DisplayRegistrationStr" does make the appropriate changes (Registration Key), all else being equal. Neither one is being run as administrator.

I have also tried running the application as administrator and using "RegisterStr" to register the key, but I'm getting the same problem.

Thanks,
Francis
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Accessing License Information from Code (C#)

Hello Francis,

Thank you for your sent sample.

After investigating this it seems there is a small error caused just by the char set (Ansi instead of Unicode) you use when importing the "RegisterStr" function. Although you did post previously the code you used to import this function I really didn't notice (my fault and please accept my apologies for this) that you use the Ansi char set

Code: Select all

[DllImport("Trial.dll", EntryPoint = "RegisterStr", CharSet = CharSet.Ansi)]
        private static extern uint RegisterLicense([MarshalAs(UnmanagedType.LPStr)] string aKeyCode, string aLicense);
Please note that our "RegisterStr" function uses the Unicode char set. So, could you please use the following code when importing our "RegisterStr" function:

Code: Select all

[DllImport("Trial.dll", EntryPoint = "RegisterStr", CharSet = CharSet.Unicode)]
        private static extern uint RegisterLicense([MarshalAs(UnmanagedType.LPStr)] string aKeyCode, string aLicense);
and see how it goes? I have just tested this and works correctly on my test machine.:)

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
fkus
Posts: 27
Joined: Fri Jan 04, 2019 7:38 pm

Re: Accessing License Information from Code (C#)

Hi Daniel,

I fixed the CharSet used and its working fine now, thanks for the assistance!

Francis
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Accessing License Information from Code (C#)

You are always welcome Francis.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”