Jump to content

Help with DLLStruct and DLLCall


Recommended Posts

Hello Guys,

once aggain I need your help on a DLL Topic :)

I need to pass arguments to my function via a structure, because I am limited to only one argument that can be passed.
But I don't think that thats so important.

So here's my approach:

C++ Code (just the important part):

extern "C"
{

    struct ParamStruct
    {
        const char* test1;
        const char* test2;
        const char* test3;
        int size;
    };

    int testFunc(struct ParamStruct * params)
    {   
        return params->size;
    }
}

And thats how I try to call the function using Autoit:

Local $struct = "struct;char shapefile[128];char output[128];char filename[128];int size;endstruct"
Local $tTest = DllStructCreate($struct)
DllStructSetData($tTest, "test1", "Bla")
DllStructSetData($tTest, "test2", "BlaBla")
DllStructSetData($tTest, "test3", "BlaBlaBla")
DllStructSetData($tTest, "size", 40)

$dll = DLLOpen("myDLL.dll")
$ret = DllCall($dll, "int:cdecl", "testFunc", "STRUCT*", DllStructGetPtr($tTest))
MsgBox(0, 0, $ret[0])

DllClose($dll)

Just for testing I want the function to just return the integer value in the struct.

But this aint working. I tested many things, but still I'm not able to get it running. I even don't know if the mistakes are in the C++ code or the Autoit code or both .. I'm not that skilled at C++ and also not that skilled at Autoit DLLCalls :-/

I would really appreciate some help! :)

Kind regards,
leo

Edited by Leo1906
Link to comment
Share on other sites

Ok, I got it to work. Don't know what I changed but now it works ..

Another question: If I pass the Struct by ref, why can't Autoit get the changed values from the Struct?
Thats how it's done:

extern "C"
{

    struct ParamStruct
    {
        const char* test1;
        const char* test2;
        const char* test3;
        int size;
    };

    int testFunc(struct ParamStruct &params)
    {   
        params.size = 2;
        return params.size;
    }
}
#NoTrayIcon
Local $struct = "struct;char shapefile[128];char output[128];char filename[128];int size;endstruct"
Local $tTest = DllStructCreate($struct)
DllStructSetData($tTest, "test1", "bla")
DllStructSetData($tTest, "test2", "blabla")
DllStructSetData($tTest, "test3", "blablabla ")
DllStructSetData($tTest, "size", 40)


$dll = DLLOpen("Split_DLL.dll") ;<- open DLL
$ret = DllCall($dll, "int:cdecl", "testFunc", "STRUCT*", DllStructGetPtr($tTest))
MsgBox(0, 0, DllStructGetData($tTest, 4))

MsgBox(0, 0, $ret[0])
MsgBox(0, 0, DllStructGetData($tTest, 4))

DllClose($dll)

 

Edit: and another question. Can I use Strings in a Struct with Autoit? Can't find that dataformat in the helpfile regarding structs ..?

Edit2: Well the code seems not to work though .. as soon as I want to access the char's in the DLL Code Autoit crashes ..?

Edited by Leo1906
Link to comment
Share on other sites

ParamStruct is defining char pointers while the struct you create in Autoit is declaring char arrays.

Perhaps ParamStruct should look more like:
 

struct ParamStruct
    {
        char const test1[128];
        char const test2[128];
        char const test3[128];
        int size;
    };

 

Link to comment
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
 Share

×
×
  • Create New...