Author Topic: PInvoke from C#  (Read 2202 times)

0 Members and 1 Guest are viewing this topic.

sinc

  • Guest
PInvoke from C#
« on: December 01, 2007, 11:33:05 AM »
In this thread, Tony T gives a nice workaround for the Symbol Table Indexer problem (the one where things like blocktable["BlockName"] may return an erased entity).

Essentially, the solution is to do a PInvoke of a method that is not exposed in the managed API.  It contains some rather scary-looking code:

Code: [Select]
        public static class AcDbSymbolTable
        {
            // Acad::ErrorStatus AcDbSymbolTable::getAt(wchar_t const *, class AcDbObjectId &, bool)

            [System.Security.SuppressUnmanagedCodeSecurity]
            [DllImport("acdb17.dll", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Unicode,
               EntryPoint = "?getAt@AcDbSymbolTable@@QBE?AW4ErrorStatus@Acad@@PB_WAAVAcDbObjectId@@_N@Z")]

            public static extern ErrorStatus getAt(IntPtr symbolTable, string name, out ObjectId id, bool getErased);
        }

Anyone care to dissect this, and explain exactly what is going on?  Especially that EntryPoint stuff, with that long string of info.  Where does all that stuff come from?  I found the documentation for the System.Runtime.InteropServices.DllImportAttribute class, with it's EntryPoint method.  I also found where it says to declare the method as a "static extern" in the C# prototype.  But I'm still confused about how to figure out what to use in the EntryPoint.

I saw something about using "dumpbin" or "link".  Is that what I need to do?  Do I need to download and install VS Express C++ edition for it, or do something else?

Chuck Gabriel

  • Guest
Re: PInvoke from C#
« Reply #1 on: December 01, 2007, 01:48:37 PM »
The EntryPoint you are curious about is the mangled function name for the getAt member function of the AcDbSymbolTable class.  C++ compilers "mangle" function names when compiling to avoid name collisions (at least I think that is why they do it).

You can use dumpbin.exe, included with many of Microsoft's developmengt tools, to find the mangled names of functions in dll files.