Author Topic: DDE on FreeBasic? Final message sent, see below  (Read 6162 times)

0 Members and 1 Guest are viewing this topic.

Offline stefan63

  • ZX 81
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Hi all,
Can anybody point to some article, which describes usage of DDE in FreeBasic?
I have DDE server running (Excel works with him) - how to communicate with server from FreeBasic?
regards,
Stefan
« Last Edit: December 01, 2006 by stefan63 »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: DDE on FreeBasic?
« Reply #1 on: November 25, 2006 »
If you know how to do it in other languages, I can help you with freebasic versions.
Jim
Challenge Trophies Won:

Offline stefan63

  • ZX 81
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: DDE on FreeBasic?
« Reply #2 on: November 26, 2006 »
Thank You Jim,
nice answer.
I'll study how it's done on other laguages and will ask You later - can i?
It is not very clear for me DDE prrocess at whole, so maybe i'll need more time.
regards
Stefan

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: DDE on FreeBasic?
« Reply #3 on: November 26, 2006 »
For sure.  I'll hunt down some info myself.  I've never played with DDE (Dynamic Data Exchange) - it's for making sure two apps have the same view of data, if you change a value in one program, like modifiying a cell's value in Excel, then if that spreadsheet was linked by DDE into a Word document you were working on, then the Word doc would change too.

Jim
Challenge Trophies Won:

Offline stefan63

  • ZX 81
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: DDE on FreeBasic?
« Reply #4 on: November 29, 2006 »
hello Jim,
i tryed my first DDE programm (client) with DDE server from
http://www.fatek.com/Download%20Page/English/Fatek_Server/FaSvr110-ENU.EXE

My successful attempt is PEEK (request)  - function RETRIEVEmyDDEitem ,
and one variant of POKE, second variant of POKE is unsuccessfull and
commented below - both in function CHANGEmyDDEitem.

This unsuccessfull attempt is important - i think that way will be naccassary for othet DDE functions.
I dont know how to include my code in shrinkable widow in post , so i just copied (using "CODE" icon above)

Here is my code:
Code: [Select]

#DEFINE WIN_INCLUDEALL
option explicit


#include once "windows.bi"

declare function RETRIEVEmyDDEitem(byval itemname as string) as string
declare function DdeCallBack as PFNCALLBACK
declare function CHANGEmyDDEitem(byval itemname as string,byval newmydata as integer) as integer
dim shared state1 (0 to 30 ) as integer

dim shared i as integer

dim shared idInst as long


dim shared myhsz as HSZ,hszMYSERVER as HSZ,hszMYTOPIC as HSZ
dim shared hszMYITEM as HSZ
dim shared MYDDEservername as string
dim shared MYDDEtopicname as string, psz as string,MYDDEitemname as string 'LPTSTR

dim shared MYDDEconnection as HCONV
dim shared d1tmp as integer
dim shared d2tmp as integer
MYDDEservername="FaconSvr"
MYDDEtopicname="Channel0.Station1.Group0"


DdeInitialize(@idInst, DdeCallBack,CBF_FAIL_EXECUTES or CBF_SKIP_ALLNOTIFICATIONS,0)
hszMYSERVER=DdeCreateStringHandle(idInst,MYDDEservername,CP_WINANSI)
hszMYTOPIC=DdeCreateStringHandle(idInst,MYDDEtopicname,CP_WINANSI)
MYDDEconnection=DdeConnect(idInst,hszMYSERVER,hszMYTOPIC,NULL)

'================= d1

print "Retrieve:",RETRIEVEmyDDEitem("D1")
print "Retrieve:",RETRIEVEmyDDEitem("D2")
print "Retrieve:",RETRIEVEmyDDEitem("D1")
d1tmp=valint(RETRIEVEmyDDEitem("D1"))
d2tmp=valint(RETRIEVEmyDDEitem("D2"))
print "sleep1"Â  Â  
sleep

CHANGEmyDDEitem("D1",d1tmp+1)
CHANGEmyDDEitem("D2",d2tmp+2)
print "sleep2"
sleep
print "Retrieve:",RETRIEVEmyDDEitem("D1")
print "Retrieve:",RETRIEVEmyDDEitem("D2")
print "Retrieve:",RETRIEVEmyDDEitem("D1")

DdeDisconnect(MYDDEconnection)
DdeFreeStringHandle(idInst,hszMYSERVER)
DdeFreeStringHandle(idInst,hszMYTOPIC)
DdeUninitialize(idInst)
sleep
end

function DdeCallBack as PFNCALLBACK
print "MYCALLBACK"
return  NULL
end function  Â  
'===========================
function RETRIEVEmyDDEitem(byval itemname as string) as string
dim md1 as HDDEDATA
dim mysize1 as integer
dim mystringresultptr as ubyte ptr
dim help1ptr as ubyte ptr
dim mystringresult as string
dim myres1 as long
  Â  
MYDDEitemname=itemname
print "ASK="; MYDDEitemname
hszMYITEM=DdeCreateStringHandle(idInst,MYDDEitemname,CP_WINANSI)
md1=DdeClientTransaction( NULL,0,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_REQUEST ,110,@myres1)

mysize1= DdeGetdata(md1,NULL,0,0)
print "returned data has size=",mysize1
mystringresultptr=allocate(mysize1)
help1ptr=mystringresultptr
DdeGetdata(md1,mystringresultptr,mysize1,0)

mystringresult=""
while (*mystringresultptr)<>0
mystringresult=mystringresult+chr(*mystringresultptr)
mystringresultptr+=1
wend
deallocate(help1ptr)
DdeFreeStringHandle(idInst,hszMYITEM)
DdeFreeDataHandle(md1)
return mystringresult
end function  Â  

'====================

function CHANGEmyDDEitem(byval itemname as string,byval newmydata as integer) as integer
dim md1 as HDDEDATA
dim md2 as HDDEDATA
dim md2ptr as HDDEDATA ptr
dim mysize1 as integer
dim mystringresultptr as ubyte ptr
dim help1ptr as ubyte ptr
dim mystringresult as string
dim myres1 as long
dim mystringptr as string ptr
dim mypbyte as PBYTE



MYDDEitemname=itemname
hszMYITEM=DdeCreateStringHandle(idInst,MYDDEitemname,CP_WINANSI)

mystringresult=""
mystringresult=str(newmydata)
'print mystringresult;"*"
mysize1=len(mystringresult)
mysize1+=1
mystringptr= Strptr(mystringresult)
mypbyte=cptr(PBYTE,mystringptr)

print "POKE TO="; MYDDEitemname, mystringresult

'successful POKE
md1=DdeClientTransaction(mypbyte,mysize1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)

'UNSUCCESSFUL poke
'md2=DdeCreateDataHandle(idInst,mypbyte,mysize1,0,hszMYITEM,CF_TEXT,0)
''debug backread
'mysize1= DdeGetdata(md2,NULL,0,0)
'print "md2-datasize=",mysize1Â  Â 'returns correct value
'mystringresultptr=allocate(mysize1)
'help1ptr=mystringresultptr
'DdeGetdata(md2,mystringresultptr,mysize1,0) ''
'mystringresult=""
'while (*mystringresultptr)<>0
'Â  Â ' print "~",*mystringresultptr,"~"
'mystringresult=mystringresult+chr(*mystringresultptr)
'mystringresultptr+=1
'wend
'deallocate(help1ptr)
'print "md2 string=";mystringresult;"*"  'string is  correct
''end debug backread

'different variants tryed without success
'mypbyte=cptr(PBYTE,@md2)
'd1=DdeClientTransaction(@md2,-1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)
'md1=DdeClientTransaction(mypbyte,-1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)
'md1=DdeClientTransaction(mypbyte,mysize1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)
'md1=DdeClientTransaction(md2ptr,-1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)
'md1=DdeClientTransaction(mypbyte,-1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)
'DdeFreeDataHandle(md2)


DdeFreeStringHandle(idInst,hszMYITEM)

DdeFreeDataHandle(md1)
return 0
end function  Â  


Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: DDE on FreeBasic?
« Reply #5 on: November 29, 2006 »
OK, so do you have a question?
Jim

Challenge Trophies Won:

Offline stefan63

  • ZX 81
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: DDE on FreeBasic?
« Reply #6 on: November 29, 2006 »
Question-At this time - what is wrong in CreateDataHandle
md2=DdeCreateDataHandle(idInst,mypbyte,mysize1,0,hszMYITEM,CF_TEXT,0)
and usage
d1=DdeClientTransaction(@md2,-1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)

I'm reading DDE description in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/dataexchange/dynamicdataexchangemanagementlibrary/dynamicdataexchangemanagementreference/dynamicdataexchangemanagementtransactions/xtyp_advdata.asp

regards

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: DDE on FreeBasic?
« Reply #7 on: November 29, 2006 »
I want to help, really.  You need to give example code of what's not working, ie some FB code and other bits.  It's impossible to say from the snippets you've posted what should and shouldn't work.  You really need to come up with a test case, say what it should do, and then say how it does or doesn't work in fb.

Jim
Challenge Trophies Won:

Offline stefan63

  • ZX 81
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: DDE on FreeBasic? final message
« Reply #8 on: December 01, 2006 »
I found sintax , which is good for DDE library , here is my test program (tested with Facon sever, see above), both variants of POKE are working.
Hope this will be helpfull for someone:
Code: [Select]


#DEFINE WIN_INCLUDEALL
option explicit


#include once "windows.bi"
''#inclib "libuser32.dll"

'declare function setstateDDE(i as integer) as integer
declare function RETRIEVEmyDDEitem(byval itemname as string) as string
declare function DdeCallBack as PFNCALLBACK
declare function CHANGEmyDDEitem(byval itemname as string,byval newmydata as integer) as integer
dim shared state1 (0 to 30 ) as integer

dim shared i as integer

dim shared idInst as long


dim shared myhsz as HSZ,hszMYSERVER as HSZ,hszMYTOPIC as HSZ
dim shared hszMYITEM as HSZ
dim shared MYDDEservername as string
dim shared MYDDEtopicname as string, psz as string,MYDDEitemname as string 'LPTSTR

dim shared MYDDEconnection as HCONV
dim shared d1tmp as integer
dim shared d2tmp as integer
MYDDEservername="FaconSvr"
MYDDEtopicname="Channel0.Station1.Group0"


DdeInitialize(@idInst, DdeCallBack,CBF_FAIL_EXECUTES or CBF_SKIP_ALLNOTIFICATIONS,0)
hszMYSERVER=DdeCreateStringHandle(idInst,MYDDEservername,CP_WINANSI)
hszMYTOPIC=DdeCreateStringHandle(idInst,MYDDEtopicname,CP_WINANSI)
MYDDEconnection=DdeConnect(idInst,hszMYSERVER,hszMYTOPIC,NULL)

'================= d1

print "Retrieve:",RETRIEVEmyDDEitem("D1")
print "Retrieve:",RETRIEVEmyDDEitem("D2")
print "Retrieve:",RETRIEVEmyDDEitem("D1")
d1tmp=valint(RETRIEVEmyDDEitem("D1"))
d2tmp=valint(RETRIEVEmyDDEitem("D2"))
print "sleep1"   
sleep

CHANGEmyDDEitem("D1",d1tmp+1)
CHANGEmyDDEitem("D2",d2tmp+2)
print "sleep2"
sleep
print "Retrieve:",RETRIEVEmyDDEitem("D1")
print "Retrieve:",RETRIEVEmyDDEitem("D2")
print "Retrieve:",RETRIEVEmyDDEitem("D1")

DdeDisconnect(MYDDEconnection)
DdeFreeStringHandle(idInst,hszMYSERVER)
DdeFreeStringHandle(idInst,hszMYTOPIC)
DdeUninitialize(idInst)
sleep
end

function DdeCallBack as PFNCALLBACK
print "MYCALLBACK"
return  NULL
end function   
'===========================
function RETRIEVEmyDDEitem(byval itemname as string) as string
dim md1 as HDDEDATA
dim mysize1 as integer
dim mystringresultptr as ubyte ptr
dim help1ptr as ubyte ptr
dim mystringresult as string
dim myres1 as long
   
MYDDEitemname=itemname
print "ASK="; MYDDEitemname
hszMYITEM=DdeCreateStringHandle(idInst,MYDDEitemname,CP_WINANSI)
md1=DdeClientTransaction( NULL,0,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_REQUEST ,110,@myres1)

mysize1= DdeGetdata(md1,NULL,0,0)
'print "returned data has size=",mysize1
mystringresultptr=allocate(mysize1)
help1ptr=mystringresultptr
DdeGetdata(md1,mystringresultptr,mysize1,0)

mystringresult=""
while (*mystringresultptr)<>0
mystringresult=mystringresult+chr(*mystringresultptr)
mystringresultptr+=1
wend
deallocate(help1ptr)
DdeFreeStringHandle(idInst,hszMYITEM)
DdeFreeDataHandle(md1)
return mystringresult
end function   

'====================

function oldCHANGEmyDDEitem(byval itemname as string,byval newmydata as integer) as integer
dim md1 as HDDEDATA
dim md2 as HDDEDATA
dim md2ptr as HDDEDATA ptr
dim mysize1 as integer
dim mystringresultptr as ubyte ptr
dim help1ptr as ubyte ptr
dim mystringresult as string
dim myres1 as long
dim mystringptr as string ptr
dim mypbyte as PBYTE



MYDDEitemname=itemname
hszMYITEM=DdeCreateStringHandle(idInst,MYDDEitemname,CP_WINANSI)

mystringresult=""
mystringresult=str(newmydata)
'print mystringresult;"*"
mysize1=len(mystringresult)
mysize1+=1
mystringptr= Strptr(mystringresult)
mypbyte=cptr(PBYTE,mystringptr)

print "POKE TO="; MYDDEitemname, mystringresult

'successful POKE
md1=DdeClientTransaction(mypbyte,mysize1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)
DdeFreeStringHandle(idInst,hszMYITEM)

return 0
end function   
'========================

function CHANGEmyDDEitem(byval itemname as string,byval newmydata as integer) as integer
dim md1 as HDDEDATA
dim md2 as HDDEDATA
dim md2ptr as HDDEDATA ptr
dim mysize1 as integer
dim mystringresultptr as ubyte ptr
dim help1ptr as ubyte ptr
dim mystringresult as string
dim myres1 as long
dim mystringptr as string ptr
dim mypbyte as PBYTE
MYDDEitemname=itemname
hszMYITEM=DdeCreateStringHandle(idInst,MYDDEitemname,CP_WINANSI)
mystringresult=""
mystringresult=str(newmydata)
mysize1=len(mystringresult)
mysize1+=1

mystringptr= Strptr(mystringresult)
mypbyte=cptr(PBYTE,mystringptr)

print "POKE TO="; MYDDEitemname, mystringresult
md2=DdeCreateDataHandle(idInst,mypbyte,mysize1,0,hszMYITEM,CF_TEXT,0)

'successful POKE
md1=DdeClientTransaction(md2,-1,MYDDEconnection,hszMYITEM,CF_TEXT,XTYP_POKE ,110,@myres1)

DdeFreeDataHandle(md2)
DdeFreeStringHandle(idInst,hszMYITEM)
'DdeFreeDataHandle(md1) 'not needed i think
return 0
end function   





Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: DDE on FreeBasic? Final message sent, see below
« Reply #9 on: December 01, 2006 »
OK.  Complicated.  Lots of Windows...:D  Yummy.
Now, what is this piece of code supposed to do? What is wrong with it? Or is it working now? :D

Jim
Challenge Trophies Won:

Offline stefan63

  • ZX 81
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: DDE on FreeBasic? Final message sent, see below
« Reply #10 on: December 01, 2006 »
Workplace from where i started:
 - hardware PLC( industrial controller FATEK) is connected to PC via cable. On PC is running FaconServer - tool from FATEK, which is
DDE server . MSExcel is running too. Some cell in Excel is defined as =FaconSvr|Channel0.Station1.Group0!'D1' .
When D1 changes , this change is visible in Excel. With some macros one can change back this value from Excel to PLC.

So my testcode can do  the same ;)  - function RETRIEVEmyDDEitem("D1") reads value of D1 from FaconSvr, function
CHANGEmyDDEitem("D1",120) sets D1 to be 120.

This is very little part of DDE, i suppose Excell uses more complicated DDE functions, most important - CALLBACK mechanism.
In my code CALLBACK is wrongly defined and used.I'll work on this.
regards.


regards,

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: DDE on FreeBasic? Final message sent, see below
« Reply #11 on: December 01, 2006 »
I think CALLBACK is just the same as cdecl (stack based calling) which I've used before in freebasic (if you use qsort from clib for example).

Jim
Challenge Trophies Won: