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

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
(GetEnv equivalent in C#
« on: November 30, 2007, 12:18:01 AM »
Question :

I'm trying to retrieve the Acad Search Path variable value
equivalent to (GetEnv "ACAD") in Lisp

As far as I can determine this can't be done with the NET API Library

Anyone have any ideas about a (GetEnv ... and (SetEnv ... equivalent in C# ?

Something similar to one of these :-
« Last Edit: October 17, 2009, 02:48:33 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: (GetEnv equivalent in C#
« Reply #1 on: November 30, 2007, 03:10:05 AM »
I don't see a way with reflector, or in the Arx docs, as the way you would do it in lisp, it doesn't look like you can with C#.  Lisp would be
Code: [Select]
(vla-get-SupportPath
  (vla-get-Files
    (vla-get-Preferences
      (vlax-get-Acad-Object)
    )
  )
)

You can get an object, from the Application object, for the Preferences, but I don't know what you can get from there.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #2 on: November 30, 2007, 03:24:29 AM »
Thanks Tim, that was my conclusion too.

there is AN AcedGetEnv(.. in arx that may perhaps could be PEnvoke'd ... but I'm lost there ..
.. or at a pinch, perhaps the ActiveX methods used in VLisp and vba could be addressed via COM.

My brains not working at the moment, so I'll come back to it.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

FengK

  • Guest
Re: (GetEnv equivalent in C#
« Reply #3 on: November 30, 2007, 04:06:12 AM »
I'm guessing here. Would info in this post be helpful?
http://www.theswamp.org/index.php?topic=10017.msg135522#msg135522

Can you access "Vl.Application.16" via C#?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #4 on: November 30, 2007, 04:47:14 AM »
This is trying with Autodesk.AutoCAD.Interop referenced

I'm out of my depth here ... [gurgle, gurgle, gurgle ]

the piccy to prove it :

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #5 on: November 30, 2007, 05:17:10 AM »
like a dog at a bone ....
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #6 on: November 30, 2007, 07:05:55 AM »
Kerry,

I was going to post up how to do the COM thing once I returned from walking the mutt, but I see you've already done it.

Here is an implementation of a wrapper for acedGetEnv.

If you look at the docs, it tells you that acedgetenv will look in a particular registry key for the value or failing that, the windows sys env table. You could use the built-in acad .net functions to find the product root key and then do a reg search thru .net, then the system environment variables if you wanted to implement your own.


Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #7 on: November 30, 2007, 08:06:16 AM »
Thanks Glenn.

That's the direction I wanted to head in next.

I noticed you called that 'setenvwrapper' ..
do you actually have a setenvwrapper using acedGetEnv acedSetEnv ?   <<<< EDIT






« Last Edit: November 30, 2007, 08:24:20 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #8 on: November 30, 2007, 08:17:56 AM »
Sorry Kerry, that was a slip of the fingers on this UK keyboard  :-) It was supposed to be getenv...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #9 on: November 30, 2007, 08:26:20 AM »
I've heard the spelling over there is weird too !

:-D


ps: I really meant using acedSetEnv


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

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #10 on: November 30, 2007, 08:32:07 AM »
Here is an updated code file with both acedGetEnv and acedSetEnv.

Is that what you're after?

Cheers,
Glenn.

Chuck Gabriel

  • Guest
Re: (GetEnv equivalent in C#
« Reply #11 on: November 30, 2007, 08:34:45 AM »
Sorry Kerry, that was a slip of the fingers on this UK keyboard  :-) It was supposed to be getenv...

Is this a permanent relocation, or are you just visiting?

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #12 on: November 30, 2007, 08:38:21 AM »
Hey Chuck,

Permanent, or at least until we decide to go someplace else  :-)
Shipped the dog and the cat as well. Max (the beagle), seems to be loving it - all the new smells and cooler weather. The cat doesn't seem to care one way or the other as long as there's food and a heater to sleep by.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #13 on: November 30, 2007, 08:49:08 AM »
Here is an updated code file with both acedGetEnv and acedSetEnv.

Is that what you're after?

Cheers,
Glenn.

perfectamundo !


I hope Jane was happy to see the animules.
 .. I bet Surrey is a little greener than the landscape you left here.

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

Bryco

  • Water Moccasin
  • Posts: 1882
Re: (GetEnv equivalent in C#
« Reply #14 on: November 30, 2007, 08:50:19 AM »
Well done Kerry and Glenn.
Autodesk.AutoCAD.Interop wasn't in my cad but I did find it in Objectarx 2008\inc

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #15 on: November 30, 2007, 08:52:45 AM »
I hope Jane was happy to see the animules.
 .. I bet Surrey is a little greener than the landscape you left here.

Yes, she was, but it didn't take her long to remember what a pain the mutt can be.
Yeah, it's green here and blasted brown if not bare earth there.

Cheers,
Glenn.

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #16 on: November 30, 2007, 08:54:42 AM »
Well done Kerry and Glenn.
Autodesk.AutoCAD.Interop wasn't in my cad but I did find it in Objectarx 2008\inc

It should be on the COM tab in the add reference dialog Bryce. Kerry should be able to post a nice piccy of the entries you need to reference seeing as he's snagit happy at the moment ;)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #17 on: November 30, 2007, 10:17:18 AM »
Well done Kerry and Glenn.
Autodesk.AutoCAD.Interop wasn't in my cad but I did find it in Objectarx 2008\inc

It should be on the COM tab in the add reference dialog Bryce. Kerry should be able to post a nice piccy of the entries you need to reference seeing as he's snagit happy at the moment ;)

OK

just Reference the  AutoCAD 2008 Type Library
and add the using statement
using Autodesk.AutoCAD.Interop;

added:
just realised, these threads must look weird if the viewer isn't logged in ( a member) ... cause the piccys really do tell a story :-)

« Last Edit: November 30, 2007, 10:26:42 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: (GetEnv equivalent in C#
« Reply #18 on: November 30, 2007, 01:29:07 PM »
Thanks mate. Picture is worth a 1000 beers.

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #19 on: November 30, 2007, 02:22:32 PM »
And Kerry is the king of piccies  :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #20 on: November 30, 2007, 07:04:09 PM »
And Kerry is the king of piccies  :-D


[you] can be as well !
http://www.theswamp.org/index.php?topic=20184.msg245455#msg245455


I'm just too lazy to write properly .. succinct is difficult.


**
[you] of course being [you the reader].
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #21 on: November 30, 2007, 07:10:09 PM »
Duly noted my friend.

Cheers,
Glenn.

Chumplybum

  • Newt
  • Posts: 97
Re: (GetEnv equivalent in C#
« Reply #22 on: December 02, 2007, 07:10:29 PM »
what about:  Autodesk.AutoCAD.ApplicationServices.UserConfigurationManager ????


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #23 on: December 02, 2007, 11:55:42 PM »

Haven't tried that Chumplybum .. have you ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: (GetEnv equivalent in C#
« Reply #24 on: December 03, 2007, 12:33:03 AM »
sorry, I was board  :laugh:

Code: [Select]
AcMgdWrprs.Utilities.GetEnv("ACAD")

for 2007



Chumplybum

  • Newt
  • Posts: 97
Re: (GetEnv equivalent in C#
« Reply #25 on: December 03, 2007, 01:11:07 AM »
this is the same as (getenv "ACAD")...


Dim UserConfigurationManager As Autodesk.AutoCAD.ApplicationServices.UserConfigurationManager = Autodesk.AutoCAD.ApplicationServices.Application.UserConfigurationManager
Dim ProfileBase As Autodesk.AutoCAD.ApplicationServices.IConfigurationSection = UserConfigurationManager.OpenCurrentProfile

Dim GeneralRegKey As Autodesk.AutoCAD.ApplicationServices.IConfigurationSection = ProfileBase.OpenSubsection("General")

System.Windows.Forms.MessageBox.Show(GeneralRegKey.ReadProperty("ACAD", "").ToString)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #26 on: December 03, 2007, 02:03:49 AM »

cool !

I'll have a play tonight ... Thanks.


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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #27 on: December 03, 2007, 02:32:32 AM »
Yes,Mark, that seems fine.
I made a method that's a little more generic
Haven't tested for invalid data in this version ...

Code: [Select]
        public static object GetProfileEntry(string sSubSection, string sProperty)
        {
            UserConfigurationManager ucManager = Application.UserConfigurationManager;
            IConfigurationSection currentProfile = ucManager.OpenCurrentProfile();
            IConfigurationSection subSection = currentProfile.OpenSubsection(sSubSection);
            return subSection.ReadProperty(sProperty, "");
        }

A piccy for Joe ..
« Last Edit: December 03, 2007, 02:41:02 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (GetEnv equivalent in C#
« Reply #28 on: December 03, 2007, 06:13:02 AM »
I s'pose there's really no need for a method when we can do it in-line without intermediate variable assignments ..

Code: [Select]
           
 string sSupportPath = AcadApp.UserConfigurationManager.OpenCurrentProfile().
                                 OpenSubsection("General").ReadProperty("ACAD", "").ToString();
« Last Edit: December 03, 2007, 07:34:35 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: (GetEnv equivalent in C#
« Reply #29 on: December 03, 2007, 06:52:51 AM »

I s'pose there's really no need for a method when we can do it in-line without intermediate variable assignments ..

Code: [Select]
            string sSupportPath = AcadApp.UserConfigurationManager.OpenCurrentProfile().
                                 OpenSubsection("General").ReadProperty("ACAD", "").ToString();code]

True, but the function looks better; is easier to read and if one property in that 'property chain' fails, it's a lot harder to debug...just some thoughts.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • 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());
        }
    }
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • 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 :)

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

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • 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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • 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: 11654
  • 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:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.