Jump to content

[Solved] C DeviceIoControl function Communication in AutoIt?


Skitty
 Share

Recommended Posts

How do I convert this to autoit? I've seen people here post examples (Manko) of communicating with device drivers, but this is ridiculously complex!

#define CODEMSG(_digitval) CTL_CODE(FILE_DEVICE_UNKNOWN,_digitval , METHOD_BUFFERED,\
                                FILE_READ_DATA | FILE_WRITE_DATA)
#define CODE_VALUE 4094


//handle to driver
HANDLE device = 0;

char ret[1024];
WCHAR SendValue[512];
DWORD codeval, bytes;


if( !strcmp(argv[i],"-p") ) {
    codeval = CODE_VALUE;
    i++; continue;
}

MultiByteToWideChar(CP_ACP, 0, argv[i], -1, SendValue, sizeof(SendValue));
DeviceIoControl(device, CODEMSG(codeval), SendValue, (wcslen(SendValue)+1)*2,
        &ret, sizeof(ret),&bytes,NULL);

The farthest I've got is~

#Include <WinAPI.au3>
$Handle = _LoadDriver()
$Struc = DllStructCreate("char[1024];WCHAR[512];DWORD")
DllStructSetData($Struc, 3, _WinAPI_MultiByteToWideChar("I have no idea what I'm doing" , 0, $MB_PRECOMPOSED , true))
MsgBox(0,"",DllStructGetPtr($Struc))
  $ret = DllCall("kernel32.dll", "int", "DeviceIoControl", _
  "dword", $Handle, _
  "dword", DllStructGetPtr($Struc,3), _
  "ptr", DllStructGetPtr($Struc), _
  "dword", 12, _
  "ptr", DllStructGetPtr($Struc), _
  "dword", 12, _
  "dword*", 0, _
  "ptr", 0)

Until I slowly started to realize I have absolutely no idea what I'm doing.

Edited by THAT1ANONYMOUSEDUDE
Link to comment
Share on other sites

Well you dont really need to convert ascii to unicode, just start of with unicode (autoit default).

CTL_CODE can be translated into this:

Func CTL_CODE($Devicetype, $Function, $Method, $Access)
Return BitOR(BitShift($Devicetype,-16),BitShift($Access,-14),BitShift($Function,-2),$Method)
EndFunc

CODEMSG:

Func CODEMSG($digit)
return CTL_CODE($FILE_DEVICE_UNKNOWN, $Digit, $METHOD_BUFFERED, BitOr($FILE_READ_DATA, $FILE_WRITE_DATA))
EndFunc

Rest can be written like this:

Global $CodeVal = 4094 ; What code value do you want to send?

$SendValue = DllStructCreate("wchar[512]")
DllStructSetData($SendValue, 1, $CMDLINE[1]) ; in the c example, this is retrieved through cmdline.
$Return = DllStructCreate("dword")
DeviceIoControl($Handle, CODEMSG($codeval),DllStructGetPtr($SendValue),(StringLen($CMDLINE[1]) + 1) * 2,DllStructGetPtr($Return), DllStructGetSize($Return), 0)

ConsoleWrite("Return code: " & DllStructGetData($Return,1) & @CRLF)

Func DeviceIoControl($hDevice, $dwIoControlCode, $lpInBuffer, $nInBufferSize, $lpOutBuffer, $nOutBufferSize, $lpBytesReturned = 0, $lpOverlapped = 0)
$Call = DllCall("Kernel32.dll","bool","DeviceIoControl","handle", $hDevice, "dword", $dwIoControlCode, "ptr", $lpInBuffer, _
"dword", $nInBufferSize, "ptr", $lpOutBuffer, "ptr", $lpBytesReturned, "ptr", $lpOverlapped)
Return $Call[0]
EndFunc

Basically, MSDN is your friend. Look everything you dont understand, it can all be translated to plain autoit. Since you are not really explaining why, or what you want to do, its hard to help you :D

E: or if youre lazy like me, launch visual c++, type the thing you want to understand, rightclick -> goto definition. Then you can see the code, or the value of constants etc.

Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Well you dont really need to convert ascii to unicode, just start of with unicode (autoit default).

CTL_CODE can be translated into this:

Func CTL_CODE($Devicetype, $Function, $Method, $Access)
Return BitOR(BitShift($Devicetype,-16),BitShift($Access,-14),BitShift($Function,-2),$Method)
EndFunc

CODEMSG:

Func CODEMSG($digit)
return CTL_CODE($FILE_DEVICE_UNKNOWN, $Digit, $METHOD_BUFFERED, BitOr($FILE_READ_DATA, $FILE_WRITE_DATA))
EndFunc

Rest can be written like this:

Global $CodeVal = 4094 ; What code value do you want to send?

$SendValue = DllStructCreate("wchar[512]")
DllStructSetData($SendValue, 1, $CMDLINE[1]) ; in the c example, this is retrieved through cmdline.
$Return = DllStructCreate("dword")
DeviceIoControl($Handle, CODEMSG($codeval),DllStructGetPtr($SendValue),(StringLen($CMDLINE[1]) + 1) * 2,DllStructGetPtr($Return), DllStructGetSize($Return), 0)

ConsoleWrite("Return code: " & DllStructGetData($Return,1) & @CRLF)

Func DeviceIoControl($hDevice, $dwIoControlCode, $lpInBuffer, $nInBufferSize, $lpOutBuffer, $nOutBufferSize, $lpBytesReturned = 0, $lpOverlapped = 0)
$Call = DllCall("Kernel32.dll","bool","DeviceIoControl","handle", $hDevice, "dword", $dwIoControlCode, "ptr", $lpInBuffer, _
"dword", $nInBufferSize, "ptr", $lpOutBuffer, "ptr", $lpBytesReturned, "ptr", $lpOverlapped)
Return $Call[0]
EndFunc

Basically, MSDN is your friend. Look everything you dont understand, it can all be translated to plain autoit. Since you are not really explaining why, or what you want to do, its hard to help you :D

E: or if youre lazy like me, launch visual c++, type the thing you want to understand, rightclick -> goto definition. Then you can see the code, or the value of constants etc.

Thanks! I'm going to play with those examples and see if I can get something happening, as for MSDN, unfortunately for me, I'm not fluent in moon speak... You should see how many of their tabs I have open trying to get a grip on this.
Link to comment
Share on other sites

Well you dont really need to convert ascii to unicode, just start of with unicode (autoit default).

Basically, MSDN is your friend. Look everything you dont understand, it can all be translated to plain autoit. Since you are not really explaining why, or what you want to do, its hard to help you :D

I love you, not in a gay way though, I've been working to get this to work for a week now and didn't wan't to ask, but I was against the wall, this works!

Only problem is I have to figure out why autoit is freezing and crashes after it successfully sends an instruction.

Again, THANK YOU!!1!11

Edit: I guess it was freezing because it needed "'ptr', 0)" appended to the end.

Edited by THAT1ANONYMOUSEDUDE
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...