Jump to content

C++ DLL (char *string) how to implement in AutoIt


MoRBiDuS
 Share

Go to solution Solved by Danyfirex,

Recommended Posts

Hi, I've this DLL structure,
 
 
Code:
extern "C" __declspec(dllexport) void GetInfo(MODULEINFO *);

The GetInfo() function is to fill the MODULEINFO structure with information on the module: 
Code:
typedef struct { 
        DWORD dwFlags;        // Combination of flags defining different modes of hashing 
        char *szAbout;        // Address of text line containing module information 
        char *szHashType;     // Address of text line containing hash name 
        DWORD dwReserved[13]; // Reserved for future usage 
} MODULEINFO;
But I do not know how to implement in AutoIt "char * string"
 
Can you help me?
Edited by MoRBiDuS
Link to comment
Share on other sites

  • Solution

maybe this:

Local $tMODULEINFO=DllStructCreate("DWORD dwFlags;ptr szAbout;ptr szHashType;DWORD dwReserved[13]")


Local $aResult = DllCall("yourdll.dll", "none:cdecl", "GetInfo", "ptr", DllStructGetPtr($tMODULEINFO))

Saludos

Link to comment
Share on other sites

Thank you Danyfirex

 

This is the complete code, in case anyone needs it in the future...

$dll = DllOpen("md5.dll")
If $dll=-1 Then
    MsgBox(0, "", "DLL not found " & @error);
    Exit
EndIf



;~ typedef struct {
;~         DWORD dwFlags;        // Combination of flags defining different modes of hashing
;~         char *szAbout;        // Address of text line containing module information
;~         char *szHashType;     // Address of text line containing hash name
;~         DWORD dwReserved[13]; // Reserved for future usage
;~ } MODULEINFO;
$tMODULEINFO=DllStructCreate("DWORD dwFlags;ptr szAbout;ptr szHashType;DWORD dwReserved[13]")
If @error Then
    MsgBox(0, "", "Error in DllStructCreate " & @error);
    Exit
EndIf

;=========================================================
;   make the DllCall
;=========================================================
$aResult = DllCall($dll, "none:cdecl", "GetInfo", "ptr", DllStructGetPtr($tMODULEINFO))
If @error Then
    MsgBox(0, "", "Error in DllCall " & @error);
    Exit
EndIf

;=========================================================
;   Get data from pointer
;=========================================================
$ptr_szAbout = DllStructGetData($tMODULEINFO, "szAbout")
$ptr_szHashType = DllStructGetData($tMODULEINFO, "szHashType")
$data_szAbout = DllStructCreate("char[255]", $ptr_szAbout)
$data_szHashType = DllStructCreate("char[255]", $ptr_szHashType)


;=========================================================
;   Display info
;=========================================================
MsgBox(0, "DllStruct", _
        "Struct Size: " & DllStructGetSize($tMODULEINFO) & @CRLF & _
        "Struct pointer: " & DllStructGetPtr($tMODULEINFO) & @CRLF & _
        "__________________________________" & @CRLF & _
        "dwFlags: " & DllStructGetData($tMODULEINFO, "dwFlags", 1) & @CRLF & _
        "szAbout ptr: " & DllStructGetData($tMODULEINFO, "szAbout") & @CRLF & _
        "szHashType ptr: " & DllStructGetData($tMODULEINFO, "szHashType") & @CRLF & _
        "__________________________________" & @CRLF & _
        "szAbout str: " & DllStructGetData($data_szAbout, 1) & @CRLF & _
        "szHashType str: " & DllStructGetData($data_szHashType, 1))

;=========================================================
;   Free the memory allocated for the structs if needed
;=========================================================
$tMODULEINFO = 0
$data_szAbout = 0
$data_szHashType = 0

;=========================================================
;   Close DLL
;=========================================================
DllClose($dll)
Link to comment
Share on other sites

This can not work correct. You have to allocate the memory for the strings first!

Here the non-tested function:

#include <Array.au3>

;~ typedef struct {
;~         DWORD dwFlags;        // Combination of flags defining different modes of hashing
;~         char *szAbout;        // Address of text line containing module information
;~         char *szHashType;     // Address of text line containing hash name
;~         DWORD dwReserved[13]; // Reserved for future usage
;~ } MODULEINFO;
Global Const $tagMODULEINFO = "DWORD dwFlags;ptr szAbout;ptr szHashType;DWORD dwReserved[13]"


Global $aInfo = _MD5_GetInfo()
If Not @error Then
    _ArrayDisplay($aInfo)
EndIf


Func _MD5_GetInfo()
    Local $tInfo = DllStructCreate($tagMODULEINFO)
    Local $tszAbout = DllStructCreate("char szAbout[256]")
    Local $tszHashType = DllStructCreate("char szHashtype[256]")
    DllStructSetData($tInfo, "szAbout", DllStructGetPtr($tszAbout))
    DllStructSetData($tInfo, "szHashType", DllStructGetPtr($tszHashType))
    Local $aRet = DllCall("md5.dll", "none:cdecl", "GetInfo", "struct", $tInfo)
    If @error Then Return SetError(1, 0, "")
    Local $aResult[3] = [ _
        DllStructGetData($tInfo, "dwFlags"), _
        DllStructGetData($tszAbout, "szAbout"), _
        DllStructGetData($tszHashType, "szHashType")]

    Return $aResult
EndFunc

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Link to comment
Share on other sites

But you need to allocate before dll call, as I said 'first'.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I need to allocate the memory for the strings first? 
 
 
This section of my code is to fetch the data that is inside the pointers provided by the DLL:
;=========================================================
;   Get data from pointer
;=========================================================
$ptr_szAbout = DllStructGetData($tMODULEINFO, "szAbout")
$ptr_szHashType = DllStructGetData($tMODULEINFO, "szHashType")
$data_szAbout = DllStructCreate("char[255]", $ptr_szAbout)
$data_szHashType = DllStructCreate("char[255]", $ptr_szHashType)
The strucs "data_szAbout $" and "$ data_szHashType" are  necessary before dll call ?
Edited by MoRBiDuS
Link to comment
Share on other sites

The MODULEINFO has pointers for the string position. You have to make structs for the strings and tell the position of the strings to the MODULEINFO struct. Then the GetInfo() function can fill the strings to the position of the reserved memory for the strings. Go step by step through the code in post #5 to understand how it has to works.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

funkey, where did that come from. I mean, how do you know that? Is there some documentation by the dll dev available? If so, would you mind linking it.

Otherwise, I see no reason to think your code is more correct than the other. Maybe the dll allocates memory space for the strings. That's also valid option.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

funkey, where did that come from. I mean, how do you know that? Is there some documentation by the dll dev available? If so, would you mind linking it.

Otherwise, I see no reason to think your code is more correct than the other. Maybe the dll allocates memory space for the strings. That's also valid option.

Function looks like this in post#10: https://forum.antichat.ru/printthread.php?t=91666&page=13&pp=40

So if this is the code there is no memory allocation in the dll. And if there was one the memory has to be freed.

But of course I could be wrong.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

it does not need to be allocate because it return a pointer to a const in .idata section who inizialized with size of 24 bytes.

@funkey Your code will not work correctly because the fucntion will fill the structure. it will overwrite your pointer passed in the structure.

 

Saludos

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...