Author Topic: DllImport acedGetVar  (Read 4559 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
DllImport acedGetVar
« on: October 16, 2006, 10:17:44 PM »
What am I doing wrong here? the objectARX docs say that acedGetVar returns a resultbuffer.
Anyway here is my code,  Thanks a ton

edit This is for AutoCAD 2005 & VS2005 /edit

Dan

Code: [Select]
using System;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
//
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;

namespace Dans.Utilities
{
  public class Utilities
  {
    const short RTNORM = 5100;
    [CommandMethod("test")]
    public void tmp()
    {
      acedAlert(GetVar("OFFSETDIST").ToString());
    }
    //
    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    internal static extern int acedAlert(string prompt);
    //-===-==-==-==-=-=-==-=-==-=-==-=-=-==-=-=-=-=-==-=-=-=-=-==-=-==-
    //VVVV This Does not work VVVV
    //-===-==-==-==-=-=-==-=-==-=-==-=-=-==-=-=-=-=-==-=-=-=-=-==-=-==-
    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    static internal extern int acedGetVar(string prompt, out IntPtr ptr);
    //
    public static int GetVar(string title)
    {
      IntPtr ptr = IntPtr.Zero;
      ResultBuffer rb = new ResultBuffer();
      if (acedGetVar(title, out ptr) == RTNORM)
      {
        try
        {
          rb = (ResultBuffer)DisposableWrapper.Create(typeof(ResultBuffer), ptr, true);
          ArrayList arl = new ArrayList();
          foreach (TypedValue val in (IEnumerable)rb)
          {
            arl.Add((object)val.Value);
          }
          return arl.Count;
        }
        catch
        {
          // put an exception here
        }
        finally
        {
          rb.Dispose();
        }
      }
      return (int)0;
    }
  }
}
« Last Edit: October 17, 2006, 04:32:45 AM by Danielm103 »

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: DllImport acedGetVar
« Reply #1 on: October 16, 2006, 11:18:32 PM »
I'm not sure that an arraylist can take mixed types, an rb can hold various types (real, strings etc.) and as such may be causing the errors.
I have used a hashtable in the past which has worked ok.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: DllImport acedGetVar
« Reply #2 on: October 17, 2006, 12:08:28 AM »
Hi Mick,

Thanks for the reply. I think the problem is that the Resultbuffer never gets filled so the rb object is never created. It seems that using acedGetVar in this fashion doesn’t return a IntPtr.

 BTY I think  an arraylist is kind of like Object[], if I box up the value type to an object I think it should be fine, though it’s not the most efficient with all the boxing and unboxing.

 Of Course I am so new to C# and .NET that you call me the king of kludge hehe   :doa:
Dan

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: DllImport acedGetVar
« Reply #3 on: October 17, 2006, 12:41:54 AM »
Have a look at this thread, particularly this post by Glenn R -> http://www.theswamp.org/index.php?topic=10475.msg133883#msg133883

there should be no need for a dllimport, follow the sample and you should be ok.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: DllImport acedGetVar
« Reply #4 on: October 17, 2006, 01:03:44 AM »
Mick

Unfortunately, GetSystemVariable and SetSystemVariable are not in 2005, at least not that I could find, I could use COM but that’s no fun.  :|

Thanks
Dan

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: DllImport acedGetVar
« Reply #5 on: October 17, 2006, 01:50:28 AM »
In that case, have a look here -> http://www.theswamp.org/index.php?topic=12130.0
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: DllImport acedGetVar
« Reply #6 on: October 17, 2006, 12:37:06 PM »
Code: [Select]
using System ;
using System.Collections ;
using System.Runtime.InteropServices ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.ApplicationServices ;
using Autodesk.AutoCAD.DatabaseServices ;

[assembly: CommandClass(typeof(Rivilis.Utils))]

namespace Rivilis
{
  public class Utils
  {
    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
    extern static private int acedGetVar(string args, IntPtr result);
 
    static public ResultBuffer AcadGetVar(string varname)
    {
      ResultBuffer rbb = new ResultBuffer(new TypedValue());
      acedGetVar(varname, rbb.UnmanagedObject);
      return rbb;
    }

    [CommandMethod("test")]
    static public void test()
    {
      ResultBuffer rb = AcadGetVar("OFFSETDIST");
      foreach (TypedValue rb1 in (IEnumerable) rb)
      {
        CommandLinePrompts.Message("\nCode=" + rb1.TypeCode.ToString() + " Value=" + rb1.Value.ToString());
      }
    }

  }
}

Not:
Code: [Select]
static internal extern int acedGetVar(string prompt, out IntPtr ptr);
But:
Code: [Select]
static internal extern int acedGetVar(string prompt, IntPtr ptr);
Because in acedGetVar must be passed pointer to allocated resbuf (e.g. ResultBuffer)
« Last Edit: October 17, 2006, 12:41:24 PM by Alexander Rivilis »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: DllImport acedGetVar
« Reply #7 on: October 17, 2006, 09:45:31 PM »
Hi Alexander,

You’re Awesome! Thanks for setting me strait, I was pulling my hair out on that one! I guess I need to study on when to use the “out” keyword . 

Thank you also Mick, you saved me from getting stuck on my next task acedSetVar!

Thanks again
Dan