Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Execute Xbasic code from everywhere

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Execute Xbasic code from everywhere

    Hi,

    COM
    We all know that Word and Excel and all other application that are COM able can be controlled from Alpha Five with Automation. Just create a COM object of the application in Xbasic and start coding and controlling.
    But what if you want to do the opposite? You can't create an Alpha Five object in another language and access the Alpha Five object model. It just doesn't exist. Alpha isn't COM able in the way as Word or Excel is.
    But there is a way to control Alpha Five from 'any' other application where you can program and select a COM or ActiveX device.

    MOM
    The technique I describe here looks like message-oriented middleware (MOM). Applications on different systems can exchange messages with each other. They can wait on each other for a reply (synchronous communication), or just going on while awaiting another message (asynchronous communication). Messages are queued on the recieving system (not in the example). MOM is suitable for peer-to-peer-communication, but also client/server-systems who makes use of distributed processing.

    Microsoft Word
    What if you have a Word document and in that document you have several places where you want to let the user insert some information, an address, yes/no, a selection from a list or whatever. If you have the data in Access or Excel than there is no problem, the integration between these programs is fabulous. Write your code to manage Access or Excel and voila. But what if you are an Alpha Five user and want to establish some integration between Word or Excel and Alpha Five. I mean it in this order. Word to A5, Excel to A5. Word or Excel is here the 'central' application. I haven't seen this until now.

    A5 slaved to Word or Excel
    Slaved to what? Ok, I just mean turn A5 into a server and let it listen to a tcp/ip port. The information it reads from that port tells it what it must do. If you tell A5 to delete all the records from its tables it would probably do so.

    Another possibility is that you could access an A5 server on another computer in another country just from Word and get your information you want, but for now we just refer to the localhost, your computer. It's just like your local WAS version but now with the name "Word Application Server".

    What do we need?
    First we need Alpha Five and Microsoft Word, the two application we want to communicate with each other. What do we need for connecting them? From Alpha to Microsoft Word we can use COM, but from Word to Alpha we need another technique. Alpha Five has the Xbasic language and that is a very powerful language. It is equipped with a huge command set and we can use a combination of Xbasic commands. For our purpose there is a command EVALUATE_TEMPLATE(). This command executes Xbasic statements in plain ASCII text. This means that we can use Xbasic to control Alpha Five. So how do we get the Xbasic statements executed in Alpha Five? And how do we connect these two programs? For this job I choose tcp/ip and the use of the Xbasic INET::Socket Object.

    The situation for Microsoft Word is a little more difficult. It has no tcp/ip commands standard present. In an earlier version of my example I made a Purebasic DLL with API tcp/ip commands, but that introduced a lot of overhead code. By accident I found a little toolbox that does this job just fine in Microsoft Word VBA and its free too. You can download the "Toolsack Baseline 1.1" toolbox from http://www.toolsack.com/. You need to install this toolbox because the example Word file references it.

    The Example
    1. To work with the example you need to download the MOM example. It contains the database with the server and the Word template. Unpack them and put the template in your Word template folder and the database in another folder whatever you like.
    2. Download now the Toolsack toolbox and install it.
    3. Start de Alpha Five MOM database and go to the forms tab in the control panel.
    4. Start the form with name "Server".
    5. Push the button "Start server", the button gets red when the server is active. Because the server is running in a background thread it will not block your Alpha Five session.
    6. Start MSWord and position the windows of both applications in such way that you can see them both and does not overlap each other.
    7. Create in Word a new document from the MOM template.
    8. Push on the first button in the Word document. There should be a dialog form pop up.
    What you see is that the Xdialog code from the Word document is send to the Alpha Five server and executed.

    This is just an example of what you can do with Alpha Five Xbasic and Microsoft VBA. I hope you enjoyed this example.

    What follows is the code under the Start Server button on the form.

    Code:
     
    'Date Created: 14-Mar-2007 12:04:23 AM
    'Last Updated: 27-Dec-2007 10:12:54 PM
    'Created By  : Alpha Software (source: helpfile)
    'Updated By  : Marcel Kollenaar
    option strict
    dim thread_code as C
    dim global buffer as C = ""
    thread_code := <<%code%
    'Thread error logging is done to:
    xbasic_error_log("c:\wordip_thread_error.txt")
    dim thread_log_file as C = "c:\threadlog.txt"
    dim this_thread as P
    this_thread := thread.current()
    this_thread.set_priority(-2)
    dim thread_run_flag as L = .T.
    dim serversocket as INET::socket
    dim temp as C
    dim request as C
    dim nl as C = crlf()
    dim ip_port as N
    dim acceptresult as CallResult
    'In my case I chose port 10000 for listening but it can be any not
    'predefined port. The port must be the same as the port to be send to
    'in the Word example.
    ip_port := 10000
    while thread_run_flag
     'Your computer is the localhost = 127.0.0.1
     if serversocket.Listen(ip_port,"127.0.0.1") then
      'save_to_file("Serversocket.listen(1)"+nl,thread_log_file,.t.)
      'Accept a blocking socket request.
      dim clientsocket as INET::socket
      acceptresult = serversocket.accept(clientsocket,2000)
      if acceptresult.success then
                on error resume next
       'save_to_file("ClientSocket accepted" + nl,thread_log_file,.t.)
                clientsocket.read(temp,10000)
                while len(temp) > 0             
                    request = request + temp
                    temp = ""
                    on error resume next
                    if clientsocket.IsReadable
                        clientsocket.read(temp, 10000)
                    end if
                 save_to_file("Request: " + request + nl,thread_log_file,.t.)
                end while
     
       buffer := ""
          buffer := request
       ui_beep(UI_STANDARD_BEEP)
                save_to_file("Buffer: " + buffer + nl,thread_log_file,.t.)
                request:=""
                clientsocket.Close()
      end if
      serversocket.close()
     
     end if
    yield control
    end while
    %code%
    'Pass the thread a pointer of the variable namespace
    'of the main thread with main_space.
    thread_create("wdthread", thread_code)
    The next code is from the OnTimer event of the form. The OnTimer event fires every 500 ms.

    Code:
     
    'Date Created: 17-Mar-2007 04:55:39 PM
    'Last Updated: 27-Dec-2007 11:18:28 PM
    'Created By  : Marcel Kollenaar
    'Updated By  : Marcel Kollenaar
    'Make the button red when the thread runs.
    if thread_is_running("wdthread") then
     :Server:Button1.Fill.Color = "red"
    else
     :Server:Button1.Fill.Color = "System-ButtonFace"
    end if
    'The execution interval is 500 ms. If the data recieving doesn't fit
    'within the 500 ms than the code in the recieve buffer will not be complete
    'and creates probably an error when ececuted.
    if len(buffer) > 0 then
     EVALUATE_TEMPLATE( buffer )'Execute the that is send from Word.
     'ui_msg_box("INFO",buffer)'Show the code that has been send.
     'ui_beep(UI_STANDARD_BEEP) 'for testing
     buffer := "" 'Clear the recieve buffer.
    end if
    The next code is from the Stop Server button.

    Code:
     
    'Date Created: 15-Mar-2007 07:17:26 PM
    'Last Updated: 27-Dec-2007 02:35:54 PM
    'Created By  : Marcel Kollenaar
    'Updated By  : Marcel Kollenaar
    'This statement assigns a false to the thread_run_flag.
    'The thread with name "wdthread" will stop.
    if thread_is_running("wdthread") then
     thread_variables("wdthread").thread_run_flag := .F.
     ui_msg_box("Thread Info","Thread has stopped!")
    else
     ui_msg_box("Thread Info","There is no Thread active!")
    end if
    This is just an example and is not fit for a real appliaction but it show you an "How to do" if you want execute Xbasic code from any place in Alpha Five.



    ************* Addition ******

    I forgot to tell that the thread dumps the xbasic code that is send to the server in file threadlog.txt in the root. There you can see if all things are send well.

    Here is also the VBA code that supports the send process.

    Code:
    Public Sub fromu3()
    'Uses Toolsack COM.
    Dim s As New socket
    Dim rs As Integer
    Dim sendstring As String
    On Error GoTo err_1
    s.connect "127.0.0.1", 10000
    sendstring = Replace(ActiveDocument.Bookmarks("xdialog").Range.Text, Chr(13), vbCrLf)
    s.Write sendstring
    Exit Sub
    err_1:
    MsgBox "The Alpha Five MOM server must be started first"
    End Sub
    
    Public Sub fromu4()
    'Uses Toolsack COM.
    Dim s As New socket
    Dim rs As Integer
    On Error GoTo err_1
    s.connect "127.0.0.1", 10000
    sendstring = Replace(ActiveDocument.Bookmarks("xdialogEvent").Range.Text, Chr(13), vbCrLf)
    s.Write sendstring
    Exit Sub
    err_1:
    MsgBox "The Alpha Five MOM server must be started first"
    End Sub
    Last edited by Marcel Kollenaar; 12-27-2007, 07:50 PM. Reason: Fixed typos and added VBA code
    Marcel

    I hear and I forget. I see and I remember. I do and I understand.
    ---- Confusius ----

    #2
    Re: Execute Xbasic code from everywhere

    Marcel,

    thanks for taking the time to post this. You have been a busy man. I may not have a direct use for this right now but it is great to see what is possible and how it is done.

    Cheers
    Tim Kiebert
    Eagle Creek Citrus
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

    Comment


      #3
      Re: Execute Xbasic code from everywhere

      Thanks Marcel. It is great to see these more complex integration examples. Really shows what can be done with some imagination - and hard work!

      Bill.

      Comment


        #4
        Re: Execute Xbasic code from everywhere

        Hi,

        Thanks for your comment. The use of it is indeed quite special. It came from an older post were KeithW talked about "named pipes" and Aaron Brown suggested tcp/ip. I made some pipes but the suggestion of Aaron kept me busy. Besides that there were other reasons to investigate this integration.

        Years ago I had a short email conversation with one of the members of the Alpha developers and asked if there where plans to make a stand alone product out of Xdialog. It is a very "easy and speedy" way of defining your dialogs. I could use it in VBA!!

        Besides that I thougth that it would be nice if you had a server and you could talk to it in Xbasic/Xdialog or any basic. This idea is already build by Carl Gundel of Liberty Basic in RunBasic. Just running a dedicated basic server and execute your programs. This can also be done with Xbasic, see the WAS!

        In my case it would be nice if I could get access Alpha Five from Microsoft Word. I can build the xbasic code in A5 and drop these code blocks in Word in bookmarks or document variables. The code can be made dynamic and send them to Alpha Five. In this way you can make quite "intelligent" Word documents that communicate with an Alpha Five database in another way than the standard merge.

        Have a lot of fun.
        Marcel

        I hear and I forget. I see and I remember. I do and I understand.
        ---- Confusius ----

        Comment


          #5
          Re: Execute Xbasic code from everywhere

          Somehow my question ties up with the thread, I think�.

          There have been times that I wished that I can execute my app (developed in A5) and instruct it to �do something�. Although I have not done it, I was under the impression that I could simply by sending A5 and my app some parameters.

          Was I mistaken?

          Regards
          George

          Comment


            #6
            Re: Execute Xbasic code from everywhere

            George,

            Can you tell me something more about whan you wanted to do?
            Marcel

            I hear and I forget. I see and I remember. I do and I understand.
            ---- Confusius ----

            Comment


              #7
              Re: Execute Xbasic code from everywhere

              My system developed in A5, has a function that can create/manage orders.

              Often I have wanted to communicate with my app from another system, and simply pass my system some parameters and let it create the order, as if the user had done it from within the app itself.

              Is this possable?

              George

              Comment


                #8
                Re: Execute Xbasic code from everywhere

                Originally posted by georgefurnel View Post
                My system developed in A5, has a function that can create/manage orders.

                Often I have wanted to communicate with my app from another system, and simply pass my system some parameters and let it create the order, as if the user had done it from within the app itself.

                Is this possable?

                George
                Simply put, yes. In the most simple way, you write all the fuctions you need in A5. These functions recieves the (simple) information from the server that triggers them to do what they must do. A more complicated way is to have xbasic/xdialog code ready in the "other" system (even on a Linux or Apple platform) from where you want to start a process in A5. In that code that you send to the A5 server is embedded the parameters for what you want to do in A5.

                Another way is that you write a very simple state machine or parser in A5 which becomes its information from the server and pass the actions one by one on a line separated by CrLf and if possible followed by parameters for searching/selecting data.

                The benefit of all these approaches is that you can develop this completely in A5. And when it works you create the communication between A5 and the other application. Your other system must have the possibilty to send the code by tcp/ip to A5. The difficult part is to develop a kind of protocol so that both application know in which state each other system is or at least a succeed or not succeed answer.

                However not fast, a total different approach/idea is that you send an email to A5 with the code/commands it must perform. A5 checks for email with a specific header and extracts the embedded xbasic code or commands. You supply this code to the EVALUATE_TEMPLATE() function and voila it does what you want. In that way you don't need a server that listens to a tcp/ip port.

                You can also use a SMB like technic. Send a file to a shared map such as \\MY_SYSTEM\A5_TO_PROCESS. A5 looks for changes in the map and when it sees a change it opens the file and processes the code in the file again with EVALUATE_TEMPLATE() :).
                Marcel

                I hear and I forget. I see and I remember. I do and I understand.
                ---- Confusius ----

                Comment

                Working...
                X