Re: UCHAR problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



You have not said if you are dealing wtih Unicode or ANSI apps.

Note that by stating UCHAR[32], you have said that you are using 8-bit unsigned
characters. Then you copy what is a Unicode string into it, which is a type violation.
You are using _tcsncpy, which will fail in ANSI mode to copy more than one character if
the string is in the base code page (thus being in the range U0000..U00FF). The _tcsncpy
call is incorrect as shown, because to avoid buffer overflow in Unicode, it must be

_tcsncpy((TCHAR *)Data.Ssid, mySSID, sizeof(Data.Ssid)/sizeof(TCHAR));

C# works in Unicode, and therefore passes in a Unicode string. Therefore, if you need an
8-bit version, you will have to use something like W2A, W2T, or the low-level
WideCharToMultiByte, to perform the conversion from Unicode to 8-bit characters consistent
with UCHAR.

Since you are not actually making conversions, but simply copying one kind of string to an
inconsistent alternative representation to one which is incompatible, I'm surprised that
anything worked at all.
joe

On 17 Feb 2006 03:40:01 -0800, "grubbymaster" <grubbymaster@xxxxxxxxx> wrote:

Hello

The problem i have is this :
i want to use the following structure in a DLL that i created:

typedef struct _NDIS_802_11_SSID {
ULONG SsidLength;
UCHAR Ssid [32];
} NDIS_802_11_SSID, *PNDIS_802_11_SSID;

and i want to take the UCHAR Ssid[32] field and copy in it a certain
variable.I've tried the following ways :

NDIS_802_11_SSID Data;

_tcsncpy((TCHAR *)Data.Ssid, mySSID, 32);
strncpy((char *)pData.m_SSID.Ssid, mySSID, 32);

if i display it like this it works : MessageBox(NULL, Data.Ssid, L"",
MB_OK); and it displays ok : "EXAMPLE".

If i pass this as a parameter to a function like this : void X
(NDIS_802_11_SSID Data) and call this function from C# the recieved
string is "E\0X\0A\0\M\0P\0\L\0E\0".We solved this problem also by
using a for loop like this for (j=0; j<32 ; j=j+2) but if we have a
larger string that i want to put in UCHAR Ssid[32]; it will not retain
only 15chars because of the "\0" insertion after each char.I believe
that the problem is in the conversions i'm makeing above, and how i
store the data (with the TCHAR * and char * conversions). Is there a
function to copy from one UCHAR[32] to another UCHAR[32] without
conversions?

The mySSID parameter is taken as a parameter: void y(UCHAR mySSID[32])
,but in the body of the function it is seen as UCHAR[] and i can't use
this :

Data.Ssid = mySSID (error because UCHAR[32] <--->
UCHAR[])

That's it. Hope you can help me in some way.
Thank you.Bye
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages