Make your own free website on Tripod.com

AllApi.net

[an error occurred while processing this directive]
 
'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: KPDTeam@Allapi.net
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


 
Copyright © 1998-2000, The KPD-Team.
Send mail to KPDTeam@Allapi.net with comments about this web site.
This site is located at
http://www.Allapi.net/