.NET DllImport a method defined in C++

By Augusto Goncalves

Suppose there is a void MyFunc() C++ function you need to call from .NET. The DllImport call will only recognize it if the function is declared with dllexport modifier.

extern "C" __declspec( dllexport ) void MyFunc()

It is also possible pass a .NET AutoCAD Entity to C++ unmanaged, just declare the function with a pointer and use UnmanagedObject property from .NET.

C++

extern "C" __declspec( dllexport ) void MyFunc(AcDbLine* line)

.NET

[DllImport("MyArxModule.arx",

  CallingConvention = CallingConvention.Cdecl,

  CharSet = CharSet.Unicode)]

private static extern void MyFunc(System.IntPtr line);

 

[CommandMethod("myCommand")]

static public void CmdCallCommand()

{

  Line l = // do something here…

  MyFunc(l.UnmanagedObject);

}


Comments

4 responses to “.NET DllImport a method defined in C++”

  1. Why do you use MyFunc instead of SomeFunc in your .net code? MyFunc hasn’t any parameters according its export signature.

  2. Andrey,
    Thanks for catching that, it’s actually a typo… I have fixed it.
    Regards,
    Augusto Goncalves

  3. Dear Augusto!
    Another typos:
    privatestaticexternvoid
    staticpublicvoid
    ;)

  4. Alexander, that’s the online editor removing spaces for some reason… :-(
    Thanks!
    Cheers,
    Augusto Goncalves

Leave a Reply to Alexander RivilisCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading