TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: SIDESHOWBOB on December 15, 2011, 09:07:08 AM

Title: Getting arguments to/from a custom activex function
Post by: SIDESHOWBOB on December 15, 2011, 09:07:08 AM
Hello creatures from the Swamp!  I wonder if you can help me.

I have written a COM dll that exports the function
Code - C++: [Select]
  1. hresult timestwo(double in,double* out)
  2. {
  3.    *out = in*2;
  4.    return S_OK;
  5. }

I am now trying to use it in visual lisp.

Code - Auto/Visual Lisp: [Select]
  1. (setq myinstance (vlax-create-object "myactivexlibrary.MyClass"))
  2. (vlax-dump-object myinstance 1) ; succeeds - myinstance shows up as having a method called timestwo

Presumably I can then call it with (vlax-invoke-method myinstance timestwo).  But how do I create the double and double* datatypes needed as arguments?
Title: Re: Getting arguments to/from a custom activex function
Post by: Jeff_M on December 15, 2011, 09:46:29 AM
This should work:
Code: [Select]
(setq num 12.34)
(vlax-invoke-method myinstance 'timestwo num 'newnum)
(princ (strcat "Result of 'timestwo' method = " (rtos newnum)))
Title: Re: Getting arguments to/from a custom activex function
Post by: SIDESHOWBOB on December 15, 2011, 09:53:06 AM
It fails:
Code - Auto/Visual Lisp: [Select]
  1. $ (vlax-invoke-method myinstance timestwo num 'newnum)
  2. ; error: bad argument type: (or stringp symbolp): nil
  3.  

Btw does ' mean "pointer to"?
Title: Re: Getting arguments to/from a custom activex function
Post by: JohnK on December 15, 2011, 09:59:06 AM
Nope. No pointers in Autolisp. We have SET which "can operate in the spirit" of pointers. ...kinda. ...Oh, well Im gonna go back to just saying `nope' on the pointer thing.

The single quote symbol: See the QUOTE function (click the word below; its a link).
Code - Auto/Visual Lisp: [Select]
Title: Re: Getting arguments to/from a custom activex function
Post by: Jeff_M on December 15, 2011, 10:00:41 AM
I had a few typo's in the code sample....I blame a lack of coffee....the code has been edited.

Title: Re: Getting arguments to/from a custom activex function
Post by: SIDESHOWBOB on December 15, 2011, 10:07:37 AM
Excellent!  Thank you.

While we're at it, is there a way to do that for arrays?  Do I need to go read the docs for safearray?
Title: Re: Getting arguments to/from a custom activex function
Post by: Jeff_M on December 15, 2011, 12:55:06 PM
Do the same thing for any type of return object. Then it's a matter of knowing the type once in lisp. So if you want to return an array, this would be similar to what the coordinates property of a polyline returns:

(note, vl-sel is my own helper function to select on object and return the vl-object instead of an ename)

Command: (setq plobj (vl-sel))

Select object: #<VLA-OBJECT IAcadLWPolyline2 000000002eb94a48>

Command: (setq coords (vlax-get-property plobj 'coordinates))
#<variant 8197 ...>

Command: (vlax-safearray->list (vlax-variant-value coords))
(3244.69 8968.45 3627.96 8607.93 3537.78 8247.4 3875.97 7999.53)

Yes, this is a property which returns the value, but a method which has an out parameter will work the same way. I should also note that the quoted property or method can be either a single quoted symbol name as I did, or as a string (wrapped in double quotes)...either way works fine. (vlax-invoke-method myinstance "timestwo" num 'newnum)
Title: Re: Getting arguments to/from a custom activex function
Post by: Kerry on December 15, 2011, 09:40:04 PM
Nope. No pointers in Autolisp. We have SET which "can operate in the spirit" of pointers. ...kinda. ...Oh, well Im gonna go back to just saying `nope' on the pointer thing.

The single quote symbol: See the QUOTE function (click the word below; its a link).
Code - Auto/Visual Lisp: [Select]


John, the link to the help documentation is VERY cool .. well done !!

Regards
kdub
Title: Re: Getting arguments to/from a custom activex function
Post by: JohnK on December 15, 2011, 09:46:56 PM
John, the link to the help documentation is VERY cool .. well done !!

Regards
kdub

Leave out the "the link to the help documentation" part and you got a deal my friend. *lol*
Title: Re: Getting arguments to/from a custom activex function
Post by: MP on December 15, 2011, 09:58:24 PM
Leave out the "the link to the help documentation" part and you got a deal my friend. *lol*

(http://i44.tinypic.com/25tjl8o.jpg)
Title: Re: Getting arguments to/from a custom activex function
Post by: Kerry on December 15, 2011, 10:49:02 PM

:)

Is that John's new avater ??
Title: Re: Getting arguments to/from a custom activex function
Post by: MP on December 15, 2011, 11:17:03 PM
could be:

(http://i41.tinypic.com/2dw7tzn.jpg)

 :-D
Title: Re: Getting arguments to/from a custom activex function
Post by: JohnK on December 15, 2011, 11:31:15 PM
Eeyore walked all round Tigger one way, and then turned and walked round him the other way.
"What did you say it was?" he asked.
"Tigger."
"Ah!" said Eeyore.
"He's just come," explained Piglet.
"Ah!" said Eeyore again.
He thought for a long time and then said: "When is he going?"



*smile*
Title: Re: Getting arguments to/from a custom activex function
Post by: SIDESHOWBOB on December 16, 2011, 10:27:04 AM
Thanks all :) A A Milne appreciation aside...

Command: (vlax-safearray->list (vlax-variant-value coords))

So does this mean I have to send arrays back and forth packaged inside variants, there is no way to send a double array directly?  What if the C function is e.g
Code - C++: [Select]
  1. HRESULT arraytimestwo(double* in,double** out,long length)
is there a way to call that from vl or do I have to rewrite the C to accommodate variants?

Edit: I tried this...
Code - Auto/Visual Lisp: [Select]
  1. _$ (vlax-invoke-method n 'arraytimestwo safearray 'newsafearray arraylength)
  2. ; error: lisp value has no coercion to VARIANT with this type:  #<safearray...>