Ok here is the code I'm using to test with Dan's latest C++ DLL.
// Created By: Mick Duprez, 27th August 2007
// Used to register commands using a C wrapper for the acedRegCmds() macro
using System ;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.EditorInput;
namespace PyAcadDotNet
{
/// <summary>
/// PyAcadCmd Class:
/// Used to register commands on the AutoCAD command stack.
/// </summary>
public class PyAcadCmd
{
public PyAcadCmd()
{
}
public delegate void CmdDelegate();
/// <summary>
/// RegPyAcadCmd:
/// Registers a delegate (callback) with the AutoCAD command string
/// on the command stack.
/// </summary>
[DllImport("PyRegCmd.dll",
CallingConvention=CallingConvention.Cdecl,
EntryPoint = "?RegPyCmd@@YAXPB_W0HP6AXXZ@Z")]
public static extern void RegPyCmd(
string cmd_group,
string cmd_name,
Autodesk.AutoCAD.Runtime.CommandFlags cmd_flags,
[MarshalAs(UnmanagedType.FunctionPtr)] PyAcadCmd.CmdDelegate cmd_delegate);
//testing stuff
public static void testcommand()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\ncb1 delegate seems to work!\n");
}
[CommandMethod("regcmds")]
static public void test() // This method can have any name
{
CmdDelegate cb1 = new CmdDelegate(PyAcadCmd.testcommand);
RegPyCmd("_pycmds", "TESTER", CommandFlags.Session, cb1);
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nRegister command was called, but does it work?\n");
}
}
}
I call "regcmds" from the command line and the message is written to the command line. However my "TESTER" command is not available from the command line. I and doing this from debug mode in VC# Express. When I close AutoCAD it gives me a System.StackOverflowException.
Now if I passed bad values to the underlying C function should I expect an error? I ask because the C code askes for an int on cmd_flags and I'm passing a AutoDesk.AutoCAD.Runtime.CommandFlag, which I don't think is an int.
The C code in the dll I'm using:
#pragma unmanaged
__declspec(dllexport) void __cdecl RegPyCmd(const ACHAR * cmdGroup,
const ACHAR * cmdName,
int cmdFlags,
void(*functionptr)())
{
// Note: cast functionptr as AcRxFuctionPtr.
acedRegCmds->addCommand(cmdGroup, cmdName, cmdName, (int)cmdFlags,(AcRxFunctionPtr)functionptr);
}
Note: I'm almost certain I have the entry point correct as I ran "dumpbin /EXPORTS PyRegCmd.dll" and it gave me the one I use in the code above.