Page 1 of 2

Calling Activex dll's

Posted: Tue Nov 12, 2013 10:16 am
by jsampson45
I have a commercial program with a communications interface in the form of an ActiveX DLL. It can be called from Visual Basic 6 or Visual Basic for Applications. I am looking for a modern programming language implementation that might be able to do the same. Can PureBasic call functions from such DLLs?

Re: Calling Activex dll's

Posted: Tue Nov 12, 2013 2:56 pm
by ebs
Hi jsampson45,

Yes, PureBasic can call functions in COM (ActiveX) DLLs; not directly, but with the help of srod's great COMatePlus library (http://www.purecoder.net/comate.htm).

Regards,
Eric

Re: Calling Activex dll's

Posted: Wed Nov 13, 2013 5:24 am
by Korolev Michael
Standard PureBasic "Interface" library, no?

Code: Select all

  Interface MyObject
    Move(x,y)
    MoveF(x.f,y.f)
    Destroy()
  EndInterface
  
  ; CreateObject is the function which creates the object, from the DLL,
  ; whose interface has just been defined.
  ; Create the first object...
  ;
  Object1.MyObject = MyCreateObject()
  
  ; And the second one.
  ;
  Object2.MyObject = MyCreateObject()
  
  ; Then the functions which have just been defined, may 
  ; be used, in order to act upon the desired object.
  ;
  Object1\Move(10, 20)
  Object1\Destroy()
  
  Object2\MoveF(10.5, 20.1)
  Object2\Destroy()

Re: Calling Activex dll's

Posted: Thu Nov 14, 2013 10:05 am
by jsampson45
Many thanks for this. Further searches on the forum suggest that I should also ask if this is possible with the demo version of PureBasic. I cannot find a description of the limitations of the demo version vs the full version.

Re: Calling Activex dll's

Posted: Fri Nov 15, 2013 4:32 pm
by swhite
Yes it can call COM dlls as I do it all the time with Visual FoxPro COM dlls. You have to correctly create the Interface by getting the information from the type library as shown below. In my case the VFP COM dll only has two methods Prepare110 and Prepare130. They both return a text string that is a response to a specific authorization request. I pass them the request string and the location of the file where the request is to be logged and they return a response string. The strange part is the hidden return value that must be specified in PB. I say hidden because the return value is not a parameter in the the VFP methods. Not being an expert in COM it must be something that happens behind the scenes that I am not aware of.

Code: Select all

Macro DEFINE_GUID(Name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
  Global Name.GUID
  
  Name\Data1    = l
  Name\Data2    = w1
  Name\Data3    = w2
  Name\Data4[0] = b1
  Name\Data4[1] = b2
  Name\Data4[2] = b3
  Name\Data4[3] = b4
  Name\Data4[4] = b5
  Name\Data4[5] = b6
  Name\Data4[6] = b7
  Name\Data4[7] = b8
EndMacro

DEFINE_GUID(CLSID_KardAuth, $8DC6BA5D, $6861, $4F0A, $A0, $80, $7D, $ED, $CC, $97, $C1, $0F)
DEFINE_GUID(IID_KardAuth,   $4C791FAF, $8649, $462D, $A9, $3A, $18, $5F, $95, $C2, $DC, $0E)

Interface KardAuth Extends IDispatch
  Prepare110_115(String.p-bstr, String.p-bstr, ReturnValue.i)
  Prepare130(String.p-bstr, String.p-bstr, ReturnValue.i)
EndInterface

Define *lnReturnPtr,*loVFP.KardAuth

If CoCreateInstance_(@CLSID_KardAuth, 0, #CLSCTX_INPROC_SERVER, @IID_KardAuth, @*loVFP)= #S_OK

   If *loVFP\Prepare130(lcTxt, gcStartIn+#dCLogFileB, @*lnReturnPtr) = #S_OK
      ...do something
   Endif
Endif


Re: Calling Activex dll's

Posted: Fri Nov 15, 2013 11:36 pm
by jsampson45
Thanks - that pretty well shows that I could not do it - I haven't heard of any 'type library' and I am afraid I do not understand your code. From VBA in Word I can run a test program I was provided with, which I can understand.

Re: Calling Activex dll's

Posted: Sat Nov 16, 2013 12:37 am
by Kiffi
@jsampson45:

as ebs wrote: There is an include called COMate you can use to call ActiveX - DLL pretty similar to VBS or VBA.

Code: Select all

ExcelObject = COMate_CreateObject("Excel.Application")

If ExcelObject
	If ExcelObject\SetProperty("Visible = #True") = #S_OK
		WorkBook = ExcelObject\GetObjectProperty("Workbooks\Add")
		If WorkBook
			ExcelObject\SetProperty("Cells(1,1) = 'Hello'")
			ExcelObject\SetProperty("Cells(1,2) = 'from'")
			ExcelObject\SetProperty("Cells(1,3) = 'COMate!'")
			...
Unfortunately you cannot use COMate in the PureBasic Demo Version.

Greetings ... Kiffi

Re: Calling Activex dll's

Posted: Tue Nov 19, 2013 12:17 pm
by jsampson45
COMatePlus has an empty help file. I don't know who the author is and there are no contact details on the nxSoftware website. Has anyone actually used this software? I would be grateful for any lead to instructions on calling ActiveX DLL's from Purebasic that someone unfamiliar with Windows COM can understand.

Re: Calling Activex dll's

Posted: Tue Nov 19, 2013 12:37 pm
by luis
jsampson45 wrote:COMatePlus has an empty help file.
It's not true.
I just downloaded the zip file from the location linked by ebs (I suppose you got it from there) and the help file is certainly not empty.

jsampson45 wrote: I don't know who the author is
srod, as mentioned above.
jsampson45 wrote: and there are no contact details on the nxSoftware website.
http://www.purecoder.net/contact.php

But the site looks semi-abandoned, and probably it is. So you shouldn't count on updates of the software.
Truth is if you don't have a background in COM or the time and will to wrap your head around it and srod's code (you have the source) probably it's a risky choice.
jsampson45 wrote: Has anyone actually used this software?
Yes.

http://www.purebasic.fr/english/viewtop ... 14&t=37214
http://www.purebasic.fr/english/search. ... mit=Search

Re: Calling Activex dll's

Posted: Tue Nov 19, 2013 1:44 pm
by netmaestro
But the site looks semi-abandoned, and probably it is. So you shouldn't count on updates of the software.
Sadly, I can confirm this. But nobody can kick me for hoping that will change someday. And the software he's written "pretty much just works", so if you need support on it, you can likely find it on the forums.

Re: Calling Activex dll's

Posted: Tue Nov 19, 2013 4:03 pm
by jsampson45
My apologies - I have discovered that the COMatePlus help file was blocked, not empty. On Windows 7, help files fail silently and appear to be empty. I find that one can read them by right-clicking and selecting 'unblock' in 'properties'.

Re: Calling Activex dll's

Posted: Fri Dec 20, 2013 11:49 am
by jsampson45
Coming back to this, I am reading the example code for COMatePLus to see if I can eventually translate Visual Basic code, which I have, into PureBasic code for accessing a third-party ActiveX dll. I see "Define.COMateObject" - is COMateObject a type? The PureBasic manual seems to indicate that what follows the "."" after the term "Define" is a type. What sort of type would COMateObject be?

Re: Calling Activex dll's

Posted: Fri Dec 20, 2013 12:13 pm
by luis
From what I remember looking briefly at the source, it's an interface. In PB you can use structures (for the object's data), procedures + virtual tables (for the object's methods) and interfaces to glue them together and build an object. Object as in OOP. Not really easy stuff for beginners but highly educational :wink:

To understand how this work see the docs for interfaces in the help file -> http://www.purebasic.com/documentation/ ... faces.html, and any of the tutorials available in this forum on the subject, or the one from srod's himself on his site. Since he's the author of the lib you are using probably it's the optimal one for you to read. It's very clear and easy to follow.

http://www.purecoder.net/oop.zip

Re: Calling Activex dll's

Posted: Fri Dec 20, 2013 6:07 pm
by jsampson45
My guess that "oWScript\Invoke(x)" is equivalent to Visual Basic's "Call .x" appears to be correct in that I can get one of the commands exposed by the DLL to work. But only one command - the others do not work. So there seems to be a partial connection. I expect for non-experts it is best to stick to Visual Basic.

Re: Calling Activex dll's

Posted: Sat Dec 21, 2013 4:52 pm
by swhite
You can use the PureBasic "TypeLib Explorer.exe" to create all the code necessary to access an ActiveX dll then it is just a matter of using the methods. I spent some time reading the Windows documentation about COM which helped me understand the basic ideas and then created the example I gave earlier. It all works quite well in Purebasic but it does require some research to understand what is happening under the hood. I came from a VFP background so it was all new to me when I started but I use it all the time now.

Simon