Solved

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

Posted on 2002-04-22
19
477 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
Comment Utility
Here is an excellent example of using NetUserGetInfo:

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

Expert Comment

by:TimCottee
Comment Utility
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
hearing...
0
 
LVL 3

Expert Comment

by:nzjonboy
Comment Utility
ping...
0
 

Author Comment

by:chhavigarg
Comment Utility
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
Comment Utility
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
Comment Utility
Why? tim's suggestion doesn't works?
0
 

Author Comment

by:chhavigarg
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
0
 

Author Comment

by:chhavigarg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How should I post a question at community support to PAQ?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 

Author Comment

by:chhavigarg
Comment Utility
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
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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