Re: Using TLSAlloc to read data into a DLL




"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:OXnkFGYTIHA.6036@xxxxxxxxxxxxxxxxxxxxxxx
Ben Voigt [C++ MVP] <rbv@xxxxxxxxxxxxx> wrote:
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:OZiZTv9SIHA.4768@xxxxxxxxxxxxxxxxxxxxxxx
"Sanjay" <sanjay.gangadhara@xxxxxxxxx> wrote in message
news:bfccc6af-46c2-4091-907e-29e5b7485908@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
I am very new to Windows programming, and wanted to learn more about
the use of the function TlsAlloc. My specific application is that I
would like to read data into a file when the DLL is first loaded,
and store that data in a memory block that is untouched until the
DLL is unloaded. For my particular problem, use of the STATIC
keyword is insufficient.

What precisely do you feel is wrong with it? It looks like you can
simply allocate a block of memory in DllMain(DLL_PROCESS_ATTACH) and
save it in a global variable. Then free it in
DllMain(DLL_PROCESS_DETACH).

Note that DllMain shouldn't call any complicated routines because it
is called under OS loader lock. Allocating memory should be ok,
depending on the allocator used. Reading from a file is almost
certainly not safe.

CreateFile et al are in the kernel. It is safe to call APIs from the
kernel in DllMain.

Sanjay was trying to use fopen, not CreateFile. I don't know any guarantee
that the CRT won't play some tricks that cause problems.

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925




.



Relevant Pages

  • Re: Memory Management
    ... include the memory manager after any files that declare malloc etc. I'm not ... I have tried to export the operators from a DLL, ... I'm still using the CRT's malloc and free to allocate memory. ...
    (microsoft.public.vc.language)
  • Re: Sharing memory between kernelspace and userspace
    ... deallocate, on a totally dynamic basis, userspace ... Let userspace allocate shared memory visible to multiple ... and pass that into the kernel for it to write to. ...
    (Linux-Kernel)
  • Re: abnormal OOM killer message
    ... I have got a log message with OOM below. ... one last chance is given to allocate memory before OOM routine is executed. ... Here is some kernel source code in __alloc_pages function to understand easily. ...
    (Linux-Kernel)
  • [PATCH 1/2] x86: reinstate numa remap for SPARSEMEM on x86 NUMA systems
    ... Recent kernels have been panic'ing trying to allocate memory early in boot, ... at kernel virtual address 0. ...
    (Linux-Kernel)
  • Re: kernel memory
    ... Kernel virtual address space is used for a number of things. ... Pat of this are the memory pools, from which you can allocate memory for your drivers. ... You can allocate physical memory using a DDI like MmAllocatePagesForMdl and then map those into the system address space. ... That's probably the easiest way to explain why you cannot allocate 1GB of non-paged pool for yourself:) ...
    (microsoft.public.development.device.drivers)

Loading