Sign in to follow this  
Followers 0
tehhahn

Help with DllStructCreate / Calling C DLL functions

2 posts in this topic

Hi everybody,

I'm dealing with C functions exported in a dynamic link library at the moment. For that, I've implemented a simple Stack in C. Im already able to call functions, so building the .dll file and accessing its functions is not a problem for me. The problem is the AutoIt function DllStructCreate. I do not know how to create nested structures in AutoIt. I have the following implemented in C:

stack.h

#ifndef STACK_H
#define STACK_H

typedef int stackDataType;

typedef struct stack {
    stackDataType vValue;
    struct stack *pNext;
} SStack;

#ifdef  __cplusplus
extern "C" {
#endif

#ifndef DLL_TEST
#define DLL_DECLARE __declspec(dllexport)
#endif

DLL_DECLARE void dll_check();
DLL_DECLARE stackDataType pop(SStack **head);
DLL_DECLARE void push(SStack **head, stackDataType vValue);

#ifdef  __cplusplus
}
#endif

#endif

stack.c (not really relevant)

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"

void dll_check() { printf("Works"); }

stackDataType pop(SStack **head) {
    SStack *pTop = *head;
    stackDataType vValue = pTop->vValue;
    *head = pTop->pNext;
    free(pTop);
    return vValue;
}

void push(SStack **head, stackDataType vValue) {
    SStack *pNode = malloc( sizeof(SStack) );
    pNode->vValue = vValue;
    pNode->pNext = *head;
    *head = pNode;
}

And last, but not least the CppStack.au3:

#include-once

Exit Main()

Func Main()
    Local Const $hDLL = _StackCpp_Startup()
    MsgBox(64, "_StackCpp_Startup", @error)

    _StackCpp_Check($hDLL)
    MsgBox(64, "_StackCpp_Check", @error)

    _StackCpp_Push($hDLL, 1)
    MsgBox(64, "_StackCpp_Push", @error)

    _StackCpp_Shutdown($hDLL)
EndFunc   ;==>Main

Func _StackCpp_Startup($sFile = Default)
    Local $h = 0
    If $sFile = Default Then $sFile = @ScriptDir & "\libCppStack.dll"
    $h = DllOpen($sFile)
    If $h = -1 Then Return SetError(1, 0, False)
    Return $h
EndFunc   ;==>_StackCpp_Startup

Func _StackCpp_Shutdown(ByRef Const $h)
    DllClose($h)
EndFunc   ;==>_StackCpp_Shutdown

;~ Works...
Func _StackCpp_Check(ByRef Const $h)
    DllCall($h, "none", "dll_check")
    If @error Then Return SetError(@error, 0, False)
    Return True
EndFunc   ;==>_StackCpp_Check

;~ Does not work since the DllCall call fails with a runtime error...
Func _StackCpp_Push(ByRef Const $h, Const $V)
    Local Const $SSTACK = DllStructCreate("int;int")
    DllCall($h, "none", "push", "ptr", DllStructGetPtr($SSTACK), "int", $V)
    If @error Then Return SetError(@error, 0, False)
    Return True
EndFunc   ;==>_StackCpp_Push

My problem is to deal with the following two lines, since the second datatype in the structure is the structure itself.

Local Const $SSTACK = DllStructCreate("int;int")
DllCall($h, "none", "push", "ptr", DllStructGetPtr($SSTACK), "int", $V)

Can somebody explain me, how to realize that nested structure in AutoIt? I've added the project (NetBeans IDE) to the appendix!

Thanks!

CppStack.zip

Share this post


Link to post
Share on other sites



You just have to change calling convention and all should work. If you want to do that from your script then it's:

DllCall($h, "none", "push:cdecl", "ptr", DllStructGetPtr($SSTACK), "int", $V)

Or before compiling (to use stdcall). In that case AutoIt script can stay the way it is now.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0