多线程应用程序中的 MASM dll 内存分配

MASM dll memory allocation in multithreading application

我询问了如何在 MASM 中动态分配内存 但我还有 2 个问题。

如何为字节分配内存?

.data
tab DB ?
result DB ?

.code
invoke GetProcessHeap
; error here
mov tab, eax          ; I cannot do this because of wrong sizes, AL and AH are equal 0
INVOKE HeapAlloc, tab, 0,  <size>

invoke GetProcessHeap
mov result, eax          ; same here
INVOKE HeapAlloc, result, 0,  <size>

第二个问题,我可以在多线程应用程序中使用这种分配内存的方法还是应该使用GlobalAlloc?

HeapAlloc 函数接受 3 个参数:

hHeap - 堆对象句柄

flags - 标志,关于如何分配内存

size - 你需要的内存块大小

函数 returns EAX 中的一个双字,它是指向已分配内存的指针。

您不需要在每次调用 HeapAlloc 时都调用 GetProcessHeap

变量tabresult必须是双字,因为指针是双字长(eax)

这些指针所指向的内存块可以按您需要的任何数据大小进行访问。 它们只是内存块。

Windows 堆函数是线程安全的,您可以在多线程应用程序中使用它们。

这一切在汇编中的样子:

    .data

tab    dd ?
result dd ?

    .code

    invoke GetProcessHeap
    mov    ebx, eax          ; the heap handle

    invoke HeapAlloc, ebx, 0, <size>
    mov    tab, eax     ; now tab contains the pointer to the first memory block     

    invoke HeapAlloc, ebx, 0,  <size>
    mov    result, eax          ; now result contains the pointer to the second block