CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Guest

    DDE without controls

    Hi

    I am trying to write an application in visual basic that talks via a DDE link to our local stock exchange (South African). The problem is that I don't want to link to a VB control like a textbox, label or picturebox. I would prefer to have the information come into a variable, so that I can write it into a database table. Is this possible?

    Thanks

    Paul


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: DDE without controls

    DDE being First generation stuff (it is the grand daddy of COM), is designed to work only with controls and its base is windows messaging. OLE has developed more on it and COM is "the" thing , with COM+ and DCOM becoming "the" things ( or have they already become?!!)

    In short, if you want the values to come into variables, you should write a Ole (or COM) server and run it on the server side to which you will connect and get the values direclty.

    RK

  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: DDE without controls

    you can try to use the functions in DDEML like DdeConnect, DdeGetData and so on.



  4. #4
    Join Date
    Nov 1999
    Location
    Berks, UK
    Posts
    5

    Re: DDE without controls

    This may help (sorry but it isn't commented):


    public Function GetItemData(DDEInfo as DDEConversation) as DDEConversation
    on error GoTo ErrorHandler
    Const sProcSig = MODULE_NAME & "GetItemData"
    Dim lTransCode as Long
    Dim ErrorVal
    Dim test
    Dim DataSize as Long
    Dim StartVal as Long
    Dim StringBuffer() as Byte
    Dim ReturnString as string
    Dim lResult
    Dim DataLen

    'Create the item string handle
    DDEInfo.CurrentItemHandle = DdeCreateStringHandle(DDEInfo.idInst, DDEInfo.CurrentItem, CP_WINANSI)
    ErrorVal = DdeGetLastError(DDEInfo.idInst)
    If ErrorVal <> DMLERR_NO_ERROR then
    DDEInfo.ErrorStatus = DisplayDDEError(ErrorVal).ErrorStatus
    If Left(DDEInfo.ErrorStatus, 5) <> "error" then
    DDEInfo.ErrorStatus = DDEInfo.ErrorStatus + " When creating SERVERHANDLE in InitDDE(DDEML.BAS)"
    End If
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    DDEInfo.CurrentItemHandle = 0
    GetItemData = DDEInfo
    Exit Function
    End If

    If DDEInfo.CurrentItemHandle = 0 then
    DDEInfo.CurrentItemHandle = 0
    DDEInfo.ErrorStatus = "Unknown error when creating Item Handle in GetItemData in DDEML"
    GetItemData = DDEInfo
    Exit Function
    End If

    'Request the data
    lResult = DDEML_ClientTransaction(0, 0, DDEInfo.hConv, DDEInfo.CurrentItemHandle, CF_TEXT, XTYP_REQUEST, DDEInfo.TimeOut, 0)
    ErrorVal = DdeGetLastError(DDEInfo.idInst)
    If ErrorVal <> DMLERR_NO_ERROR then
    DDEInfo.ErrorStatus = DisplayDDEError(ErrorVal).ErrorStatus
    If Left(DDEInfo.ErrorStatus, 5) <> "error" then
    DDEInfo.ErrorStatus = DDEInfo.ErrorStatus + " When creating SERVERHANDLE in InitDDE(DDEML.BAS)"
    End If
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    test = DdeFreeDataHandle(lResult)
    DDEInfo.CurrentItemHandle = 0
    GetItemData = DDEInfo
    Exit Function
    End If

    ' get pointer to data
    StartVal = DdeAccessData(lResult, DataSize)
    ErrorVal = DdeGetLastError(DDEInfo.idInst)
    If ErrorVal <> DMLERR_NO_ERROR then
    DDEInfo.ErrorStatus = DisplayDDEError(ErrorVal).ErrorStatus
    If Left(DDEInfo.ErrorStatus, 5) <> "error" then
    DDEInfo.ErrorStatus = DDEInfo.ErrorStatus + " When creating SERVERHANDLE in InitDDE(DDEML.BAS)"
    End If
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    test = DdeFreeDataHandle(lResult)
    DDEInfo.CurrentItemHandle = 0
    GetItemData = DDEInfo
    Exit Function
    End If

    StringBuffer = Space$(256)
    DataLen = DdeGetData(lResult, StringBuffer(0), DataSize, 0)
    ErrorVal = DdeGetLastError(DDEInfo.idInst)
    If ErrorVal <> DMLERR_NO_ERROR then
    DDEInfo.ErrorStatus = DisplayDDEError(ErrorVal).ErrorStatus
    If Left(DDEInfo.ErrorStatus, 5) <> "error" then
    DDEInfo.ErrorStatus = DDEInfo.ErrorStatus + " When creating SERVERHANDLE in InitDDE(DDEML.BAS)"
    End If
    test = DdeUnaccessData(lResult)
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    test = DdeFreeDataHandle(lResult)
    DDEInfo.CurrentItemHandle = 0
    GetItemData = DDEInfo
    Exit Function
    End If

    ReturnString = StrConv(StringBuffer, vbUnicode)
    DDEInfo.CurrentItemData = Left$(ReturnString, DataLen)

    test = DdeUnaccessData(lResult)
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    test = DdeFreeDataHandle(lResult)

    ' Assign value of item name
    DDEInfo.ErrorStatus = ""
    GetItemData = DDEInfo
    Exit Function

    ErrorHandler:
    With Err
    sErrorDescription = "error '" & .Number & " " & .Description & "' occurred in " _
    & sProcSig & IIf(Erl <> 0, " at line " & CStr(Erl) & ".", ".")
    End With
    test = DdeUnaccessData(lResult)
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    test = DdeFreeDataHandle(lResult)

    DDEInfo.ErrorStatus = sErrorDescription
    GetItemData = DDEInfo
    Exit Function
    End Function

    public Function PutItemData(DDEInfo as DDEConversation) as DDEConversation
    on error GoTo ErrorHandler
    Const sProcSig = MODULE_NAME & "PutItemData"
    Dim test
    Dim ErrorVal
    Dim lResult

    DDEInfo.CurrentItemHandle = DdeCreateStringHandle(DDEInfo.idInst, DDEInfo.CurrentItem, CP_WINANSI)
    ErrorVal = DdeGetLastError(DDEInfo.idInst)
    If ErrorVal <> DMLERR_NO_ERROR then
    DDEInfo.ErrorStatus = DisplayDDEError(ErrorVal).ErrorStatus
    If Left(DDEInfo.ErrorStatus, 5) <> "error" then
    DDEInfo.ErrorStatus = DDEInfo.ErrorStatus + " When creating SERVERHANDLE in InitDDE(DDEML.BAS)"
    End If
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    DDEInfo.CurrentItemHandle = 0
    PutItemData = DDEInfo
    Exit Function
    End If

    ' Poke the data
    lResult = DDEML_ClientTransaction(Format(DDEInfo.CurrentItemData), len(DDEInfo.CurrentItemData) + 1, DDEInfo.hConv, DDEInfo.CurrentItemHandle, CF_TEXT, XTYP_POKE, DDEInfo.TimeOut, 0)
    ErrorVal = DdeGetLastError(DDEInfo.idInst)
    If ErrorVal <> DMLERR_NO_ERROR then
    DDEInfo.ErrorStatus = DisplayDDEError(ErrorVal).ErrorStatus
    If Left(DDEInfo.ErrorStatus, 5) <> "error" then
    DDEInfo.ErrorStatus = DDEInfo.ErrorStatus + " When creating SERVERHANDLE in InitDDE(DDEML.BAS)"
    End If
    test = DdeFreeDataHandle(lResult)
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    DDEInfo.CurrentItemHandle = 0
    PutItemData = DDEInfo
    Exit Function
    End If


    test = DdeFreeDataHandle(lResult)
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)

    DDEInfo.ErrorStatus = ""
    PutItemData = DDEInfo
    Exit Function

    ErrorHandler:
    With Err
    sErrorDescription = "error '" & .Number & " " & .Description & "' occurred in " _
    & sProcSig & IIf(Erl <> 0, " at line " & CStr(Erl) & ".", ".")
    End With
    test = DdeFreeDataHandle(lResult)
    test = DdeFreeStringHandle(DDEInfo.idInst, DDEInfo.CurrentItemHandle)
    DDEInfo.ErrorStatus = sErrorDescription
    PutItemData = DDEInfo
    Exit Function
    End Function


    This is my own type:

    public Type DDEConversation
    ServerName as string
    ServerHandle as Long
    TopicName as string
    TopicHandle as Long
    idInst as Long
    hConv as Long
    CurrentItem as string
    CurrentItemHandle as Long
    CurrentItemData as string
    ErrorStatus as string
    TimeOut as Integer
    End Type





    Contact me if you want more info - am trying to do a DDE Monitor prog at the moment and it's a complete *****.

    If you have any decent info on DDE stuff, I'd appreciate a copy please? All I can find is the standard stuff on the MS site (http://msdn.microsoft.com/isapi/msdn...emlib_97ub.htm) and that is all about doing it in C++

    Regards,

    Jon


  5. #5
    Join Date
    Mar 2002
    Posts
    4

    Re: DDE without controls (I need help)

    I am very grate to saw your article,but i have some questions ,wish your help.
    1.Can you give me a simply Sample about the two functions (GetItemData and PutItemData)
    2.I can not find the declare of "DDEML_ClientTransaction",why and where can I get the function?

    Thank you very much

    Cart Wang


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: DDE without controls (I need help)

    I am not an expert of this, but maybe Api guide can help you with dde.
    Have a look if following code can help you:


    'This application shows you how to use DDE to create a better PrevInstance-function.
    'Compile this sample and start it two times
    'The first instance minimizes itself and when the second instance is started, it will
    'send a message to the first instance to maximize itself.

    'in a module
    public Const VBServerName = "TestServer"
    public Const VBTopicName = "SHOW_YOUR_MAIN_WINDOW"
    public Const XCLASS_BOOL = &H1000
    public Const XTYPF_NOBLOCK = &H2 ' CBR_BLOCK will not work
    public Const XTYP_CONNECT = &H60 Or XCLASS_BOOL Or XTYPF_NOBLOCK
    public Const CP_WINANSI = 1004 ' default codepage for windows & old DDE convs.
    public Const SW_RESTORE = 9
    public Const DDE_FACK = &H8000
    public Const XCLASS_FLAGS = &H4000
    public Const XTYP_EXECUTE = &H50 Or XCLASS_FLAGS
    public Const DNS_REGISTER = &H1
    public Const DNS_UNREGISTER = &H2
    public Declare Function DdeInitialize Lib "user32" Alias "DdeInitializeA" (pidInst as Long, byval pfnCallback as Long, byval afCmd as Long, byval ulRes as Long) as Integer
    public Declare Function DdeCreateStringHandle Lib "user32" Alias "DdeCreateStringHandleA" (byval idInst as Long, byval psz as string, byval iCodePage as Long) as Long
    public Declare Function DdeConnect Lib "user32" (byval idInst as Long, byval hszService as Long, byval hszTopic as Long, pCC as Any) as Long
    public Declare Function DdeNameService Lib "user32" (byval idInst as Long, byval hsz1 as Long, byval hsz2 as Long, byval afCmd as Long) as Long
    public Declare Function DdeFreeStringHandle Lib "user32" (byval idInst as Long, byval hsz as Long) as Long
    public Declare Function DdeQueryString Lib "user32" Alias "DdeQueryStringA" (byval idInst as Long, byval hsz as Long, byval psz as string, byval cchMax as Long, byval iCodePage as Long) as Long
    public Declare Function DdeUninitialize Lib "user32" (byval idInst as Long) as Long
    Function DdeCllback(byval uType as Long, byval uFmt as Long, byval hConv as Long, byval hszTopic as Long, byval hszItem as Long, byval hData as Long, byval dwData1 as Long, byval dwData2 as Long) as Long
    DdeCllback = Form1.AppDdeCallback(uType, uFmt, hConv, hszTopic, hszItem, hData, dwData1, dwData2)
    End Function
    'in a form
    Dim isRun as Boolean, idInst as Long, hszVBServer as Long, hszVBTopic as Long
    Dim hconvVBServer as Long, dderesult as Long
    private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    If DdeInitialize(idInst, AddressOf DdeCllback, 0, 0) then Exit Sub
    hszVBServer = DdeCreateStringHandle(idInst, VBServerName, CP_WINANSI)
    hszVBTopic = DdeCreateStringHandle(idInst, VBTopicName, CP_WINANSI)
    'try to find the first instance
    hconvVBServer = DdeConnect(idInst, hszVBServer, hszVBTopic, byval 0&)
    If hconvVBServer then
    Unload me
    Exit Sub
    End If
    DdeNameService idInst, hszVBServer, 0, DNS_REGISTER
    me.WindowState = vbMinimized
    End Sub
    private Sub Form_Unload(Cancel as Integer)
    DdeFreeStringHandle idInst, hszVBServer
    DdeFreeStringHandle idInst, hszVBTopic
    'only unregister the DDE server for first instance
    If isRun then
    If DdeNameService(idInst, hszVBServer, 0, DNS_UNREGISTER) then
    MsgBox "in ServiceUnRegister", vbOKOnly, "error"
    End If
    End If
    DdeUninitialize idInst
    End Sub
    Function AppDdeCallback(byval wType as Long, byval wFmt as Long, byval hConv as Long, byval hszTopic as Long, byval hszItem as Long, byval hData as Long, byval lData1 as Long, byval lData2 as Long) as Long
    Dim iCount as Long, Buffers as string, Ret as Long
    Select Case wType
    Case XTYP_CONNECT
    iCount = DdeQueryString(idInst, hszTopic, vbNullString, 0, CP_WINANSI)
    Buffers = Space(iCount)
    DdeQueryString idInst, hszTopic, Buffers, iCount + 1, CP_WINANSI
    If Buffers = VBTopicName then
    me.WindowState = vbNormal
    'add any code for the first instance have found the second one is launch
    Ret = DDE_FACK
    End If
    AppDdeCallback = Ret
    Case XTYP_EXECUTE
    AppDdeCallback = Ret
    End Select
    End Function





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Mar 2002
    Posts
    4

    DDE without controls (thanks)

    Cimperiali:
    Thank you very much,beacause of your helps ,I can finished my program.
    I have found a good example about DDE how to write server and client at "http://www.planet-source-code.com".It's very good.
    Best wish to you

    Cart Wang




  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    :-)

    You're welcome.

    Planet source code is a good place to visit for sources...

    Have a nice day,

    Cesare Imperiali


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured