TSRs in VB

how can we write TSR programs in VB?
what are the fuctions used in writing TSRs?
if possible give me a background program which stores the text entered from a keyboard into a file.
sunilvirusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

st_steveCommented:
TSRs are for DOS.....technically, we don't use them in Windows anymore...we call them "service modules" (or something like that) nowadays...if you want to write TSR, you need to know some assembly language, coz the program has to allocate some memory and hook it so that it stays resident. there are two portions in a TSR program; the transient part and and memory-resident part. the transient parts loads first, allocates necessayr memory and loads the resident part, then it exits. it's actually the resident part that does the useful work.

Basically, you need to be very familar with INT 27h calls...that's the servicing for hooking routines. Some people also use INT 21h (Service 53 or 51 - I can't remember) but don't use the INT 21h unless you want some lawsuit from Microsoft...coz I hear that that service routine is "undocumented"!!!

Well...enough said.....need more info?? I don't know a lot more! :) that's the truth!
0
TimCotteeHead of Software ServicesCommented:
Agreed, TSR programs are not "Windows", it depends entirely on what it is that you want your program to do as to how you implement it in VB. Creating an executable that can be run as a windows service is one method. If you can give more information on what you are trying to do then perhaps I can be more specific in my comments.
0
VBGuruCommented:
 
PSS ID Number: Q95545
Article last modified on 09-21-1998
 

 

 

======================================================================
----------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Windows Software Development Kit (SDK) for Windows
   versions 3.1, 3.0
----------------------------------------------------------------------
 
SUMMARY
=======
 
GlobalDosAlloc() is used by Windows-based applications and dynamic-link
libraries (DLLs) to allocate memory blocks that are guaranteed to
exist in the first megabyte (MB) of linear address space.
GlobalDosFree() is used to free memory blocks allocated by
GlobalDosAlloc(). Memory blocks allocated by GlobalDosAlloc() are
fixed in standard mode and page locked in 386 enhanced mode.
 
Neither of these functions is prototyped in the Microsoft Windows
version 3.0 Software Development Kit's WINDOWS.H; however, both are
correctly prototyped in the Microsoft Windows version 3.1 SDK's
WINDOWS.H. Furthermore, both functions are prototyped incorrectly in
the Windows 3.0 SDK "Reference, Volume 1" and in the Windows 3.1 SDK
"Programmer's Reference, Volume 2: Functions." The correct prototypes
are:
 
   DWORD FAR PASCAL GlobalDosAlloc(DWORD cbAlloc);
   UINT FAR PASCAL GlobalDosFree (UINT uSelector);  // in Windows 3.1
   WORD FAR PASCAL GlobalDosFree (WORD wSelector);  // in Windows 3.0
 
The return value of GlobalDosAlloc(), if successful, contains a
selector in the low-order word and a paragraph-aligned segment value
in the high-order word. If GlobalDosAlloc() fails, it returns zero. If
GlobalDosFree() succeeds, it returns zero. If it fails,
GlobalDosFree() returns the value of the selector passed to it.
 
Memory below 1 MB is scarce and should not be allocated by
applications or DLLs unless absolutely necessary. For further
information about how memory below 1 MB is used, query on the
following words in the Microsoft Knowledge Base:
 
   GlobalDosAlloc() or memory
 
MORE INFORMATION
================
 
The following code sample demonstrates how to use GlobalDosAlloc() and
GlobalDosFree():
 
   DWORD dwMyLowMemory;
   UINT  uMySegment;
   UINT  uMySelector;
   LPSTR lpProtModePtr;
   DWORD lpRealModePtr;
    .
    .
    .
 
   if ((dwMyLowMemory = GlobalDosAlloc(512)) == 0)
      return FALSE;    // Error: unable to allocate memory
 
   // GlobalDosAlloc() allocates global memory that can be accessed by
   // real-mode applications. The memory is guaranteed to exist in the
   // first megabyte of linear address space.
   //
   // GlobalDosAlloc() returns a DWORD in which the high-order word is a
   // paragraph segment and the low-order word is a selector.
 
   uMySegment  = HIWORD(dwMyLowMemory);
   uMySelector = LOWORD(dwMyLowMemory);
 
   lpProtModePtr = (LPSTR)MAKELONG(0, uMySelector);
                                               // protected mode pointer
 
   lpRealModePtr = (DWORD)MAKELONG(0, uMySegment);
                                               // real mode pointer
    .
    .
    .
 
   if ((uMySelector = GlobalDosFree(uMySelector)) != NULL)
      return FALSE;    // Error: unable to free memory
 
This example works correctly in all Windows modes. The selector value
returned by GlobalDosAlloc() does not need to be locked via a call to
GlobalLock() because it is already locked. Because the value of
uMySegment is a paragraph-aligned segment value, the starting offset
of the memory object will be zero. In Windows 3.0 real mode,
GlobalDosAlloc() returns segment values in both words.
 
GlobalDosAlloc() allocates memory below 1 MB in linear address space. In
standard mode, this memory is in the first megabyte of physical memory
because linear addresses are the same as physical addresses. In 386
enhanced mode, however, linear addresses are not the same as physical
addresses. Memory allocated via GlobalDosAlloc() is not guaranteed to
reside in the first megabyte of physical memory nor to be physically
contiguous. Furthermore, because the physical address of a memory
block allocated via GlobalDosAlloc() is not known in 386 enhanced mode,
it is not suitable for DMA transfers or memory-mapped hardware.
 
In 386 enhanced mode Windows, GlobalDosAlloc() allocates memory blocks
in the Windows virtual machine only. Terminate-and-stay-resident
programs (TSRs) and MS-DOS device drivers that were started before
Windows are global to all virtual machines and thus can use these
memory blocks. However, TSRs and MS-DOS-based applications that are started
in Windows MS-DOS boxes run in their own virtual machines and so cannot
see the memory allocated by GlobalDosAlloc(). Because memory allocated
by GlobalDosAlloc() is not global to all virtual machines, it cannot
be used to communicate asynchronously (based on hardware interrupts,
for example) with TSRs. For examples of Windows-based applications that
communicate with TSRs, study the GDOSMEM and TDOSMEM sample
applications included in the Windows 3.1 SDK, or query on the
following words in the Microsoft Knowledge Base:
 
   GDOSMEM or TDOSMEM
 
If GlobalDosAlloc() is called from within a DLL, the allocated memory
block is owned by the application instance that called the DLL, not
the DLL. However, it is sometimes necessary for a DLL to own a block
of memory that MS-DOS can access. (For example, a DLL may need its own
buffer to communicate with a TSR.)
 
Because GlobalDosAlloc() doesn't take a parameter to specify the
attributes of the memory block to be allocated as GlobalAlloc does,
how can the memory be assigned to a DLL? The DLL can allocate the
memory on behalf of the calling application instance and then call
GlobalReAlloc() to change the ownership of the memory block to itself.
The following code sample demonstrates this technique:
 
   // Called from within a DLL
 
   HGLOBAL hMyLowMem;
   DWORD   dwMyLowMemory;
   UINT    uSelector;
   LPSTR   lpszProtModePtr;
    .
    .
    .
   if ((dwMyLowMemory = GlobalDosAlloc (512)) == 0)
      return (FALSE);      // Error: unable to allocate memory
 
   uSelector = LOWORD(dwMyLowMemory);
   lpszProtModePtr = (LPSTR)MAKELONG(0, uSelector);
 
   hMyLowMem = LOWORD(GlobalHandle (uSelector));
 
   hMyLowMem = GlobalReAlloc (hMyLowMem, 0, GMEM_MODIFY |
                                            GMEM_DDESHARE);
    .
    .
    .
 
After the call to GlobalReAlloc(), the memory block will belong to the
DLL and can be accessed by lpszProtModePtr. To free the memory block,
just call GlobalDosFree(); there is no need to change the ownership of
the block back to its original owner.
 
GlobalDosAlloc() and GlobalDosFree() are not the same as the DPMI DOS
Memory Management Services (functions 100h to 102h). Windows
applications and DLLs should not use these DPMI functions because
GlobalDosAlloc() and GlobalDosFree() perform the same functions.
 
Important note: Applications and DLLs should not use GlobalDosAlloc()
unless absolutely necessary because the free memory pool below 1 MB
from which it allocates is a scarce system resource.
 
GlobalDosAlloc() and GlobalDosFree() are documented on pages 4-236 and
4- 237 of the Microsoft Windows SDK 3.0 "Reference, Volume 1" and on
pages 501 and 502 of the Microsoft Windows SDK 3.1 "Programmer's
Reference, Volume 2: Functions."
 
Additional query words:
======================================================================
Keywords          : kb16bitonly
=============================================================================
Copyright Microsoft Corporation 1998.


 
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

VBGuruCommented:
sorry to say that the function GlobalDosAlloc is not supported in the windows 4.0 onwards.
0
st_steveCommented:
VBGuru..you might need to translate the code you've posted into a VB code....so that VB people in here can understand...your code is in C/C++
0
st_steveCommented:
not putting the experts down or anything....but I speak for myself....C/C++ makes me dizzy!! I know C/C++ can do just about anything (come on....unix was written in C, right?) but ...VB is fine for me....thanks
0
mark2150Commented:
A TSR, by definition, is loaded *PRIOR* to windows. Since VB depends on Windows .DLL's for it's operation it's simply not possible to write a "true" TSR in VB.

TSR's are written in .ASM to perform specific, dedicated, functions. Memory managers, device drivers, etc. are all examples of TSR's.

It *is* possible to write "non-terminating" VB programs. These can be loaded at startup and left running as long as windows is up. If this is what you're after you can do a variety of things on your system, but these are not "TSR's" in the strictest sense.

As VBGuru said:

"Terminate-and-stay-resident
programs (TSRs) and MS-DOS device drivers that were started before
Windows are global to all virtual machines and thus can use these
memory blocks. However, TSRs and MS-DOS-based applications that are started
in Windows MS-DOS boxes run in their own virtual machines and so cannot
see the memory allocated by GlobalDosAlloc(). Because memory allocated
by GlobalDosAlloc() is not global to all virtual machines, it cannot
be used to communicate asynchronously (based on hardware interrupts,
for example) with TSRs."

M
0
st_steveCommented:
See...this is what I said

quote from "mark2150"

"It *is* possible to write "non-terminating" VB programs. These can be loaded at startup and left running as long as windows is up. "

programs like "systray" and "nprotect" (norton antivirus auto-protect) are close examples of Windows "TSR"s
0
mark2150Commented:
St_Steve, yes.

But they are not "true" TSR's as they can be unloaded (unless extreme coding effort is used).

M
0
st_steveCommented:
so "sunilvirus"...still want to write TSRs in win environment?? haven't heard from you....??
0
sunilvirusAuthor Commented:
Edited text of question.
0
VBGuruCommented:
st_steve, Sorry for the late response. It was posted here to show some info about TSR programming here. About transklatig the code, as it is an absolete piece of code and windows95 no more supports this. Currently I don't have windows 3.1 to test the translated code if at all I do.
Please not the it was posted to see the info about the tsrs in MSDN not to tell anything about C/C++ or VB.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBGuruCommented:
st_steve,  Its by mistake I applied it as an answer. You can reject it.
0
st_steveCommented:
VBGuru....I got your point....the same point as well that I made....people don't use TSRs anymore...and I can't reject the answer...coz I wasn't the one who asked the question! lol....

if you have some more info on TSRs...post some as well..it's good to read these stuff...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.