WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: kdmoyers on November 16, 2020, 01:14:40 PM

Title: Trouble with DllStructAlloc
Post by: kdmoyers on November 16, 2020, 01:14:40 PM
Don't ask why I'm looking at this now, but,
I can't see what I'm doing wrong here.  I get 1378 DllCall Bad Parm Code
I can't find an example of DllStruct actually being used in a call, but this is my best guess. 
Can anyone tell me what's wrong?
Code (winbatch) Select
    ;    int MessageBoxA(
    ;      HWND   hWnd,
    ;      LPCSTR lpText,
    ;      LPCSTR lpCaption,
    ;      UINT   uType
    ;    );
    ;
    ; from https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxa
    ;

    user32 = DirWindows(1) : "user32.DLL"

    caption = "attention!"
    text = "hello there"
    wtype = 48
    myself = DllHwnd("")

    descriptor = "hwnd:winhandle lpcstr:text lpcstr:caption uint:control"
    structhand = DllStructAlloc( descriptor )

    dllstructpoke(structhand, "winhandle",  myself  )
    dllstructpoke(structhand, "text",       text    )
    dllstructpoke(structhand, "caption",    caption )
    dllstructpoke(structhand, "winhandle",  wtype   )

    xx = DllCall(user32, long:"MessageBoxA", structhand)

    dllstructfree(structhand)
Title: Re: Trouble with DllStructAlloc
Post by: td on November 16, 2020, 02:11:02 PM
Yikes!?  DllStructAlloc and friends are intended for use in calls into C based DLL export functions when those functions expect a pointer to a C data structure known as a "struct" in the C language parlance.   Or as the help file describes it, "The returned handle can be used with DllCall or DllCallCdecl when calling an external DLL entry point that requires the memory address of a C/C++ language style structure as a parameter."

The MessageBox Win32 function does not take a pointer to a structure as a parameter so you can't use a DllStructAlloc created structure when calling the function.

There are several examples of using DllStructAlloc on the forum.  Here is a link to one of them:

https://forum.winbatch.com/index.php?topic=2397.msg12697#msg12697 (https://forum.winbatch.com/index.php?topic=2397.msg12697#msg12697)
Title: Re: Trouble with DllStructAlloc
Post by: kdmoyers on November 16, 2020, 03:18:41 PM
Got it.  Makes sense, thanks Tony!

BTW, when I type DllStructAlloc into the tech database search, it comes up "Sorry, no items matching".  How did you find that example?

-Kirby
Title: Re: Trouble with DllStructAlloc
Post by: td on November 16, 2020, 05:05:08 PM
The example is from this forum and not the Tech Database.   It does seem a bit odd that there are no examples in the Tech Database though.  We will have to do something about that when time permits.
Title: Re: Trouble with DllStructAlloc
Post by: td on November 19, 2020, 02:46:06 PM
Here is a new Tech Database DllStructAlloc example:

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DllCall~Information+DllStructAlloc~Example.txt (https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DllCall~Information+DllStructAlloc~Example.txt)