Jump to content

[C++] Unicode functions


Recommended Posts

Hi all,

I wrote some c++ code to do some user management using the netapi32 library. I use certain functions such as NetLocalGroupAddMembers that requires the groupname to be a LPCWSTR. The variable I have for the groupname is a TCHAR and I need to convert it to a WCHAR* at runtime but am having some difficulty with it.

I was trying to use the MultiByteToWideChar function to get the required size, allocate the appropriate memory and then convert the groupname (group) to a WCHAR* (wGroup). But when calling MultiByteToWideChar the second time during execution, I get an application error. 'The instruction at "0x77f536f7" referenced memory at "0x5c740000". The memory could not be "written".

Below is the code in which I try to do this. Can anyone help me to get this working? Thanks.

iReqSize = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (char*)group, -1, wGroup, 0);
if ( (wGroup = (WCHAR*)LocalAlloc(LPTR, iReqSize)) == NULL)
{
_tprintf(_T("\nUnable to add user to administrators group."));
if (bDebug) _tprintf(_T("..Error allocating memory for group name"));
LocalFree(wUser); wUser = NULL;
return ERROR_SET_GROUP;
}
if ( (MultiByteToWideChar(CP_ACP, 0, (char*)group, -1, wGroup, iReqSize)) == 0)
{
_tprintf(_T("\nUnable to add user to administrators group."));
if (bDebug) _tprintf(_T("..Error converting group name to unicode: %d"), GetLastError());
LocalFree(wUser); wUser = NULL;
LocalFree(wGroup); wGroup = NULL;
return ERROR_SET_GROUP;
}
NetLocalGroupAddMembers(NULL, wGroup, 3, (LPBYTE) &luser, 1);

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...