Author Topic: (GetEnv equivalent in C#  (Read 5272 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10709
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #30 on: October 17, 2009, 02:43:21 am »
I've coded this as an exercise in answer to a question elsewhere.

The p/Envoke for GetEnv and SetEnv seemed the easiest way to handle the Variables stored in the  Fixed Profile ->General section of the registry.
.. Other Variables are stored in Profiles->  <<UserProfile>>  -> General

any tips for improvement appreciated.


Code: [Select]
// (C) CodeHimBelonga kdub @TheSwamp 20091017
//
using System;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(KDUB_testing.MyTestCommands))]

namespace KDUB_testing
{   
    public class MyTestCommands
    {
        // p/Envoke from Acad.exe
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe",
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.Cdecl)]
        private static extern int acedGetEnv(string envName, StringBuilder ReturnValue);

        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe",
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.Cdecl)]
        private static extern int acedSetEnv(string envName, StringBuilder NewValue);

        //--------------

        [CommandMethod("DoIt", CommandFlags.Modal)]
        public void MyGetEnv()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            string mainCuiFile = AcadApp.GetSystemVariable("MENUNAME") as string;
            ed.WriteMessage("\nMenuName : {0}", mainCuiFile);

            const int maxResultLength = 1024;
            StringBuilder sbRes = new StringBuilder(maxResultLength);

            acedGetEnv("ACAD", sbRes);

            ed.WriteMessage("\nValue of ACAD environment variable: {0}", sbRes.ToString());

            acedGetEnv("AcetMoveBak", sbRes);
            ed.WriteMessage("\n\nMoveBak Variable: {0}", sbRes.ToString());

            //--------------
            // assume the folder exists for this exercise

            StringBuilder sbResNew = new StringBuilder("c:\\DWG-BAK\\Auto");
            acedSetEnv("AcetMoveBak", sbResNew);

            acedGetEnv("AcetMoveBak", sbRes);
            ed.WriteMessage("\n\nMoveBak Variable is Now : {0}", sbRes.ToString());
        }
    }
}
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

nullptr

  • Bricscad
  • Needs a day job
  • Posts: 6584
  • AKA Daniel
Re: (GetEnv equivalent in C#
« Reply #31 on: October 17, 2009, 11:17:56 am »
perfect!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10709
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #32 on: October 18, 2009, 10:01:01 pm »

except as Tony T pointed out ... we don't need a StringBuilder for the acedSetEnv ... a String is sufficient.

[ long distance critique appreciated :)

Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

nullptr

  • Bricscad
  • Needs a day job
  • Posts: 6584
  • AKA Daniel
Re: (GetEnv equivalent in C#
« Reply #33 on: October 19, 2009, 01:30:23 am »
True, I thought of that too , but then again depends on the usage, example;
if I wanted to append search paths to the environment "ACAD" then Stringbuilder might be more efficient.

nullptr

  • Bricscad
  • Needs a day job
  • Posts: 6584
  • AKA Daniel
Re: (GetEnv equivalent in C#
« Reply #34 on: October 19, 2009, 01:43:21 am »
you could always make it overloaded  :-)

Code: [Select]
namespace ExecMethod
{
 public static class Commands
 {
  [System.Security.SuppressUnmanagedCodeSecurity]
  [DllImport("acad.exe", CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl)]
  internal static extern int acedGetEnv(string envName, StringBuilder ReturnValue);

  [System.Security.SuppressUnmanagedCodeSecurity]
  [DllImport("acad.exe", CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl)]
  internal static extern int acedGetEnv(string envName, string ReturnValue);

  [System.Security.SuppressUnmanagedCodeSecurity]
  [DllImport("acad.exe",CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl)]
  internal static extern int acedSetEnv(string envName, StringBuilder NewValue);

  [System.Security.SuppressUnmanagedCodeSecurity]
  [DllImport("acad.exe",CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl)]
  internal static extern int acedSetEnv(string envName, string NewValue);


  [CommandMethod("doit")]
  static public void doit()
  {
   try
   {
    AcEd.Editor editor =
     AcAp.Application.DocumentManager.MdiActiveDocument.Editor;

    string env = new string(char.MinValue, 1024);
    acedGetEnv("ACAD", env);
    acedSetEnv("ACAD", env);

    editor.WriteMessage("{0}", env);
   }
   catch (System.Exception ex)
   {
    AcAp.Application.DocumentManager.
      MdiActiveDocument.Editor.WriteMessage
       ("\n{0}\n{1}", ex.Message, ex.StackTrace);
   }
  }
 }
}
« Last Edit: October 19, 2009, 02:44:45 am by Daniel »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10709
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #35 on: October 19, 2009, 04:55:21 am »

You're a sick puppy Fronkensteen  :wink:
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--