Here's an example code i wrote where a user can query if an AutoLISP symbol is valid.
I've tested the code in 64 bit versions of AutoCAD 2013 through 2015, and it works fine on those systems.
I've also tested the code on 32 bit versions of AutoCAD 2013 through 2015. It only fails to run on 32 bit 2015.
AGS_Class.vb
'import the AutoCAD/ObjectDBX type libeary
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports System.Text ' for StringBuilder
Imports Autodesk.AutoCAD.DatabaseServices ' For ResultsBuffer
Imports System.Runtime.InteropServices 'for DllImport()
'imports the AutoCAD Type library
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.ApplicationServices
Imports System.Windows.Forms
Public Class AGS_Class
'Define ThisDrawing. The production code i am working on was converted from VBA to VB.NET, hence this has to be defined.
Public ReadOnly Property ThisDrawing() As AcadDocument
Get
Return DocumentExtension.GetAcadDocument(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument)
End Get
End Property
'Define the entry point/command.
<Autodesk.AutoCAD.Runtime.CommandMethod("AIY_AcedGetSym_Test")> _
Public Sub EntryPoint()
Dim My_Form1 As New Form1
My_Form1.ShowDialog()
End Sub
'Use P/Invoke for AcedGetSym
<DllImport("acCore.dll", CharSet:=CharSet.Unicode, _
CallingConvention:=CallingConvention.Cdecl, EntryPoint:="acedGetSym")> _
Shared Function acedGetSym(ByVal args As String, <Out()> ByRef result As IntPtr) As Integer
End Function
Public Shared Function AcadGetSym(ByVal varname As String) As ResultBuffer
' IntPtr is an integer (used as a pointer for .NET) initialize to zero
Dim rb As IntPtr = IntPtr.Zero
' use the function that was p/Invoked, pass in the name that was
' provided by the user and the IntPtr
Dim stat As Integer = 0
stat = acedGetSym(varname, rb)
' If stat is ok and the IntPtr is not zero create a ResultBuffer and Return it
If stat = CType(PromptStatus.OK, Integer) AndAlso Not (rb = IntPtr.Zero) Then
Return CType(DisposableWrapper.Create(GetType(ResultBuffer), rb, True), ResultBuffer)
End If
' Getting the symbol failed if we reach this return statement
Return Nothing
End Function
Public Shared Function GetLispStrSym(ByVal sym As String) As String
' Get the string value of a Lisp symbol
' The name (or expression) of the lisp symbol is passed in sym.
' Adapted from CP 214-4 AutoCADŽ .NET: Using .NET With Your LISP Applications example
' Output values are returned in an AutoCAD specific ResultBuffer.
' ResultBuffer can contain int, float, string, etc.
' This routine only accomodates a string!
' Populate a ResultBuffer with the values of the symbol by calling
' local function AcadGetSym which uses acedGetSym that was called through
' P/Invoke
Dim ResBuf As ResultBuffer = AcadGetSym(sym)
' Ensure that ResultBuffer is not nothing
If Not (ResBuf Is Nothing) Then
Dim s As StringBuilder = New StringBuilder
' Append each type and value in the symbol
For Each val As TypedValue In CType(ResBuf, IEnumerable)
If val.TypeCode = 5005 Then 'is it a string?
s.AppendFormat("{0}", val.Value.ToString)
Else
GetLispStrSym = "Valid LISP symbol, but it's not a string!"
Exit Function
End If
Next
GetLispStrSym = s.ToString()
Else
GetLispStrSym = "*That's not a Lisp Symbol*"
End If
End Function
End Class
and the simple form code. Form1.vb
Public Class Form1
'setup an example LISP symbol input.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
EnterText.Text = "pi"
End Sub
'On cliking okay, we pass the entered entered text into GetLispStrSym
Private Sub Okay_Click(sender As Object, e As EventArgs) Handles Okay.Click
ReturnTxt.Text = AGS_Class.GetLispStrSym(EnterText.Text).ToString
End Sub
'Exit if canceled.
Private Sub CanIt_Click(sender As Object, e As EventArgs) Handles CanIt.Click
Me.Close()
End Sub
End Class