TheSwamp
Code Red => .NET => Topic started by: It's Alive! 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
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;
}
}
}
-
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.
-
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
-
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.
-
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
-
In that case, have a look here -> http://www.theswamp.org/index.php?topic=12130.0
-
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:
static internal extern int acedGetVar(string prompt, out IntPtr ptr);
But:
static internal extern int acedGetVar(string prompt, IntPtr ptr);
Because in acedGetVar must be passed pointer to allocated resbuf (e.g. ResultBuffer)
-
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