Solved

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

Posted on 2002-04-22
19
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA to copy paste columns form one file to other 20 100
VBA Shell can't Find Word document 11 131
Modifying Conditional Format from VBA code 3 71
Spell Check in VB6 13 137
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

733 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