Author Topic: P/Invoke acedSetVar  (Read 5339 times)

0 Members and 1 Guest are viewing this topic.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
P/Invoke acedSetVar
« on: August 29, 2006, 04:11:55 PM »
Acad 2005

Can anyone see what I'm doing wrong?  The call to acedSetVar is returning -5001, and the MODEMACRO value doesn't change.  Thanks.

Code: [Select]
    [DllImport("acad.exe", CallingConvention=CallingConvention.Cdecl,  CharSet=CharSet.Auto)]
    public static extern int acedSetVar(string variableName, IntPtr value);


    public static void SetModeMacro(string value)
    {
      ResultBuffer val = new ResultBuffer(new TypedValue((short)DxfCode.Text, value));

      int retval = acedSetVar("MODEMACRO", val.UnmanagedObject);

      Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message(retval.ToString());
    }
« Last Edit: August 29, 2006, 04:16:27 PM by Bobby C. Jones »
Bobby C. Jones

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: P/Invoke acedSetVar
« Reply #1 on: August 29, 2006, 06:18:10 PM »
I've noticed with some imports that they require the mangled name of the exported arx function, maybe this is the case here??
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Glenn R

  • Guest
Re: P/Invoke acedSetVar
« Reply #2 on: August 29, 2006, 07:06:27 PM »
The compiler mangled name in 2006:

?acedSetVarFromBag@@YAHPBDPBUresbuf@@@Z

Dumpbin that ships with Visual studio will do this. Fire a Visual studio command prompt and change to acad folder.
Then type in: Dumpbin /EXPORTS "YourpathToAcad.exeHere" >> "C:\Temp\AcadExports.txt". Once you have the exported exports :D
just search the file for what you need.

Hope this helps.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: P/Invoke acedSetVar
« Reply #3 on: August 29, 2006, 07:08:33 PM »
<deleted>
Glenn beat me to it .. :lol:
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.

Draftek

  • Guest
Re: P/Invoke acedSetVar
« Reply #4 on: August 30, 2006, 08:38:17 AM »
I've been playing with some late binding, Interop and Reflection.

This will work. (You don't need an Interop reference to Autocad since late binding is used:

Code: [Select]
        public static void SetModeMacro(string value)
        {
            object[] parameters;
            object acadapp = null;
            object cmd = null;
            object dwg = null;
            acadapp = (object)Marshal.GetActiveObject("AutoCAD.Application");
            dwg = acadapp.GetType().InvokeMember("ActiveDocument",
                        BindingFlags.GetProperty, null, acadapp, null);
            string sTemp = "(Command " + @"""" + "MODEMACRO" + @"""" + " " + @"""" +
                        value + @"""" + " )" + "\r";
            parameters = new object[1];
            parameters[0] = sTemp;
            cmd = dwg.GetType().InvokeMember("SendCommand",
                BindingFlags.InvokeMethod, null, dwg, parameters);
        }

I'd be interested in opinions as to which method to use.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: P/Invoke acedSetVar
« Reply #5 on: August 30, 2006, 09:55:29 AM »
I see both exports, but both give the same result :-(

Code: [Select]
    [DllImport("acad.exe", EntryPoint = "?acedSetVarFromBag@@YAHPBDPBUresbuf@@@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
    public static extern int acedSetVar(string variableName, IntPtr value);

Any other ideas?  Thanks.
Bobby C. Jones

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: P/Invoke acedSetVar
« Reply #6 on: August 30, 2006, 12:58:20 PM »
Any other ideas?  Thanks.
Code: [Select]
[CommandMethod("TEST")]
static public void test()
{
      SetModeMacro("12345");
}
// From adscodes.h :
const int RTSTR      = 5005; /* String */
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int acedSetVar(string variableName, IntPtr value);
public static void SetModeMacro(string value)
{
      ResultBuffer val = new ResultBuffer(new TypedValue(RTSTR, value));
      int retval = acedSetVar("MODEMACRO", val.UnmanagedObject);
      Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message(retval.ToString());
}
I think you can find yourself errors in yours code... :-)
« Last Edit: August 30, 2006, 12:59:33 PM by Alexander Rivilis »

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: P/Invoke acedSetVar
« Reply #7 on: August 30, 2006, 01:34:23 PM »
I think you can find yourself errors in yours code... :-)

Code: [Select]
CharSet.Ansi

const short RTSTR = 5005;
That did it.  Thank you Alexander.
Bobby C. Jones

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: P/Invoke acedSetVar
« Reply #8 on: August 30, 2006, 01:36:38 PM »
 :-)