How to use type LPCWSTR in masm

589 Views Asked by At

I am new to the msdn library and masm.

After invoking NetUserAdd, I am trying to invoke NetLocalGroupAddMembers, but it requires type LPCWSTR for the group name. I think that is why my account I create is not visible and still does not appear after invoking NetLocalGroupAddMembers.

(I import a lot of files because my code is not complete and I know what I will be adding.)

Here is my code:

.386
.model flat,stdcall 
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\user32.inc 
INCLUDE \masm32\include\msvcrt.inc
include \masm32\include\kernel32.inc 
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\msvcrt.lib
include \masm32\include\netapi32.inc
INCLUDELIB \masm32\lib\netapi32.lib


.data

lmi3 LOCALGROUP_MEMBERS_INFO_3 <>

domain byte "WIN-XXX\NAME",0

;conversion of domain to unicode
domainName BYTE 500 DUP(?)

; argument for NetUserAdd
error byte 50 dup(?)

; user name and password for new user
userPassword byte "NAME",0
userName byte "PWD",0

; will contain conversion of userPassword and userName to unicode
user byte 500 dup(?)
pwd byte 500 dup(?)

;structure declaration fo NetAddUser
account USER_INFO_1 <>

groupName BYTE "Users",0

; where I would place conversion of groupName
uniGroupName byte 200 DUP(?)

.code

main PROC

    ; convert to lpwstr (unicode)
    invoke MultiByteToWideChar, CP_ACP, NULL, addr userName, -1, addr user, 500
    invoke MultiByteToWideChar, CP_ACP, NULL, addr userPassword, -1, addr pwd, 500

    mov account.usri1_name, offset user
    mov account.usri1_password, offset pwd
    ; user access has to be User_Priv_User to call NetAddUser
    mov account.usri1_priv, USER_PRIV_USER
    mov account.usri1_home_dir, NULL
    mov account.usri1_comment, NULL
    mov account.usri1_flags, UF_SCRIPT 
    mov account.usri1_script_path, NULL

    ; 1 indicates user_info_1
    invoke NetUserAdd, NULL, 1, addr account, offset error

    ; conversion to lpwstr
    invoke MultiByteToWideChar, CP_ACP, NULL, addr domain, -1, addr domainName, 500

    mov lmi3.lgrmi3_domainandname, offset domainAndName

    ; uniGroupName needs to be in LPCWSTR
    invoke NetLocalGroupAddMembers,NULL,addr uniGroupName,3, offset lmi3,1

    invoke ExitProcess,0 

; end of procedure
main ENDP


; end of program
END main
1

There are 1 best solutions below

0
On

You could either declare your group name string as DW 'U','s','e','r','s',0. Or if you'd rather do the conversion at runtime you can use the crt_mbstowcs function from msvcrt.

Here's a small example program that tests both methods:

.686p
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\msvcrt.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\msvcrt.lib

.data

; %s for ASCII, %S for wide strings
format db "original: %s, converted: %S",13,10,0

; An ASCII string that we'll convert at runtime
original db "Foobar",0
converted dw 32 dup(0)

; A statically created wide string    
testws dw 'T','e','s','t',13,10,0

.code

main PROC

; Print our statically created wide string
invoke crt_wprintf, ADDR testws

; Converts the characters in 'original' into wide characters and stores them
; in 'converted'.
invoke lstrlen,ADDR original
invoke crt_mbstowcs, ADDR converted, ADDR original, eax

; Print both the original and converted string, then exit the program.
invoke crt_printf, ADDR format, ADDR original, ADDR converted
invoke ExitProcess, 0

main ENDP

END main