Jump to content

Understanding DllCall and data types


Recommended Posts

Hi !
I have difficulties to learn how to use WinAPI functions with DllCall.
 
I red the (very good) >Tutorial on DllCall() & DllStructs. I understand the tutorial, but it's hard for me to apply it myself.
For example, the function GetUserName (just an example, I know @username of course) :

BOOL WINAPI GetUserName(
  _Out_    LPTSTR lpBuffer,
  _Inout_  LPDWORD lpnSize
);

Parameters

lpBuffer [out]

    A pointer to the buffer to receive the user's logon name. If this buffer is not large enough to contain the entire user name, the function fails. A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.
lpnSize [in, out]

    On input, this variable specifies the size of the lpBuffer buffer, in TCHARs. On output, the variable receives the number of TCHARs copied to the buffer, including the terminating null character.

    If lpBuffer is too small, the function fails and GetLastError returns ERROR_INSUFFICIENT_BUFFER. This parameter receives the required buffer size, including the terminating null character.

For me, the first parameter (lpBuffer) sould be a STR type (from AutoIt helpfile in DllCall)

The second, a DWORD (I think), but the MSDN says it is the size of the lpBuffer, in TCHARs : what is it ?

In >this topic, I found a solution for GetUserName :

MsgBox(0, "", _GetUserName() )

Func _GetUserName()
    Local $tlpnSize = DllStructCreate("dword[255]")
    Local $aDLL = DllCall("Advapi32.dll", "int", "GetUserName", "str", "", "dword*", DllStructGetPtr($tlpnSize))
    If @error Then Return SetError(@error, 0, 0)
    Return $aDLL[1]
EndFunc

I don't understand :

 - why using "int" instead of "bool" for the first parameter (as said in the MSDN page)?

 - why the second parameter value is empty ?

 - does TCHAR is equals to dword[255], how to find this by myself ?

 - why the last parameter is not DllStructGetPtr (I thought the size of the lpBuffer buffer should have been defined by DllStructGetSize)

As you can see, I am a newbie for this, and I would like to understand more, but I don't know how ....

Can someone give me some explanations or links ??

Thanks in advance, and sorry for the blurred question...

Link to comment
Share on other sites

I would write the same function this way, the more right way:

Func _GetUserNameFunkey()
    Local $aDLL = DllCall("Advapi32.dll", "BOOL", "GetUserName", "str", "", "dword*", 255)
    If @error Then Return SetError(@error, 0, 0)
    Return $aDLL[1]
EndFunc

BOOL and int are both 32 bit integer, so it can be interchanged, but for understanding of the function use BOOL so that you know there are only two values returned, zero and non zero.

If you pass an empty string to the dll function then this happens lieke documentation says: a minimum of 65536 chars is allocated.

So the last parameter can be up to 65536 without any difference. But usernames are not that long ;)

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

Thanks for this explanation Funkey. I appreciate you help.

Now, can you explain me how to represent the EXTENDED_NAME_FORMAT parameter in GetUserNameEx function ?

I tried this, but it does not work at all... :

MsgBox(0, "", _GetUserNameEx() )

Func _GetUserNameEx()
    Local $aDLL = DllCall("Secur32.dll", "bool", "GetUserNameEx", "int", 2, "str", "", "ulong*", 255)
    If @error Then Return SetError(@error, 0, 0)
    Return $aDLL[1]
EndFunc
do not laugh, please ... :muttley:
Link to comment
Share on other sites

Allright JFX !

I begin to understand : $aDLL[1] contains the value of the first parameter (value=2), $aDLL[2] contains the value of the 2nd parameter and so on...
It is written in the helpfile, but I did not understand that.
Thanks, it's clearing up !
 
I will continue with other functions and come here to ask for help (I will need, sure)
Global Const $NameUnknown = 0, _
             $NameFullyQualifiedDN  = 1, _
             $NameSamCompatible     = 2, _
             $NameDisplay           = 3, _
             $NameUniqueId          = 6, _
             $NameCanonical         = 7, _
             $NameUserPrincipal     = 8, _
             $NameCanonicalEx       = 9, _
             $NameServicePrincipal  = 10, _
             $NameDnsDomain         = 12
             
MsgBox(0, "", _GetUserNameEx($NameDisplay) )



Func _GetUserNameEx ($NameFormat)
    $ret = DllCall("Secur32.dll", "bool", "GetUserNameEx", "int", $NameFormat , "str", "", "ulong*", 255)
    If @error Then Return SetError(1, 0, 0)
    
    Return $ret[2]
EndFunc
Link to comment
Share on other sites

Well, now, I tried to use CreateProfile, and... it's a success !

MsgBox(0, "", _WinAPI_CreateProfile("S-1-5-21-3114055946-370887941-3244374214-500", "administrator") )

Func _WinAPI_CreateProfile($sUserSid, $sUserName)
    Local $ret = DllCall("Userenv.dll", "long", "CreateProfile", "wstr", $sUserSid, "wstr", $sUserName, "wstr", "", "dword", 255)
    If @error Then Return SetError(1, 0, -1)
    
    Return $ret[3]
EndFunc

i still have difficulties to understand when I must use (or not) a wildcard after the datatype...

Link to comment
Share on other sites

Thanks trancexx.
Not familiar at all... I have never use C or C++...
It seems to be a reason to my difficulties.

I think there are a lot of members in this case.
What could you recommend to us ? Is it possible to learn how to use complex WinApi funtions with these limited knowledges?

Thanks for your patience

Edited by jguinch
Link to comment
Share on other sites

You just need to see Conversions from Windows API types to AutoIt types.

it says 

LPCWSTR/LPWSTR=wstr

or get the pointer (ptr*) to that unicodestring and supply that pointer to a structure (dllstructcreate("wchar",yourptr)).

 

PD: I know basic about C++/C

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Hi again !

I tried some other functions, and now I have a little question.

Here is my code for QueryFullProcessImageName (just for learning, I know _WinAPI_GetProcessFileName):

$h = _OpenProcess( @AutoitPid)
MsgBox(0, "", _QueryFullProcessImageName($h) )


Func _QueryFullProcessImageName($hProcess)
    $ret = DllCall("Kernel32.dll", "bool", "QueryFullProcessImageName", "handle", $hProcess, "dword", 0, "str", "", "dword*", 256)
    If @error Then Return SetError(1, 0, -1)
    
    Return $ret[3]
EndFunc


Func _OpenProcess($iProcessId)
    $ret = DllCall("Kernel32.dll", "HANDLE", "OpenProcess", "dword", 0x0400, "bool", True, "dword", $iProcessId)
    Return $ret[0]
EndFunc

As you can see in my QueryFullProcessImageName call, I set the last parameter to 256. But if the returned full path length is bigger than this value, the function fails.

So my question is : which value should I use for this parameter ? Something like 4096 or more ?

Thanks again. It''s a pleasure to learn with you ! :thumbsup:

 

Edit : Danyfirex, I do not really understand what you said...

Edited by jguinch
Link to comment
Share on other sites

If you use (ANSI) API  Use 256.

if you use (Unicode) API use  32767

what exactly you don't understand?

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Link to comment
Share on other sites

In this case you have to do the structure before because is not a pointer to constant(LPCSTR/LPCWSTR). and pass the pointer. look:

MsgBox(0, "", _WinAPI_CreateProfile("S-1-5-21-3114055946-370887941-3244374214-500", "Danyfirex") )


Func _WinAPI_CreateProfile($sUserSid, $sUserName)
Local $tPath=DllStructCreate("wchar[255]")
    Local $ret = DllCall("Userenv.dll", "long", "CreateProfile", "wstr", $sUserSid, "wstr", $sUserName, "ptr", DllStructGetPtr($tPath), "dword", 255)
    If @error Then Return SetError(1, 0, -1)
    Return DllStructGetData($tPath,1)
EndFunc

If it was a pointer to a constant you could use ptr* something like this:

;this code will not work (it's a way if the api returns a pointer, CreateProfile does not).

msdn say:

pszProfilePath [out]

When this function returns, contains a pointer to the full path of the profile.

But it's wrong it will need a wide string pointer to fill it.

MsgBox(0, "", _WinAPI_CreateProfile("S-1-5-21-3114055946-370887941-3244374214-521", "Danyfirex") )

Func _WinAPI_CreateProfile($sUserSid, $sUserName)
    Local $tPath=0
    Local $ret = DllCall("Userenv.dll", "long", "CreateProfile", "wstr", $sUserSid, "wstr", $sUserName, "ptr*", 0, "dword", 255)
    If @error Then Return SetError(1, 0, -1)
    $tPath=DllStructCreate("wchar[255]",$ret[3])
    Return DllStructGetData($tPath,1)
EndFunc

or simply use Conversions from Windows API types to AutoIt types

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

You exactly point out a difficulty for me : wchar[255] in a structure is equivalent to wstr ?

Sorry for these ridiculous questions....

Moderators, if you consider this topic is not in the good section, you can move it. The problem is that I have not just one question, but a lot of interrogations..

Thanks again Danyfirex.

Link to comment
Share on other sites

Basically yes.

wstr is a pointer to a sequence of Unicode characters with null termination( in this case for being a wide(unicode) string need to be double null termination)

wchar is a sequence of Unicode characters.

if you do a structure with wchar[n size] and pass its pointer is  same as you pass wstr.

Saludos

Link to comment
Share on other sites

wchar is the type of one UTF16 (-LE or BE) Unicode encoding unit. A Unicode character needs one or two encoding unit(s) to represent. Those Unicode characters requiring two units are seldom used and AFAIK no widespread font can display them.

So most practical applications restrict wchar to mean "one Unicode character", which implies that only those in BMP (or plane 0 or base Multilingual plane) can be represented. This restricted character set is (roughly) called UCS-2 and it's what AutoIt uses.

wchar is to wstr what char is to str. [w]str is a pointer to the base address of a C [w]char array (C doesn't have a built-in string type).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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