Non NULL terminated PWCHAR

  • Thread starter Thread starter B Vidyadhar Joshi
  • Start date Start date
B

B Vidyadhar Joshi

I have a function which takes a NOT NULL terminated PWCHAR.

int myFunction (PWCHAR pszKey, ULONG ulKeyLen);

I have to pass a 4 charecter value to the "key" parameter. For example,
"1111". How do I do it?

TIA.

Regards,

B Vidyadhar Joshi
 
B said:
I have a function which takes a NOT NULL terminated PWCHAR.

int myFunction (PWCHAR pszKey, ULONG ulKeyLen);

I have to pass a 4 charecter value to the "key" parameter. For example,
"1111". How do I do it?

Everything depends on what you want to do (it's not clear from your
post). My best guess is this:

int myFunction (PWCHAR pszKey, ULONG ulKeyLen)
{
if ( ulKeyLen >= 4 )
return MultiByteToWideChar(CP_ACP, 0, "1111", 4, pszKey, ulKeyLen);
else
return 4;
}
 
Actually, the function myFunction(PWCHAR pszKey, ULONG ulKeyLen) is exposed
by some custom 3rd party DLL. I have to pass these parameters to the DLL
where pszKey is a NON NULL terminated string and ulKeyLen is length of
pszKey in bytes. The pszKey parameter is usually a 4 charecters string like
"1234".

When I use MultiByteToWideChar(CP_ACP, 0, "1111", 4, pszKey, ulKeyLen), it
returns 0 but the pszKey is <undefined value>. When I call GetLastError, the
error number returned is ERROR_INVALID_PARAMETER.

Here is the code I use:

LPCSTR szKey = "1111";
int cbLen = sizeof(szKey); // == 4
PWCHAR pszKey;
ULONG ulKeyLen = sizeof(pszKey); // == 4;

int ret = MultiByteToWideChar(CP_ACP, 0, "1111", 4, pszKey, ulKeyLen);
//returns 0 but pszKey == <undefined value>
UINT err = GetLastError(); //returns ERROR_INVALID_PARAMETER (87)

//Call to myFunction() goes here...


Please help.

B Vidyadhar Joshi.
 
OK - A small mistak while posting. The code goes as follows:

LPCSTR szKey = "1111";
int cbLen = sizeof(szKey); // == 4
LPWSTR pszKey;
int ulKeyLen = sizeof(pszKey); // == 4;

int ret = MultiByteToWideChar(CP_ACP, 0, "1111", cbLen, pszKey, ulKeyLen);
//returns 0 but pszKey == <undefined value>
UINT err = GetLastError(); //returns ERROR_INVALID_PARAMETER (87)

//Call to myFunction() goes here...

TIA.

B Vidyadhar Joshi
 
myFunction(L"1111", 4);


L for 'wide' string literal.


The fact that you must pass the key length suggests that null-terminated
or not is irrelevant. no?
 
This doesn't work either :-(

This time myFunction returns ERROR_CONTROL_ID_NOT_FOUND.
 
Back
Top