Urgent please... convert TCHAR ot LPWSTR.. How to???



 Hi.. im confused..
 im trying to use NetUserGetInfo() function...
 this function need Server name and User name in unicode format LPWSTR which is 16bit var..
 i get the user name by GetUserName into a TCHAR[40]. and its working..
 when im trying to convert this var to LPWSTR i get an error that sais:
 "'strcpy' : cannot convert parameter 1 from 'unsigned short *' to 'char *'"

 this is my code:

 TCHAR Name[40];
 LPBYTE Buf ;
 LPWSTR Server=NULL,wName=NULL;
 strcpy(wName,Name);//this is obviously wrong what should it do insted..??
 DWORD sz=sizeof(Name);  
 GetUserName(Name,&sz);
 NetUserGetInfo(Server,wName,1,&Buf);
 NetApiBufferFree(Buf);



 how in the name of all gods do i convert this ???
 many thanks...

 confused kishk91
kishk91Asked:
Who is Participating?
 
abdijConnect With a Mentor Commented:
Try using MultiByteToWideChar() to get Unicode string from Ascii string.
Use WideCharToMultiByte() to get LPSRT from LPWSTR.
I think the latter function is what you need to use.
0
 
kishk91Author Commented:
Edited text of question.
0
 
bhatCommented:
Hi
use  _tcscpy(TCHAR*,TCHAR*) function.




0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
abdijCommented:
Hi,

Sorry for the overlook.
If you need unicode to unicode use wcscpy() function.
Abdij
0
 
abdijCommented:
By the way i tried and found both mine and bhats suggestions wont work.

Real sorry for the clumsy work.

By the way why do you need TCHAR at all.

In my place i tried and it worked this way.

LPTSTR Name = new char[40];
       LPBYTE Buf ;
       LPWSTR Server=NULL,wName=NULL;
       int      nNumBytesNeeded;
       BOOL      bRes;

       Server = new wchar_t[100];

       DWORD sz=sizeof(Name);
      
       bRes = GetUserName(Name,&sz);
       if(!bRes)
       {
             delete [] Name;
            Name = new char[sz];
            GetUserName(Name,&sz);
       }

       nNumBytesNeeded = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Name, sz, Server,100);
      
      NetUserGetInfo(Server,wName,1,&Buf);
      NetApiBufferFree(Buf);
      
But let me remind you the way U are using the NetUserGetInfo is wrong.

Bye for now.
Extreme Sorry.
Feel free to ask
abdij_b@hotmail.com
0
 
abdijCommented:
Please disregard the Memory leaks it can be fixed.
Bye
0
 
kishk91Author Commented:
Hi abdij... i thank you very much for your great help..
but i have lil problem after calling nNumBytesNeeded = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, Server,400);
int err=GetLastError();
GetLastError returns 122 which is " The data area passed to a system call is too small "
how do i fix it??

many thanks again
kishk91
0
 
abdijCommented:
Hi Man,

Try increasing the size of server from 40 to 100 or greater.

The other and better way around is :

First call nNumBytesNeeded  = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, Server,0); // note the 0 as last param

now delete [] Server and Create Server = new wchar_t[nNumBytesNeeded];

and then call
nNumBytesNeeded  = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, Server,nNumBytesNeeded)

Try both and suit the best.

All the best
Bye for now.
ABdij
0
 
kishk91Author Commented:
Hi again.. i just noticed.. you example trying to copy from Server to Name.. but the server is emtpy all the way...
my problem started when i tried to copy from Name to wName...
thanks
0
 
hariksCommented:
const UINT STR_SIZE      = 40;

void SomeClass::CopyArrayToPointer()
{
TCHAR      Name[STR_SIZE]= _T("Hi There");
LPWSTR      wName            = NULL;

wName = new TCHAR[STR_SIZE];
::ZeroMemory(wName, STR_SIZE);

_tcscpy(wName, Name); //Copied
}
0
 
hariksCommented:
hi Kishk91,
 The following Routine will copy the
 Unicode Array to Unicode Pointer
 variable.

const UINT STR_SIZE = 40;

void SomeClass::CopyArrayToPointer()
{
TCHAR Name[STR_SIZE]= _T("Hi There");
LPWSTR wName = NULL;

wName = new TCHAR[STR_SIZE];
::ZeroMemory(wName, STR_SIZE);

_tcscpy(wName, Name); //Copied
}

bye for now
hariks
0
 
abdijCommented:
Hi ,

 Sorry for being able to reply you earlier.

Regarding :

>> you example trying to copy from Server to Name
No I first get the Name in GetUserName() function in LPSTR and then convert it into the LPWSTR Server using the MultiByteToWideChar() function. Only after this function the server will have data. Ofcourse if you try to quick watch it will only give the Ascii value of the first character of the Name (Eg 65 if the Name started with A).

As regards to copying Name to wName use the Same MultiByteToWideChar() function similar to how i have used to acheive this.

But actually this is probably what you need for the NetGetUserInfo()
Param1: Computer Domain (Documentation says NULL for Local Server but it crashes)
Param2: The UserName
Param3: The Level
Param4: The Buffer to get Data

In your example you were inserting the Name instaed of the Domain. I had told that earlier in my comment.

Please recheck the function usage in MSDN.

So what i presume is you need two LPSTRs and two LPWSTRs. One for Domain and one for User name each.
Once you get the domain and user info in LPSTR convert to LPWSTR using MultiByteToWideChar()

Hope this helps

Bye
0
 
kishk91Author Commented:
Hi again.. im sorry for beong so stupid...
please take a look at this code..

      CString  error ;
      NET_API_STATUS  nasStatus ;
      USER_INFO_1 uiInfo ;
      DWORD Status=0x0001 ;
      LPTSTR Name = new char[UNLEN+1];
      LPBYTE *Buf=NULL ;
      LPWSTR Server=NULL,wName=NULL;       
      BOOL bRes;
      int nNumBytesNeeded;
      
      Server = new wchar_t[100];
      wName=new wchar_t[100];

      DWORD sz=sizeof(Name);
      bRes = GetUserName(Name,&sz);
 
      if(!bRes)                                  
      {                                    
            delete [] Name;                                    
            Name = new char[sz];                                    
            GetUserName(Name,&sz);                                    
      }      
                                          
      nNumBytesNeeded = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, wName,100);
      int err=GetLastError();//Here i get error 122 buffer size too small???
          
      
      nasStatus=NetUserGetInfo(NULL,wName,Status,Buf);//How do i use this func?? Buf shuld point to uiInfo?? how??                                           
      NetApiBufferFree(Buf);
      
      delete[]Name ;
      delete[]Server ;
      delete[]wName ;

i  appreciate your help.. many thanks....
Best regards
kishk91
0
 
kishk91Author Commented:
Adjusted points to 150
0
 
abdijCommented:
Instead of
nNumBytesNeeded = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, wName,100);

do

nNumBytesNeeded  = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, Server,0); // note the 0 as last param

delete [] Server
Server = new wchar_t[nNumBytesNeeded];

nNumBytesNeeded  = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, Name, sz, Server,nNumBytesNeeded)

The error "buffer too small will not occur)

As regards to the other question
Using NetUserGetInfo();
I have the code with me.
       LPTSTR                  szaName                  = NULL;
       LPTSTR                  szaComputerName      = NULL;
       LPWSTR                  szwName                  = NULL;
       LPWSTR                  szwComputerName      = NULL;
       LPUSER_INFO_10 pBuf                  = NULL;
       int                  nNumBytesNeeded      = 0;
        BOOL                  bRes                  = FALSE;
       DWORD                  dwaNameSize            = 0;
       DWORD                  dwwNameSize            = 0;
       DWORD                  dwaCompNmSize      = 0;
       DWORD                  dwwCompNmSize      = 0;
       DWORD                  dwLevel                  = 10;
       NET_API_STATUS nStatus;

       szwName                  = new wchar_t[100];
       szwComputerName      = new wchar_t[100];      

       szaName                  = new char[40];
       szaComputerName      = new char[40];

       dwaNameSize            = 40;
       dwaCompNmSize            = 100;
 

       bRes = GetUserName(szaName,&dwaNameSize);
       if(!bRes)
       {
             delete [] szaName;
            szaName = new char[dwaNameSize];
            GetUserName(szaName,&dwaNameSize);
       }

       // this is extra if you ever want the computer name
       bRes = GetComputerName(szaComputerName,&dwaCompNmSize);
       if(!bRes)
       {
             delete [] szaComputerName;
            szaComputerName = new char[dwaCompNmSize];
            GetComputerName(szaComputerName,&dwaCompNmSize);
       }

       nNumBytesNeeded = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szaName, dwaNameSize, szwName,0);
       nNumBytesNeeded = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szaName, dwaNameSize, szwName,nNumBytesNeeded);
       nNumBytesNeeded = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szaComputerName, dwaCompNmSize, szwComputerName,0);
       nNumBytesNeeded = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szaComputerName, dwaCompNmSize, szwComputerName,nNumBytesNeeded );
      
      nStatus = NetUserGetInfo((LPCWSTR)NULL,szwName,dwLevel,(LPBYTE*)&pBuf);
      if(nStatus == NERR_Success)
      {
            if(pBuf != NULL)
            {
                  // you get data;
                  AfxMessageBox("Success");
                  /*
                  You get the data for further needs refer MSDN
                  I have used user level 10
                  pBuf->usri10_name;
                  pBuf->usri10_comment;
                  pBuf->usri10_usr_comment;
                  pBuf->usri10_full_name;
                  */
     
            }
      }
      else
      {
            // handle error
            AfxMessageBox("Failure");
      }

      if(pBuf != NULL)
            NetApiBufferFree(pBuf);
      

It depends of what user level you want.
I have take 10
Yours need may be 1
change code accordingly

Happy Coding
Bye
Thanx
0
 
abdijCommented:
Hi again,

Wow what an awful mess i created.
I have defeated the purpose of having the 4 multibytetowidechar() functions

Here is how to do it
nNumBytesNeeded = MultiByteToWideChar(CP_ACP,
                     MB_PRECOMPOSED, szaName, dwaNameSize, szwName,0);
delete [] szwName;
szwName = new wchar_t[ nNumBytesNeeded];
                     nNumBytesNeeded = MultiByteToWideChar(CP_ACP,
                     MB_PRECOMPOSED, szaName, dwaNameSize,
                     szwName,nNumBytesNeeded);
                     nNumBytesNeeded = MultiByteToWideChar(CP_ACP,
                     MB_PRECOMPOSED, szaComputerName, dwaCompNmSize,
                     szwComputerName,0);
delete [] szwComputerName;
szwComputerName = new wchar_t[ nNumBytesNeeded];
                     nNumBytesNeeded = MultiByteToWideChar(CP_ACP,
                     MB_PRECOMPOSED, szaComputerName, dwaCompNmSize,
                     szwComputerName,nNumBytesNeeded );

Bye for now.

All the best.
Abdij
0
 
kishk91Author Commented:
Hi man...
How can i thank you for your help..???
i appreciate it very much..
many thanks...
just one more question...
when using the NetUsetGetInfo() and passing NULL to the SERVER param it will retrieve the info the LOCAL LOGED user..
when passing server name it will retrieve the user account inof that on this server..
How do i know if the user loged on localy or to the network??
Happy new year there...
kishk91
0
 
abdijCommented:
Hi,
 The user name that you got was for the Local machine. For the remote machine use have to know the user name (you cannot get it from the GetUserName() unless you execute this on the remote machine.) So have to know thw name of the acoount for which you are searching the info on. So if you donot know then you are barging in on some private info. Not allowed. (Try maybe Administrator or Admin or Guest these are some of the common login names)

All the Best.
Thanx
Bye
ABdij
0
 
kishk91Author Commented:
Hi..
thanks again for your great help man...
Best regards
kishk91
0
 
abdijCommented:
Hi,
 No Probs
 Feel free to ask if any problems again
Bye
Abdij
0
 
kishk91Author Commented:
Hi again...
just wanna inform you , i dont know if you know this thing but i found today in msdn a very usefull macro to do thid conversion between LPWSTR and char* or LPTSTR ...

check the A2W macro.
it goes something like this:

#include <afxpriv.h>

Test(char *String)
{      
      USES_CONVERSION;
      LPWSTR lpwStr = A2W(String);  
}

I found it very use full..
i hope you will too...
Regards
kishk91
0
 
abdijCommented:
Hi,
 Thanx man. I did not know such a tool existed. Good Find.

Bye
Abdij
0
All Courses

From novice to tech pro — start learning today.