Click to See Complete Forum and Search --> : _TCHAR Conversion


agenore
July 29th, 2008, 10:49 AM
Hi, I have a NETRESOURCE (nr) and a _TCHAR (drive) variables. How I Can assign drive to nr.lpLocalName? I did nr.lpLocalName = (LPWSTR)drive .... It compile ... but it doesen't work!

0xC0000005
July 29th, 2008, 12:46 PM
_TCHAR (drive) doesn't make sense. Show exactly how drive is defined. In fact, show your code where it's defined and used. If it's defined correctly you don't need to cast and you shouldn't be using an explicit 'W' type when using TCHAR anyway. Also define 'doesn't work'. Is there an error when you use NETRESOURCE? What is it?

agenore
July 30th, 2008, 07:21 AM
The code compiling without error, the generated code too, simply when I use NETRESOURCE into WebAddConnection2, this function return an error because if I watch the nr.lpLocalName by debug It's '..invalid pointer...' or like this.

0xC0000005
July 30th, 2008, 08:24 AM
If you aren't going to provide the information requested, then I'm not going to guess. Anyone else?

agenore
July 30th, 2008, 09:10 AM
Ok try alternative way. I created a struct:
struct Dati {
int iOpe;
_TCHAR cDrive;
Parametri* strPar;
};

the part of code is :
....

nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = (LPWSTR)(pDati.cDrive);
nr.lpRemoteName = sPath;

the debug information is on attach files.

0xC0000005
July 30th, 2008, 09:41 AM
_TCHAR cDrive is not a valid way of declaring a string that will be required for NETRESOURCE. It must be a TCHAR *cDrive, or an actual TCHAR array TCHAR cDrive[MAX_DRIVE];

Once it is defined this way then you need to set it be giving it a pointer to the actual name (case 1) or copying the actual name into the array (case 2).

After you have modified the Dati structure then remove the LPWSTR cast when assigning cDrive to nr.lpLocalName. It won't be needed and if you get a compiler error then you are still doing something wrong.

Since cDrive is currently a TCHAR rather than TCHAR * i assume that you are currently assigning it like this:

cDrive = 'G';

If so, that will have to be changed to:

cDrive = _T("G");

or

_tcscpy(cDrive, _T("G"));

I am only guessing that you are using 'G' since that's what your debugger appears to show.

If you prefer to leave cDrive as TCHAR then there are some alternative methods of dealing with it. Please post again if you want suggestions.

Note: MAX_DRIVE is undefined since I don't know what you plan to put in cDrive. If you declare cDrive as an array you will have to give it a max size based on how you plan to use it.

agenore
July 30th, 2008, 10:54 AM
Ok. The pointer problem seem done. But the function doesn't work again.
You use _TCHAR and TCHAR in your answer...It's equal?
There is a way to use VS2005 with the old mode? I don't understand how _TCHAR works

0xC0000005
July 30th, 2008, 11:34 AM
_TCHAR and TCHAR appear to be identical. To the best of my understanding it appears that _TCHAR with language rules regarding reserved names and leading underscores. Apparently they will both be defined and equal if you have Microsoft language extensions enabled.

Once again, you have to explain "the function doesn't work." What function? WebAddConnection2()? I can't find any information about it. You must look at the documentation for this function and determine how to detect errors. Does it return a bool or an error code? If error code, what is it? Does the documentation say what to do when an error is encountered? GetLastError()...

agenore
July 30th, 2008, 11:43 AM
the code is:
.....

_TCHAR sPwd[MAX_PATH];
_TCHAR sUser[MAX_PATH];

_tcscpy_s(sPwd, pDati.strPar->pwd);
_tcscpy_s(sUser, pDati.strPar->user);
_tcscpy_s(sPath, _T("\\\\"));
_tcscat_s(sPath, pDati.strPar->host);
_tcscat_s(sPath, _T("\\"));
_tcscat_s(sPath, pDati.strPar->work_dir);
nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = pDati.cDrive;
nr.lpRemoteName = sPath;
nr.lpProvider = NULL;
res = WNetAddConnection2(&nr, sPwd, sUser, FALSE);

It compile and generate but doesen't works. res -> 1200

0xC0000005
July 30th, 2008, 12:26 PM
...
nr.lpLocalName = pDati.cDrive;
...
res = WNetAddConnection2(&nr, sPwd, sUser, FALSE);

It compile and generate but doesen't works. res -> 1200You should investigate how to read the MSDN (or VisualStudio Help) documentation and determine the reason API calls fail:

res = 1200 = ERROR_BAD_DEVICE = The value specified by lpLocalName is invalid.

i.e. pDati.cDrive is invalid.

I don't know what you have copied to pData.cDrive so I don't know how to tell you what's wrong - and I'm not sure I could because I don't know what the function is expecting. I would guess something line _T("G:") for the G: drive - but I don't know. Perhaps if you showed us exactly what is in cDrive.

agenore
July 30th, 2008, 04:19 PM
just a moment...try a workaround. Can you show me a little part of code which use WNetAddConnection2 with _TCHAR variables?
Thanks

0xC0000005
July 30th, 2008, 05:27 PM
Here's the workaround that I was hinting at

// cDrive requires a single TCHAR so:
pDati.cDrive = 'G';

// nr requires TCHAR array so create a temporary array
TCHAR drive[3];

// Now copy 'G' to the temporary array (and add a colon)
_stprintf(drive, _T("%c:"), pData.cDrive);

// Next assign the temporary array to nr.cDrive (lpLocalName=_T("G:"))
nr.lpLocalName = drive;


The local drive name is limited to being defined by a single character since you are imposing that limitation. All I have done is converted TCHAR to TCHAR * as required by nr and assumed that a colon is needed too.

I would not recommend this method because of the limitation mentioned. But if all you need if for is single letter drives...

agenore
July 31st, 2008, 09:36 AM
Ok. Now the program works very fine. I don't understand _TCHAR conversion ... but I'll study that.
Thank you very much.

0xC0000005
July 31st, 2008, 09:45 AM
Ok. Now the program works very fine. I don't understand _TCHAR conversion ... but I'll study that.
Thank you very much.It's not just you. The relationship between a single character, an array of characters and a character pointer is very basic C++ and it always seems to cause difficulties for those just learning the language.