TheSwamp

Code Red => .NET => Topic started by: Bobby C. Jones on August 29, 2006, 04:11:55 PM

Title: P/Invoke acedSetVar
Post by: Bobby C. Jones 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());
    }
Title: Re: P/Invoke acedSetVar
Post by: MickD 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??
Title: Re: P/Invoke acedSetVar
Post by: Glenn R 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.
Title: Re: P/Invoke acedSetVar
Post by: Kerry on August 29, 2006, 07:08:33 PM
<deleted>
Glenn beat me to it .. :lol:
Title: Re: P/Invoke acedSetVar
Post by: Draftek 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.
Title: Re: P/Invoke acedSetVar
Post by: Bobby C. Jones 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.
Title: Re: P/Invoke acedSetVar
Post by: Alexander Rivilis 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... :-)
Title: Re: P/Invoke acedSetVar
Post by: Bobby C. Jones 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.
Title: Re: P/Invoke acedSetVar
Post by: Alexander Rivilis on August 30, 2006, 01:36:38 PM
 :-)