Jump to content

Help with DLL struct and call


Recommended Posts

Hello,

can someone help me out with some DllCall in AU3.
The relevant source of function and struct definition are as follows (in slightly modified form):

typedef struct
{
    char    *host;
    char    *key;
    char    *value;
}
test_sender_value_t;

/******************************************************************************
 * Parameters: address   - [IN] server address                                *
 *             port      - [IN] server port                                   *
 *             source    - [IN] source IP, optional - can be NULL             *
 *             values    - [IN] array of values to send                       *
 *             count     - [IN] number of items in values array               *
 *             result    - [OUT] the server response/error message, optional  *
 *                                                                            *
 * Return value: 0 - the values were sent successfully, result contains       *
 *                         server response                                    *
 *               -1 - an error occurred, result contains error message        *
 *                                                                            *
 ******************************************************************************/
TEST_API int test_sender_send_values(const char *address, unsigned short port, const char *source, const test_sender_value_t *values, int count, char **result);


In AutoIt I use the following code to build the struct and call the dll:

Global $struct = DllStructCreate("ptr ptrtostring1;ptr ptrtostring2;ptr ptrtostring3;")

Global $string1 = DllStructCreate("char host[255];")
DllStructSetData($string1, "host", "Test_Client")
DllStructSetData($struct, "ptrtostring1", DllStructGetPtr($string1))

Global $string2 = DllStructCreate("char key[255];")
DllStructSetData($string2, "key", "clientver")
DllStructSetData($struct, "ptrtostring2", DllStructGetPtr($string2))

Global $string3 = DllStructCreate("char value[255];")
DllStructSetData($string3, "value", "123456")
DllStructSetData($struct, "ptrtostring3", DllStructGetPtr($string3))

Global $aCall = DllCall("C:\temp\test.dll", "int", "test_sender_send_values", _
    "str", "192.168.1.2", _
    "int", 80, _
    "str", "192.168.1.3", _
    "ptr", DllStructGetPtr($struct), _
    "int", 1)
If @error Then
    ConsoleWrite("Error: " & @error & @CRLF)
    exit 1
Else
    ConsoleWrite("Success: " & $aCall[0] & @CRLF)
EndIf


If I ran the AU3-Script with SciTE I get the following output:

!>12:00:00 AutoIt3.exe ended.rc:-1073741783

Can someone point me into the right direction. I must admit, that I'm very new to handling DLL-Calls and structs.
But I've read down the forum threads and the AutoIt help.

Thanks in advance.

 

Edited by MagicSpark
Link to comment
Share on other sites

@MagicSpark I'm not an expert at DLL calls either, but I see that your fourth parameter (values) says it is expecting an array of values; so I'm not sure if providing a ptr is appropriate.  That's about the only hunch I have...sorry if it leads nowhere.

4 hours ago, Mugen said:

where is the result parameter in your call?

Should be optional per the description of the DLL function, yes?

Link to comment
Share on other sites

^^ "Optional" doesn't mean optional in such way. What it really means is that (C/C++) compiler will generate the argument for programmer if omitted - as per definition.
Obviously, AutoIt is not C compiler, therefore "optional" means nothing here. It should be:

DllCall("C:\temp\test.dll", "int", "test_sender_send_values", _
    "str", "192.168.1.2", _
    "ushort", 80, _
    "str", "192.168.1.3", _
    "ptr", DllStructGetPtr($struct), _
    "int", 1, _
    "ptr*", 0)

If that fails for some reason, then calling convention should be changed to cdecl ("int:cdecl"), because it's not obvious from the OP what TEST_API is.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Thank you so much!

Especially trancexx's post help me alot. The DllCall works now with these code changes:

DllCall("C:\temp\test.dll", "int:cdecl", "test_sender_send_values", _
    "str", "192.168.1.2", _
    "ushort", 80, _
    "str", "192.168.1.3", _
    "ptr", DllStructGetPtr($struct), _
    "int", 1, _
    "ptr*", 0)

I now try to get the other needed functions in that .dll to work by myself. ;) I get back to you, if I need further help.

Thanks again.

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...