using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            StringBuilder BufferVarValue = new StringBuilder(100);

            InitializeComponent();

            // We make the following call to allow testing in an unprotected state.
            // "LoadWinlicenseDll" will set up the environment variables with your custom valued in WinlicenseSDK.ini
            // NOTE: In your release version, before protecting, you have to REMOVE this call!!!

            // #if _DEBUG_MODE_
            //     WinlicenseSDK.WLLoadWinlicenseDll();
            // #endif  

             // Check if application is Registered
        }
        
        private void btReadCustomStr_Click(object sender, EventArgs e)
        {
            StringBuilder StringName = new StringBuilder(500);

            StringName.Append("Our String");

            // Get value for "Our String"

            Kernel32.GetEnvironmentVariableW("WLTrialStringReadW", StringName, 500);

            MessageBox.Show(StringName.ToString(), "Our String:", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btWriteCustomStr_Click(object sender, EventArgs e)
        {
            StringBuilder StringNameValue = new StringBuilder(500);

            StringNameValue.Append("Our String,Test 123");

            // Write our value "Test 123" (notice the "," separator between the StringName and StringValue

            Kernel32.GetEnvironmentVariableW("WLTrialStringWriteW", StringNameValue, 500);
        }
    }
    
    /****************************************************************************** 
    /* Class: Kernel32
    /*
    /* Description: Wrapper for Kernel32 Dll
    /*
    /******************************************************************************/

    class Kernel32
    {
        [DllImport("Kernel32.dll", EntryPoint = "GetEnvironmentVariable", CallingConvention = CallingConvention.StdCall)]
        public static extern int GetEnvironmentVariable(string lpName, StringBuilder lpBuffer, int nSize);

        [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetEnvironmentVariableW", CallingConvention = CallingConvention.StdCall)]
        public static extern int GetEnvironmentVariableW(string lpName, StringBuilder lpBuffer, int nSize);
    }

    /****************************************************************************** 
    /* Class: WinlicenseSDK
    /*
    /* Description: Wrapper for the Winlicense SDK APIs
    /*
    /******************************************************************************/

    class WinlicenseSDK
    {
        [DllImport("WinlicenseSDK.dll", EntryPoint = "WLLoadWinlicenseDll", CallingConvention = CallingConvention.StdCall)]
        public static extern void WLLoadWinlicenseDll();
    }

}