Solved

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

Posted on 2002-04-22
19
479 Views
Last Modified: 2011-10-03
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
Comment
Question by:chhavigarg
  • 8
  • 6
  • 2
  • +3
19 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6959406
Here is an excellent example of using NetUserGetInfo:

http://www.mvps.org/vbnet/code/network/netuserenum.htm
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6959408
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6959614
hearing...
0
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6961173
ping...
0
 

Author Comment

by:chhavigarg
ID: 6961534
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
 

Author Comment

by:chhavigarg
ID: 6962204
Hi Experts,
Is there anyone who can answer to my question?
If you want, I can increas the point.
Thanks
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6962686
Why? tim's suggestion doesn't works?
0
 

Author Comment

by:chhavigarg
ID: 6964528
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
 

Author Comment

by:chhavigarg
ID: 6967443
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6969213
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6969258
0
 

Author Comment

by:chhavigarg
ID: 6977236
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
 

Author Comment

by:chhavigarg
ID: 6982380
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6982499
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
 

Author Comment

by:chhavigarg
ID: 6984945
How should I post a question at community support to PAQ?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6985996
0
 

Author Comment

by:chhavigarg
ID: 7000693
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7660643
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
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7755361
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now