Posted-Date:  13 Dec 92 14:25 EST
Newsgroups: bit.listserv.win3-l
Date:         Sun, 13 Dec 1992 14:22:00 EST
From:         Vince Scarafino <Scarafino@MULTICS-A.PMS.FORD.COM>
Subject:      Re: MakeProcInstance()

I've been struggling with an attempt to use a callback function in a
Windows API.  The function being specified is located in a DLL.  The
name of the function is "get_handle".  The routine that invokes the API
to set this up is called "infodt", and is being called from a Visual
Basic program.  Here is the source:


 #include "windows.h"

 #include <toolhelp.h>

 int __export __pascal infodt (int i,
                                        int j)
 {

    int ret_value;
    HTASK current_task_handle;
    FARPROC  lpfnGetHandleProc;
    BOOL CALLBACK get_handle (HWND window_handle, LPARAM paramater);
    TASKENTRY taskentry;
    HINSTANCE current_instance_handle;
    current_task_handle = GetCurrentTask();
    taskentry.dwSize = sizeof taskentry;
    if (!TaskFindHandle (&taskentry, current_task_handle))
        return (i - j);
    current_instance_handle = taskentry.hInst;
    lpfnGetHandleProc = MakeProcInstance (get_handle, current_instance_handle);
    ret_value = EnumTaskWindows(current_task_handle, lpfnGetHandleProc, 0L);
    return (ret_value);

 }

This program attempts to invoke "get_handle" as a callback function from
"EnumTaskWindows" The "get_handle" function follows:

 #include "windows.h"

 BOOL CALLBACK get_handle (HWND window_handle, LPARAM parameter)

 {
    char opBuf[5];
    char inBuf[] = "infodt";
    int ret_value;
    LPSTR inBuf_ptr;
    LPSTR opBuf_ptr;
    inBuf_ptr = (LPSTR) inBuf;
    opBuf_ptr = (LPSTR) opBuf;
    opBuf[0] = 'T';
    opBuf[1] = 'e';
    opBuf[2] = 's';
    opBuf[3] = 't';
    opBuf[4] = NULL;
    SetWindowText(window_handle, opBuf_ptr);
    ret_value = MessageBox(NULL, opBuf, inBuf_ptr, MB_ICONEXCLAMATION);
    return (FALSE);

}

The DEF file used to produce the DLL follows:

 LIBRARY FLIB

 EXETYPE WINDOWS

 CODE PRELOAD MOVEABLE DISCARDABLE DATA PRELOAD SINGLE

 HEAPSIZE 1024

 EXPORTS
          WEP       @1

 IMPORTS
          toolhelp.TerminateApp
          toolhelp.TaskFindHandle



The EnumTaskWindows call does manage to call get_handle.  However, the
local variable information is incorrect before execution is turned over
to get_handle.  Thus the value of inBuf is lost, while the value of
opBuf, which is constructed at runtime, is correct for the call to
SetWindowText and MessageBox.

The programs are constructed to help me understand the Windows 3.1 API.
They're not designed to do anything else of value.  Thus, they may
appear to be a bit wierd.  However, this is managing to help my
understanding in some areas and confuse me in others.

Since the function "get_handle" is in a DLL, do I need to even bother
with MakeProcInstance?  The program behaves the same when the
MakeProcInstance call is omitted.  I would interpret the fact that DLLs
are single instance to mean the MakeProcInstance is unnecessary, and
indeed it is the reason MakeProcInstance returns the pointer handed to
it unmodified for DLLs.  Am I correct?

In any case, my local variables in get_handle are getting wiped out.  I
added a direct call to get_handle after the statement
"current_task_handle = GetCurrentTask();".  The added line was:

          get_handle (NULL, 0L);

The routine behaved properly from this call.  The local variable info
was correct, and I got the results I expected.  Is there something
special about specifying a DLL entry as a callback function?  Anyone
have an idea of what might be going on?  Any insights would be
appreciated.

Thanks, Vince
Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU