Author Topic: DllImport stuff  (Read 363 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
DllImport stuff
« on: February 05, 2024, 11:49:26 PM »
Back in the beginning . .

when AutoDesk first provided access to .NET API via wrappings to ObjectARX in AutoCAD 2005 the API wasn't complete  (surprised ? )
A lot of functionality had to be accessed using the [DllImport( . . . )} attribute to define local Methods.

Something like :
Code - C#: [Select]
  1.     [System.Security.SuppressUnmanagedCodeSecurity]
  2.     [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  3.     static internal extern int acedAlert(string prompt);
  4.     public static void alert(string msg)
  5.     {
  6.       try
  7.       {
  8.         acedAlert(msg);
  9.       }
  10.       catch
  11.       {
  12.         throw;
  13.       }
  14.     }
  15.  

Then we could generate an alert box in AutoCAD,

Code - C#: [Select]
  1.  
  2.     alert("Hello World ! and we're happy.");
  3.  
  4.  

Fast forward almost 20 years, and we use :

public static void ShowAlertDialog(string message);
Declaring Type: Autodesk.AutoCAD.ApplicationServices.Core.Application
Assembly: accoremgd, Version=25.0.0.0

Code - C#: [Select]
  1. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  2.  
  3. //----
  4.  
  5.      AcadApp.ShowAlertDialog("Wheeeeeee ....");
  6.  
  7.  

To this day the DllImport attribute needs to be called on to import functionality that AutoDesk hasn't yet wrapped.


But, We're still seeing code samples that are using DllImport that doesn't need to because the fuunctionality HAS been included in the API Assemblies.

As we migrate code to .NET8.0 C#12 for ac2025 this may be a good time to refactor code that uses DllImport un-necessarily.

Perhaps we could use this thread for samples we can think of, or find, that could be refactored.

Provide a sample of the Attribute statement  and the Library Method we can use instead.


Happy coding,

Regards,
Kerry


I'd like to thank Mark Thomas who, 19 years ago,  had the forsight to provide us with a .NET sub-forum to play in, and a second home to share with people who became friends, unsighted.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8755
  • AKA Daniel
Re: DllImport stuff
« Reply #1 on: February 06, 2024, 12:50:47 AM »
It was 20 years ago today
Sergeant pepper taught the band to play
They've been going in and out of style
But they're guaranteed to raise a smile

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8755
  • AKA Daniel
Re: DllImport stuff
« Reply #2 on: February 06, 2024, 12:52:16 AM »
Entget, entmod are must haves