Example using @WINAPI/@CAPI

I'm trying to use either @WINAPI or @CAPI to execute a function in a DLL. I've written C++ code that can use the DLL, so I know the name of the function(s), and I know the DLL is working/functional.

The prototype for the function(s) I'm wishing to "wrap" are:
LPCSTR (CALLBACK* LPFNDLLFUNC)(LPCSTR)

The function names are "Encrypt" and "Decrypt".

As near as I can tell, the correct use of either variable function is something like:

echo %@xAPI[mydll.dll,Encrypt,aBUFFER,"plaintext"] (where xAPI is either WIN or C)

and it should return the text value that is the string being pointed to by the function's return value. However, in practice, nothing is returned, so I know that I'm missing something.

Can anyone shed some light on what I'm missing? I'd really like to be able to use this...
 
try it without the commas ,

----- Original Message -----


| I'm trying to use either @WINAPI or @CAPI to execute a function in a DLL.
I've written C++ code that can use the DLL, so I know the name of the
function(s), and I know the DLL is working/functional.
|
| The prototype for the function(s) I'm wishing to "wrap" are:
| LPCSTR (CALLBACK* LPFNDLLFUNC)(LPCSTR)
|
| The function names are "Encrypt" and "Decrypt".
|
| As near as I can tell, the correct use of either variable function is
something like:
|
| echo %@xAPI[mydll.dll,Encrypt,aBUFFER,"plaintext"] (where xAPI is either
WIN or C)
|
| and it should return the text value that is the string being pointed to by
the function's return value. However, in practice, nothing is returned, so I
know that I'm missing something.
|
| Can anyone shed some light on what I'm missing? I'd really like to be able
to use this...
 
try it without the commas ,

Why would doing it w/o commas help? At least with the commas, the proc knows which argument is which. In practice, it makes no difference, which is a surprise to me; I thought for certain that I'd end up with argument mismatch errors.

Additional data--when run as:

echo %@xAPI[mydll.dll,Encrypt,a"plaintext"]

an integer is echoed; I'm going to assume that it's the INT representation for the return value, a long-pointer to a c-style string. Unfortunately, there isn't any way that I can conceive to 'get ahold' of the string at that 'address'.
 
May 30, 2008
42
0
From: millardjk
Sent: Friday, August 22, 2008 4:16 PM
Subject: RE: [Support-t-407] Re: Example using @WINAPI/@CAPI
[snip]

> an integer is echoed; I'm going to assume that it's the INT
> representation for the return value, a long-pointer to a c-style string.
> Unfortunately, there isn't any way that I can conceive to 'get ahold' of
> the string at that 'address'.

It *is* possible :-) I did it a while back for my BTM file that lists users
by calling the network APIs. The trick is actually rather devious. Recall
that @WINAPI can take a BUFFER (or an aBUFFER) as a target into which a
value for return by @WINAPI can be placed. If you have a pointer, you can
pass it into the CopyMemory API to have it blitted into the provided buffer.
The CopyMemory API is actually an intrinsic backed by RtlMoveMemory in
kernel32.dll, so you could try something like this:

set ptr_to_string=%@WINAPI[mydll,Encrypt,a"plaintext"]
set
ciphertext=%@WINAPI[kernel32,RtlMoveMemory,aBUFFER,%ptr_to_string,%ciphertex
t_length]

To do this, you need to determine what the length of the ciphertext
%ciphertext_length actually is. If this is an easy computation, you can just
do that and pass it in *plus 1 for the NUL character*. If you need to strlen
it, then:

:StringLength [strptr]
set count=0
gosub ReadByteFromPtr %@eval[%strptr + %count]
do while %byte gt 0
set /a count=count + 1
gosub ReadByteFromPtr %@eval[%strptr + %count]
enddo

return

:ReadByteFromPtr [byteptr]
set byte=%@winapi[kernel32,RtlMoveMemory,BUFFER,%byteptr,1]

iff "%byte" == "" then
set byte=0
else
set byte=%@eval[%@unicode[%byte] %% 256]
endiff

return

I haven't actually tested the StringLength subroutine, but I know
ReadByteFromPtr works as I copied it directly from ENUMUSERS.BTM. The
trickiest part of ReadByteFromPtr is the codepage conversion that takes
place when you return a character. I got around this by returning it as
Unicode, but since only one byte is asked for I could only RtlMoveMemory a
single byte. That means that the upper 8 bits of the unicode character could
be anything. The modulus operator makes quick work of that, returning the
lower 8 bits only.

Hope this helps :-)

Jonathan Gilbert
 
May 30, 2008
42
0
I hit 'Send' at 11:03 AM. Item showed up at 12:01 PM. 58 minute delay! :-(((

Jonathan Gilbert

-----Original Message-----
From: logic [mailto:]
Sent: Monday, August 25, 2008 12:01 PM
To: jgilbert@workwireless.com
Subject: RE: [Support-t-407] Re: Example using @WINAPI/@CAPI

[snip]

Hope this helps :-)

Jonathan Gilbert
 
From: millardjk
Sent: Friday, August 22, 2008 4:16 PM
Subject: RE: [Support-t-407] Re: Example using @WINAPI/@CAPI
[snip]



It *is* possible :-) I did it a while back for my BTM file that lists users
by calling the network APIs. The trick is actually rather devious. Recall
that @WINAPI can take a BUFFER (or an aBUFFER) as a target into which a
value for return by @WINAPI can be placed. If you have a pointer, you can
pass it into the CopyMemory API to have it blitted into the provided buffer.
The CopyMemory API is actually an intrinsic backed by RtlMoveMemory in
kernel32.dll, so you could try something like this:

[snip]
Hope this helps :-)

Perfect! No issues with the length of the cyphertext, either; this DLL implements a stream cypher, so the length of the cyphertext can be reliably predicted from the length of the plaintext:
%@EVAL[%@LEN[%plaintext]+1]

Thanks for the assist; it's now working very reliably!
 
Similar threads
Thread starter Title Forum Replies Date
Dan Glynhampton Documentation v15 help: Syntax error in example of @DEC Support 0
D ini directives and @select example in Help Support 0
W JABBER help/example? Support 11
dcantor @DATE help example Support 0
dcantor @AGEDATE example incorrect in help Support 0
deleyd Need example of @COMPARE Support 1
Jesse Heines Using Regular Expressions with the REN commanc Support 8
R WAD Unusable state when using Chinese characters Support 3
Joe Caverly Using TYPE with non-English text Support 22
L Using TCC.exe through an SSH connection Support 3
Joe Caverly Using @PSHELL from @EVAL Support 8
Jay Sage Command Will Not Run Using Short Name of Path Support 5
Jay Sage Cannot Postion Cursor in Command Line Using Mouse Support 7
MikeBaas Using the debugger Support 2
Joe Caverly Multiple Text Searches at once using FFIND or TPIPE Support 4
Dick Johnson Using the @instr function Support 5
Joe Caverly Using this CMD technique from TCC Support 17
Joe Caverly What version and Windows OS are you using? Support 3
Craig Fitzgerald Problems using tcstart.btm Support 3
R How to? Append files in multiple subfolders using copy? Support 8
M Ctrl-C when using command line history does nothing Support 2
Joe Caverly Using a Directory Alias with @iniwrite fails Support 14
C French accents using msgbox Support 4
C "Failed to update the system registry. Please try using REGEDIT" Support 3
x13 Problem listing repository files using DIR http(s)://... Support 8
R Regex using ^ Support 2
cxxl WAD Mouse movement sluggish when using TCC list Support 3
jfalch WAD internal "which" should check for FILEEXIST when using AppPaths entry Support 8
D How to? Scale font in TCC using wheel mouse or other means Support 2
fpefpe Documentation copy/move using /c or /u Support 2
gschizas Fixed Using codepage 65001 (UTF-8) breaks non-ASCII characters Support 8
D How to? Use typed envars using regex. Support 3
epement Using "everything" Support 10
U Installing the Compaq Visual Fortran 6.0 compiler using Take Command Support 8
Alexander WAD The number of files and dirs are multiplied when using multiple wildcards Support 6
T using bottom line for status bar Support 3
A How to? How do you launch TCC/LE using the already-open instance? Support 5
rps Fixed Using last argument variable ! Support 5
M Incorrect ARGV1 when using linux-style paths Support 4
D How to? Using a custom ini file with TCC from Take Command Support 7
thedave WAD Unable to type various characters using Windows 8's onscreen keyboard Support 7
D Documentation Using "send all input to other tabs" Support 11
Joe Caverly Word Count using TPIPE Support 10
Phileosophos Fixed Option Dialog Hangs when Using Help Support 7
MickeyF problem using COM object in VBScript from v16 x64 TCC but not from v15 32-bit TCC Support 4
S How to? How do I distinguish missing source file from download error when using HTTP? Support 6
Jay Sage Using EVERYTHING Command in TCMD15 Support 5
Charles Dye Odd behavior after using OPTION "Advanced" tab Support 2
T How to? Using FOLDERMONITOR 1 event causes multiple trigger(ing)s Support 8
samintz WAD FILEWRITE using SMOPEN Support 1

Similar threads