DDE for PureBasic

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Someone in the Forum had asked for, and i have done some stuff here.For More infos about DDE search Microsofts sites plz.

Code: Select all

;DDE Server and Client in PureBasic
;(c)2002 by S.Rings (-CodeGuru-)
;
;Compile as exe and start twice or more  :)
; PureBasic Version 3.1 or better Needed !

Global WhoIAM.s
Global DDEServerName.s  
DDEServerName = "TestServer"
Global DDETopicName.s
DDETopicName = "PUREBASIC_DDE_TOPIC"
Global idInst.l
Global isRun.l

#XCLASS_BOOL = $1000
#XCLASS_NOTIFICATION = $8000
#XTYPF_NOBLOCK = $2 
#XTYP_CONNECT = $60 | #XCLASS_BOOL | #XTYPF_NOBLOCK
#XTYP_DISCONNECT = $C0 | #XCLASS_NOTIFICATION | #XTYPF_NOBLOCK

#CP_WINANSI = 1004 ;' Default codepage For windows & old DDE convs.
#SW_RESTORE = 9
#DDE_FACK = $8000
#XCLASS_FLAGS = $4000
#XTYP_EXECUTE = $50 | #XCLASS_FLAGS
#DNS_REGISTER = $1
#DNS_UNREGISTER = $2
#CF_TEXT            =  1
#XTYP_POKE = $90 | #XCLASS_FLAGS

Procedure.s MySpace(Count.l)
Dummy.s="":For I=1 To Count:  Dummy.s=Dummy.s + " ":Next I:ProcedureReturn Dummy
EndProcedure  

Procedure.l DDECallback(uType,uFmt, hConv , hszTopic, hszItem, hData,dwData1, dwData2)
Shared DDETopicName
Shared DDEServerName
Shared idInst
Shared WHOIAM
   ;StartDrawing(WindowOutput())
   ;Locate (10,90)
   ;dummy.s=Str(uType)+":"+Str(uFmt)+":"+Str(hConv)+":"
   ;DrawText (dummy)
  ; StopDrawing()
  ReturnValue=0;#DDE_FACK
  Select uType
     Case #XTYP_CONNECT

         iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String
         Buffers.s = MySpace(iCount)
         DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)
         If Buffers = DDETopicName 
             StartDrawing(WindowOutput())
             Locate(10,10)
             DrawText (Str(uFmt) +"..the client successfully connected..")
             StopDrawing()
             ReturnValue = #DDE_FACK
         EndIf
         
       Case #XTYP_DISCONNECT
              StartDrawing(WindowOutput())
              Locate(10,70)
             DrawText ("disconnected..")
             StopDrawing()
        ReturnValue = #DDE_FACK
      Case #XTYP_EXECUTE
         ReturnValue=0
       Case #XTYP_POKE
         iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String
         Buffers.s = MySpace(iCount)
         DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)
         dummy.s= "Topic:" + Buffers 
         StartDrawing(WindowOutput())
          Locate (10,30)
          DrawText (dummy)
          StopDrawing()
          
         iCount = DdeQueryString_(idInst, hszItem, 0, 0, #CP_WINANSI);First Count Length of String
         Buffers.s = MySpace(iCount)
         DdeQueryString_( idInst, hszItem, Buffers, iCount + 1, #CP_WINANSI)
         dummy="  Data::" + Buffers 

         ReturnValue = #DDE_FACK
          StartDrawing(WindowOutput())
          Locate (10,50)
          DrawText (dummy)
          StopDrawing()
                
  EndSelect
 ProcedureReturn ReturnValue
EndProcedure


;-Attention Callbacks are  only working with Procedure for >3.1 
MyCallBackAdress=@DDECallback();
If DdeInitialize_(@idInst, MyCallbackAdress, 0, 0) 
 MessageRequester("Info","Failed:"+Str(idInst),0)
EndIf
hszServer = DdeCreateStringHandle_(idInst, DDEServerName, #CP_WINANSI)
If hszServer0
 hszTopic = DdeCreateStringHandle_(idInst, DDETopicName, #CP_WINANSI)
 If hszTopic0
   ;try To find the first instance, connect
  hconvServer = DdeConnect_(idInst, hszServer, hszTopic,  0)
  If hconvServer 
    WhoIam="Client"
    MessageRequester( WHOIAM,"Server already started...",0)
  Else
    WHOIAM="Server":MessageRequester (WHOIAM,"attempt To start Server",0)
    Result=DdeNameService_( idInst, hszServer, 0,#DNS_REGISTER)
    If result=1 : isRun=1: EndIf 
   MessageRequester(WHOIAM,"Server started:"+Str(Result),0)
  EndIf
  Else
  EndIf
 Else
EndIf  

If OpenWindow(0, 200, 200, 300, 200, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, WHOIAM)
 If   WhoIam="Client"
  If CreateGadgetList(WindowID())
   ButtonGadget(0, 10, 10,  72, 20, "Send")
  EndIf
 EndIf
  Repeat
    EventID.l = WaitWindowEvent()
    If EventID = #PB_Event_CloseWindow  : Quit = 1: EndIf
    If EventID = #PB_EventGadget
     Select EventGadgetID()
       Case 0
          Message.s="-CodeGuru- was there"
          hItemMessage = DdeCreateStringHandle_(idInst, Message, #CP_WINANSI )
          If hItemMessage = 0 :  MessageRequester (WHOIAM,"Failed",0):  EndIf
          myresult=0
          Result = DdeClientTransaction_(Message, Len(Message), hconvServer, hItemMessage, #CF_TEXT, #XTYP_POKE, 3000, @myresult )
          MessageRequester(WHOIAM,"Transaction:" +Str(Result)+":"+Str(myresult),0)   
          If Result 0:  DdeFreeDataHandle_(Result ):  EndIf 
          Result=DdeFreeStringHandle_( idInst, hItemMessage)
       EndSelect  
    EndIf
  Until Quit = 1
EndIf


Result=DdeFreeStringHandle_( idInst, hszServer)
Result=DdeFreeStringHandle_( idInst, hszTopic)

;only unregister the DDE server For first instance
If isRun 
    Result=DdeNameService_(idInst, hszServer, 0, #DNS_UNREGISTER) 
    MessageRequester( WHOIAM,"in ServiceUnRegister:"+Str(result), 0)
EndIf
Result=DdeUninitialize_( idInst)
MessageRequester(WHOIAM,"Ending:"+Str(Result),0)
End 
Its a long way to the top if you wanna .....CodeGuru
TimeSurfer
User
User
Posts: 22
Joined: Wed Jan 23, 2008 6:58 am

Post by TimeSurfer »

Code: Select all

Global WhoIAM.sGlobal DDEServerName.s  DDEServerName = "TestServer"Global DDETopicName.sDDETopicName = "PUREBASIC_DDE_TOPIC"Global idInst.lGlobal isRun.l#XCLASS_BOOL = $1000#XCLASS_NOTIFICATION = $8000#XTYPF_NOBLOCK = $2 #XTYP_CONNECT = $60 | #XCLASS_BOOL | #XTYPF_NOBLOCK#XTYP_DISCONNECT = $C0 | #XCLASS_NOTIFICATION | #XTYPF_NOBLOCK#CP_WINANSI = 1004 ;' Default codepage For windows & old DDE convs.#SW_RESTORE = 9#DDE_FACK = $8000#XCLASS_FLAGS = $4000#XTYP_EXECUTE = $50 | #XCLASS_FLAGS#DNS_REGISTER = $1#DNS_UNREGISTER = $2#CF_TEXT            =  1#XTYP_POKE = $90 | #XCLASS_FLAGSProcedure.s MySpace(Count.l)Dummy.s="":For I=1 To Count:  Dummy.s=Dummy.s + " ":Next I:ProcedureReturn DummyEndProcedure  Procedure.l DDECallback(uType,uFmt, hConv , hszTopic, hszItem, hData,dwData1, dwData2)Shared DDETopicNameShared DDEServerNameShared idInstShared WHOIAM   ;StartDrawing(WindowOutput())   ;Locate (10,90)   ;dummy.s=Str(uType)+":"+Str(uFmt)+":"+Str(hConv)+":"   ;DrawText (dummy)  ; StopDrawing()  ReturnValue=0;#DDE_FACK  Select uType     Case #XTYP_CONNECT         iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String         Buffers.s = MySpace(iCount)         DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)         If Buffers = DDETopicName              StartDrawing(WindowOutput())             Locate(10,10)             DrawText (Str(uFmt) +"..the client successfully connected..")             StopDrawing()             ReturnValue = #DDE_FACK         EndIf                Case #XTYP_DISCONNECT              StartDrawing(WindowOutput())              Locate(10,70)             DrawText ("disconnected..")             StopDrawing()        ReturnValue = #DDE_FACK      Case #XTYP_EXECUTE         ReturnValue=0       Case #XTYP_POKE         iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String         Buffers.s = MySpace(iCount)         DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)         dummy.s= "Topic:" + Buffers          StartDrawing(WindowOutput())          Locate (10,30)          DrawText (dummy)          StopDrawing()                   iCount = DdeQueryString_(idInst, hszItem, 0, 0, #CP_WINANSI);First Count Length of String         Buffers.s = MySpace(iCount)         DdeQueryString_( idInst, hszItem, Buffers, iCount + 1, #CP_WINANSI)         dummy="  Data::" + Buffers          ReturnValue = #DDE_FACK          StartDrawing(WindowOutput())          Locate (10,50)          DrawText (dummy)          StopDrawing()                  EndSelect ProcedureReturn ReturnValueEndProcedure;-Attention Callbacks are  only working with Procedure for >3.1 MyCallBackAdress=@DDECallback();If DdeInitialize_(@idInst, MyCallbackAdress, 0, 0)  MessageRequester("Info","Failed:"+Str(idInst),0)EndIfhszServer = DdeCreateStringHandle_(idInst, DDEServerName, #CP_WINANSI)If hszServer<>0 hszTopic = DdeCreateStringHandle_(idInst, DDETopicName, #CP_WINANSI) If hszTopic<>0   ;try To find the first instance, connect  hconvServer = DdeConnect_(idInst, hszServer, hszTopic,  0)  If hconvServer     WhoIam="Client"    MessageRequester( WHOIAM,"Server already started...",0)  Else    WHOIAM="Server":MessageRequester (WHOIAM,"attempt To start Server",0)    Result=DdeNameService_( idInst, hszServer, 0,#DNS_REGISTER)    If result=1 : isRun=1: EndIf    MessageRequester(WHOIAM,"Server started:"+Str(Result),0)  EndIf  Else  EndIf ElseEndIf  If OpenWindow(0, 200, 200, 300, 200, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, WHOIAM) If   WhoIam="Client"  If CreateGadgetList(WindowID())   ButtonGadget(0, 10, 10,  72, 20, "Send")  EndIf EndIf  Repeat    EventID.l = WaitWindowEvent()    If EventID = #PB_Event_CloseWindow  : Quit = 1: EndIf    If EventID = #PB_EventGadget     Select EventGadgetID()       Case 0          Message.s="-CodeGuru- was there"          hItemMessage = DdeCreateStringHandle_(idInst, Message, #CP_WINANSI )          If hItemMessage = 0 :  MessageRequester (WHOIAM,"Failed",0):  EndIf          myresult=0          Result = DdeClientTransaction_(Message, Len(Message), hconvServer, hItemMessage, #CF_TEXT, #XTYP_POKE, 3000, @myresult )          MessageRequester(WHOIAM,"Transaction:" +Str(Result)+":"+Str(myresult),0)             If Result <>0:  DdeFreeDataHandle_(Result ):  EndIf           Result=DdeFreeStringHandle_( idInst, hItemMessage)       EndSelect      EndIf  Until Quit = 1EndIfResult=DdeFreeStringHandle_( idInst, hszServer)Result=DdeFreeStringHandle_( idInst, hszTopic);only unregister the DDE server For first instanceIf isRun     Result=DdeNameService_(idInst, hszServer, 0, #DNS_UNREGISTER)     MessageRequester( WHOIAM,"in ServiceUnRegister:"+Str(result), 0)EndIfResult=DdeUninitialize_( idInst)MessageRequester(WHOIAM,"Ending:"+Str(Result),0)End 
just adding code tags for formatting issues
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

You made it worse LOL ...
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Code: Select all

;DDE Server and Client in PureBasic
;(c)2002 by S.Rings (-CodeGuru-)
;
;Compile as exe and start twice or more   
; PureBasic Version 3.1 or better Needed !

Global WhoIAM.s
Global DDEServerName.s  
DDEServerName = "TestServer"
Global DDETopicName.s
DDETopicName = "PUREBASIC_DDE_TOPIC"
Global idInst.l
Global isRun.l

#XCLASS_BOOL = $1000
#XCLASS_NOTIFICATION = $8000
#XTYPF_NOBLOCK = $2 
#XTYP_CONNECT = $60 | #XCLASS_BOOL | #XTYPF_NOBLOCK
#XTYP_DISCONNECT = $C0 | #XCLASS_NOTIFICATION | #XTYPF_NOBLOCK

#CP_WINANSI = 1004 ;' Default codepage For windows & old DDE convs.
#SW_RESTORE = 9
#DDE_FACK = $8000
#XCLASS_FLAGS = $4000
#XTYP_EXECUTE = $50 | #XCLASS_FLAGS
#DNS_REGISTER = $1
#DNS_UNREGISTER = $2
#CF_TEXT            =  1
#XTYP_POKE = $90 | #XCLASS_FLAGS

Procedure.s MySpace(Count.l)
Dummy.s="":For I=1 To Count:  Dummy.s=Dummy.s + " ":Next I:ProcedureReturn Dummy
EndProcedure  

Procedure.l DDECallback(uType,uFmt, hConv , hszTopic, hszItem, hData,dwData1, dwData2)
Shared DDETopicName
Shared DDEServerName
Shared idInst
Shared WHOIAM
   ;StartDrawing(WindowOutput())
   ;Locate (10,90)
   ;dummy.s=Str(uType)+":"+Str(uFmt)+":"+Str(hConv)+":"
   ;DrawText (dummy)
  ; StopDrawing()
  ReturnValue=0;#DDE_FACK
  Select uType
     Case #XTYP_CONNECT

         iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String
         Buffers.s = MySpace(iCount)
         DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)
         If Buffers = DDETopicName 
             StartDrawing(WindowOutput())
             Locate(10,10)
             DrawText (Str(uFmt) +"..the client successfully connected..")
             StopDrawing()
             ReturnValue = #DDE_FACK
         EndIf
         
       Case #XTYP_DISCONNECT
              StartDrawing(WindowOutput())
              Locate(10,70)
             DrawText ("disconnected..")
             StopDrawing()
        ReturnValue = #DDE_FACK
      Case #XTYP_EXECUTE
         ReturnValue=0
       Case #XTYP_POKE
         iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String
         Buffers.s = MySpace(iCount)
         DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)
         dummy.s= "Topic:" + Buffers 
         StartDrawing(WindowOutput())
          Locate (10,30)
          DrawText (dummy)
          StopDrawing()
          
         iCount = DdeQueryString_(idInst, hszItem, 0, 0, #CP_WINANSI);First Count Length of String
         Buffers.s = MySpace(iCount)
         DdeQueryString_( idInst, hszItem, Buffers, iCount + 1, #CP_WINANSI)
         dummy="  Data::" + Buffers 

         ReturnValue = #DDE_FACK
          StartDrawing(WindowOutput())
          Locate (10,50)
          DrawText (dummy)
          StopDrawing()
                
  EndSelect
 ProcedureReturn ReturnValue
EndProcedure


;-Attention Callbacks are  only working with Procedure for >3.1 
MyCallBackAdress=@DDECallback();
If DdeInitialize_(@idInst, MyCallbackAdress, 0, 0) 
 MessageRequester("Info","Failed:"+Str(idInst),0)
EndIf
hszServer = DdeCreateStringHandle_(idInst, DDEServerName, #CP_WINANSI)
If hszServer<>0
 hszTopic = DdeCreateStringHandle_(idInst, DDETopicName, #CP_WINANSI)
 If hszTopic<>0
   ;try To find the first instance, connect
  hconvServer = DdeConnect_(idInst, hszServer, hszTopic,  0)
  If hconvServer 
    WhoIam="Client"
    MessageRequester( WHOIAM,"Server already started...",0)
  Else
    WHOIAM="Server":MessageRequester (WHOIAM,"attempt To start Server",0)
    Result=DdeNameService_( idInst, hszServer, 0,#DNS_REGISTER)
    If result=1 : isRun=1: EndIf 
   MessageRequester(WHOIAM,"Server started:"+Str(Result),0)
  EndIf
  Else
  EndIf
 Else
EndIf  

If OpenWindow(0, 200, 200, 300, 200, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, WHOIAM)
 If   WhoIam="Client"
  If CreateGadgetList(WindowID())
   ButtonGadget(0, 10, 10,  72, 20, "Send")
  EndIf
 EndIf
  Repeat
    EventID.l = WaitWindowEvent()
    If EventID = #PB_Event_CloseWindow  : Quit = 1: EndIf
    If EventID = #PB_EventGadget
     Select EventGadgetID()
       Case 0
          Message.s="-CodeGuru- was there"
          hItemMessage = DdeCreateStringHandle_(idInst, Message, #CP_WINANSI )
          If hItemMessage = 0 :  MessageRequester (WHOIAM,"Failed",0):  EndIf
          myresult=0
          Result = DdeClientTransaction_(Message, Len(Message), hconvServer, hItemMessage, #CF_TEXT, #XTYP_POKE, 3000, @myresult )
          MessageRequester(WHOIAM,"Transaction:" +Str(Result)+":"+Str(myresult),0)   
          If Result <>0:  DdeFreeDataHandle_(Result ):  EndIf 
          Result=DdeFreeStringHandle_( idInst, hItemMessage)
       EndSelect  
    EndIf
  Until Quit = 1
EndIf


Result=DdeFreeStringHandle_( idInst, hszServer)
Result=DdeFreeStringHandle_( idInst, hszTopic)

;only unregister the DDE server For first instance
If isRun 
    Result=DdeNameService_(idInst, hszServer, 0, #DNS_UNREGISTER) 
    MessageRequester( WHOIAM,"in ServiceUnRegister:"+Str(result), 0)
EndIf
Result=DdeUninitialize_( idInst)
MessageRequester(WHOIAM,"Ending:"+Str(Result),0)
End

Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Converted for PureBasic 4.10. Thanks for making it easy, pdwyer. :)

Code: Select all

;DDE Server and Client in PureBasic
;(c)2002 by S.Rings (-CodeGuru-)
;
;Compile as exe and start twice or more   
;PureBasic 4.10 or better needed!

Global WhoIAM.s
Global DDEServerName.s 
DDEServerName = "TestServer"
Global DDETopicName.s
DDETopicName = "PUREBASIC_DDE_TOPIC"
Global idInst.l
Global isRun.l

#XCLASS_BOOL = $1000
#XCLASS_NOTIFICATION = $8000
#XTYPF_NOBLOCK = $2
#XTYP_CONNECT = $60 | #XCLASS_BOOL | #XTYPF_NOBLOCK
#XTYP_DISCONNECT = $C0 | #XCLASS_NOTIFICATION | #XTYPF_NOBLOCK

#CP_WINANSI = 1004 ;' Default codepage For windows & old DDE convs.
#SW_RESTORE = 9
#DDE_FACK = $8000
#XCLASS_FLAGS = $4000
#XTYP_EXECUTE = $50 | #XCLASS_FLAGS
#DNS_REGISTER = $1
#DNS_UNREGISTER = $2
#CF_TEXT            =  1
#XTYP_POKE = $90 | #XCLASS_FLAGS

Procedure.s MySpace(Count.l)
Dummy.s="":For I=1 To Count:  Dummy.s=Dummy.s + " ":Next I:ProcedureReturn Dummy
EndProcedure 

Procedure.l DDECallback(uType,uFmt, hConv , hszTopic, hszItem, hData,dwData1, dwData2)
	Shared DDETopicName
	Shared DDEServerName
	Shared idInst
	Shared WHOIAM
	
	ReturnValue=0;#DDE_FACK
	Select uType
		Case #XTYP_CONNECT
			iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String
			Buffers.s = MySpace(iCount)
			DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)
			If Buffers = DDETopicName
			StartDrawing(WindowOutput(0))
			DrawText (10,10,Str(uFmt) +"..the client successfully connected..")
			StopDrawing()
			ReturnValue = #DDE_FACK
			EndIf
			
		Case #XTYP_DISCONNECT
			StartDrawing(WindowOutput(0))
			DrawText (10,70,"disconnected..")
			StopDrawing()
			ReturnValue = #DDE_FACK
		Case #XTYP_EXECUTE
			ReturnValue=0
		Case #XTYP_POKE
			iCount = DdeQueryString_(idInst, hszTopic, 0, 0, #CP_WINANSI);First Count Length of String
			Buffers.s = MySpace(iCount)
			DdeQueryString_( idInst, hszTopic, Buffers, iCount + 1, #CP_WINANSI)
			dummy.s= "Topic:" + Buffers
			StartDrawing(WindowOutput(0))
			DrawText (10,30,dummy)
			StopDrawing()
			
			iCount = DdeQueryString_(idInst, hszItem, 0, 0, #CP_WINANSI);First Count Length of String
			Buffers.s = MySpace(iCount)
			DdeQueryString_( idInst, hszItem, Buffers, iCount + 1, #CP_WINANSI)
			dummy="  Data::" + Buffers
			
			ReturnValue = #DDE_FACK
			StartDrawing(WindowOutput(0))
			DrawText (10,50,dummy)
			StopDrawing() 
	EndSelect
	ProcedureReturn ReturnValue
EndProcedure


;-Attention Callbacks are  only working with Procedure for >3.1
MyCallBackAdress=@DDECallback();
If DdeInitialize_(@idInst, MyCallbackAdress, 0, 0)
	MessageRequester("Info","Failed:"+Str(idInst),0)
EndIf

hszServer = DdeCreateStringHandle_(idInst, DDEServerName, #CP_WINANSI)
If hszServer<>0
	hszTopic = DdeCreateStringHandle_(idInst, DDETopicName, #CP_WINANSI)
	If hszTopic<>0
		;try To find the first instance, connect
		hconvServer = DdeConnect_(idInst, hszServer, hszTopic,  0)
		If hconvServer
			WhoIam="Client"
			MessageRequester( WHOIAM,"Server already started...",0)
		Else
			WHOIAM="Server":MessageRequester (WHOIAM,"attempt To start Server",0)
			Result=DdeNameService_( idInst, hszServer, 0,#DNS_REGISTER)
			If result=1
				isRun=1
			EndIf
				MessageRequester(WHOIAM,"Server started:"+Str(Result),0)
		EndIf
	EndIf
EndIf 

If OpenWindow(0, 200, 200, 300, 200, WHOIAM, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
	If WhoIam="Client"
		If CreateGadgetList(WindowID(0))
			ButtonGadget(0, 10, 10,  72, 20, "Send")
		EndIf
	EndIf
	Repeat
		EventID.l = WaitWindowEvent()
		If EventID = #PB_Event_CloseWindow
			Quit = 1
		EndIf
		If EventID = #PB_Event_Gadget
			Select EventType()
				Case 0
					Message.s="-CodeGuru- was there"
					hItemMessage = DdeCreateStringHandle_(idInst, Message, #CP_WINANSI )
					If hItemMessage = 0
						MessageRequester (WHOIAM,"Failed",0)
					EndIf
					myresult=0
					Result = DdeClientTransaction_(Message, Len(Message), hconvServer, hItemMessage, #CF_TEXT, #XTYP_POKE, 3000, @myresult )
					MessageRequester(WHOIAM,"Transaction:" +Str(Result)+":"+Str(myresult),0)   
					If Result <>0
						DdeFreeDataHandle_(Result )
					EndIf
					Result=DdeFreeStringHandle_( idInst, hItemMessage)
				EndSelect 
		EndIf
	Until Quit = 1
EndIf

Result=DdeFreeStringHandle_( idInst, hszServer)
Result=DdeFreeStringHandle_( idInst, hszTopic)

;only unregister the DDE server For first instance
If isRun
	Result=DdeNameService_(idInst, hszServer, 0, #DNS_UNREGISTER)
	MessageRequester( WHOIAM,"in ServiceUnRegister:"+Str(result), 0)
EndIf

Result=DdeUninitialize_( idInst)
MessageRequester(WHOIAM,"Ending:"+Str(Result),0)

End
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Thanks for making it work! :)

I've hit a few of these backupuser posts before and copy-paste looses all the crlfs. If you paste once to something like MS word, the copy and paste again back to the forum (or notepad, or the IDE) it fixes it.

Otherwise getting the code into the IDE can be a bit fiddly.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Apparently DDE is depreciated (by MS) and shouldn't be used for new applications? Nice routines though. ;)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

What does MS recommend in replacement of DDE?
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Dunno. :?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

DoubleDutch wrote:Apparently DDE is depreciated (by MS) and shouldn't be used for new applications? Nice routines though. ;)

Well, yes & no...


Indeed, DDE development is not continued and has not been for a couple of years, which apparently is true for NetDDE as well. There's so much software around (including withing the MS OS's themselves) that DDE will remain to be part of the OS'es.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Mistrel wrote: What does MS recommend in replacement of DDE?
Buy their software...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

maybe I don't grasp the purpose of DDE fully.

If i want to send some data between apps I just use Sendmessage
with WM_COPYDATA
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

DDE is a rather old data exchange interface between apps that run on the same machine. It dates from way back then. Later on, MS added a networking layer on top of DDE and called the resulting interface NetDDE. Both of them are supported by almost all Windows versions. Parts of the OS'es themselves rely on DDE/NetDDE being there, as do many older applications, so MS will continue to support this interface. There is no new development in this area though, as newer (and more object oriented) mechanisms are in place.

For new applications there is no need to support DDE, unless - as is the case in my current project - this app needs to communicate with some older software.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

So what is the most common way(s) to handle inter process communications now? Are named pipes getting old too? Memory mapped files?

Found this, I suppose it's time to read about pros and cons

Code: Select all

IPC Mechanism   Win2000  WinNT  Win9x   Win32s(1)  Win16(2)  MS-DOS(2)  POSIX  OS/2
   -------------  -----  -----  ------  --------   --------  --------   -----  -----

             DDE  YES     YES    YES      YES        YES       NO        NO     NO
         OLE 1.0  YES     YES    YES      YES        YES       NO        NO     NO
         OLE 2.0  YES     YES    YES      YES        YES       NO        NO     NO
         NetBIOS  YES     YES    YES      YES        YES       YES       NO     YES
     Named pipes  YES     YES    YES(3)   YES(3)     YES(3)    YES(3)    YES(4) YES
 Windows sockets  YES(5)  YES(5) YES      YES        YES(5)    NO        NO(6)  NO
       Mailslots  YES     YES    YES      YES(3)     NO        NO        NO     YES
      Semaphores  YES     YES    YES      NO         NO        NO        YES    YES
             RPC  YES     YES    YES(7)   YES(8)     YES       YES       NO     NO
 Mem-Mapped File  YES     YES    YES      YES        NO        NO        NO     NO
     WM_COPYDATA  YES     YES    YES      YES(9)     YES       NO        NO     NO


1. Win32s IS an extension to Windows 3.1 which allows Win32-based applications to run under Windows 3.1. Win32s supports all the Win32 APIs, but only a subset provides functionality under Windows 3.1. Those APIs that are not functional return ERROR_CALL_NOT_IMPLEMENTED.  
2. This is technically not a subsystem. 
3. Cannot be created on Win16, Windows 95 and MS-DOS workstations, but can be opened.  
4. The POSIX subsystem supports FIFO queues, which do not interoperate with Microsoft's implementation of named pipes.  
5. Through the Windows sockets API. 
6. Currently BSD-style sockets are under consideration for the POSIX subsystem.  
7. Windows 95 supports the RPC 1 protocol only. The NetBios protocol is not supported. Namedpipe servers are not supported.  
8. Win32s version 1.1 provides network support through Universal Thunks. 
9. Under Win32s, WM_COPYDATA does not actually copy the data -- it only translates the pointers to the data. If the receiving application changes the buffer, then the data is changed for both applications.  
10. OLE objects created in a Win32 service must be in the same user context as a logged on user that wishes to use them. Any attempt to access these objects from a different user context will result in failure. For example, a service that runs under the LocalSystem account creates an object that an application running in Domain\User's context attempts to access will fail.  

Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Thanks Paul, that's a nice overview.

For my current contract I have no choice: the app for which I need to create a remote control needs to be accessed through DDE.

Named pipes surely look mighty portable - bar that caveat....
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply