[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

How to do an external DLL which has the LPWSTR parameter from VB?

I'm planning to create a program that uses the NetUserSetInfo() API from Visual Basic to change password.
Since I would like to call the NetUserGetInfo() API as well as the NetUserSetInfo() API, I consider of the way of calling the API that has LPWSTR as the structure member. As you know, when a String parameter (BSTR) is passed to the DLL that is delcared with "Declare", VB will automatically convert the parameter into ANSI. However, I don't want such conversion. In order to pass strings to LPWSTR without code conversion, how should I assign the strings to the Long typed variable (see "user_pwd_l" below). Also, in order to get the LPWSTR string assigned to the Long variable, how should I do?
Please tell me with an explaination and code example?
Here is the sample normal way to do it.
[http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;Q187535&LN=EN]

' USER_INFO_1003 Structure
' typedef struct _USER_INFO_1003 {
'   LPWSTR    usri1003_password;
' } USER_INFO_1003, *PUSER_INFO_1003, *LPUSER_INFO_1003;

Private Type M_USER_1003
   user_pwd_l As Long
End Type

' Get the information about the given user account
Private Declare Function NetUserGetInfo Lib "netapi32.dll" _
   (servername As Any, _
    username As Any, _
    ByVal level As Long, _
    bufptr As Long) As Long

' Set the information about the given user account
Private Declare Function NetUserSetInfo Lib "netapi32.dll" _
   (servername As Any, _
    username As Any, _
    ByVal level As Long, _
    bufptr As Long, _
    parm_err As Long) As Long
0
chhavigarg
Asked:
chhavigarg
  • 8
  • 6
  • 2
  • +3
1 Solution
 
TimCotteeCommented:
Here is an excellent example of using NetUserGetInfo:

http://www.mvps.org/vbnet/code/network/netuserenum.htm
0
 
TimCotteeCommented:
0
 
Richie_SimonettiIT OperationsCommented:
hearing...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
nzjonboyCommented:
ping...
0
 
chhavigargAuthor Commented:
Hi All, I'm looking for an explaination, How can I do it?
But seems to me that there is no explaination for this.
hmm.....
0
 
chhavigargAuthor Commented:
Hi Experts,
Is there anyone who can answer to my question?
If you want, I can increas the point.
Thanks
0
 
Richie_SimonettiIT OperationsCommented:
Why? tim's suggestion doesn't works?
0
 
chhavigargAuthor Commented:
coz I was not able to get LPWSTR user_pwd_l string assigned to the Long variable.
According to MSDN that there is no way to get.
It always return null.
 
0
 
chhavigargAuthor Commented:
I'm goint to delete this question.
As there no one able to answer to my question.
Anyway thanks a lot.
Tim, If you think that you deserve point for this question.
Let me know, I'll give it to u.
0
 
Richie_SimonettiIT OperationsCommented:
if i understood, that conversion is wrong:

' USER_INFO_1003 Structure
' typedef struct _USER_INFO_1003 {
'   LPWSTR    usri1003_password;
' } USER_INFO_1003, *PUSER_INFO_1003, *LPUSER_INFO_1003;

Private Type M_USER_1003
  user_pwd_l As Long
End Type

is LPWSTR  data type long?
See this (unicode string):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/ntlmapi3_51bm.asp
0
 
Richie_SimonettiIT OperationsCommented:
0
 
chhavigargAuthor Commented:
I have something which is not clear to me:


It appears that my explanation in the question is not sufficient. Let me explain again.

To change password, I use the USER_INFO_1003 structure to call NetUserSetInfo() alone. I don't use NetUserChangePassword().

Therefore, I need to assign the Unicode string to the variable which is defined as user_pwd_l As Long. According to the Q187535 information, it suggests StrPrt (String variable name) can be used, but my customer is not interested in using this StrPrt functions.

Question1: In order to pass a String typed string to LPWSTR which is defined as Long and is a structure member without the code conversion, how should I assign it?

Question2: I think the call processing to ChangeByteArray() in this function, may be equal to converting Unicode string from the string type to a byte typed array that contains the Unicode string.
Private Function ChangeByteArray(ByVal Ustr As String) As Byte()
    Dim Buff() As Byte
    Buff = Ustr
    ReDim Preserve Buff(Len(Ustr) * 2 + 1)
    ChangeByteArray = Buff
End Function

Question3: Can I understand ReDim Preserve Buff(Len(Ustr)*2 + 1) will correctly append Unicode NULL (0x00,0x00)?

Question4 ) Can I understand that the coding similar to below can create Unicode string properly?

   Dim sServer As String
   Dim sNew As String
@ sServer = "ServerName"
   sNew = StrConv(sServer, vbUnicode) + vbNullChar
0
 
chhavigargAuthor Commented:
Hi! Guys.
Somehow, I able to get it done using the NetAPIBufferAllocate. Please let me know, If any need a points for this question. Or should I delete this question?Thanks

*********::::
Private Declare Function NetAPIBufferAllocate Lib "NETAPI32.DLL" Alias "NetApiBufferAllocate" _
 (ByVal ByteCount As Long, Ptr As Long) As Long

Private Declare Function StrToPtr Lib "Kernel32" Alias "lstrcpyW" (ByVal Ptr As Long, Source As Byte) As Long
This below line of code is equal to StrPtr function in VB.

    Dim passArray() As Byte
   
 passArray = SPassword & vbNullChar
 result = NetAPIBufferAllocate(UBound(passArray) + 1, ptrNewPass)
result = StrToPtr(ptrNewPass, passArray(0))
0
 
Richie_SimonettiIT OperationsCommented:
If you will not award points (which is good to me), you could post a question at community support to PAQ it instead of delete it.
0
 
chhavigargAuthor Commented:
How should I post a question at community support to PAQ?
0
 
Richie_SimonettiIT OperationsCommented:
0
 
chhavigargAuthor Commented:
one more comments. I have just seen the link which u introduced me. YOu introduced me the sdk link *lol* I wrote this API declaration with PLATFORM SDK.
another link is no at all related to this issue.
As u know that I resolved this issue myself.
0
 
DanRollinsCommented:
chhavigarg, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Refund points and save as a 0-pt PAQ.

DanRollins -- EE database cleanup volunteer
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 8
  • 6
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now