CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2005
    Posts
    20

    Unhappy _TCHAR Conversion

    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!

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    _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?

  3. #3
    Join Date
    Dec 2005
    Posts
    20

    Re: _TCHAR Conversion

    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.

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    If you aren't going to provide the information requested, then I'm not going to guess. Anyone else?

  5. #5
    Join Date
    Dec 2005
    Posts
    20

    Re: _TCHAR Conversion

    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.
    Attached Images Attached Images

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    _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.

  7. #7
    Join Date
    Dec 2005
    Posts
    20

    Unhappy Re: _TCHAR Conversion

    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

  8. #8
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    _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()...

  9. #9
    Join Date
    Dec 2005
    Posts
    20

    Re: _TCHAR Conversion

    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

  10. #10
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    Quote Originally Posted by agenore
    ...
    nr.lpLocalName = pDati.cDrive;
    ...
    res = WNetAddConnection2(&nr, sPwd, sUser, FALSE);

    It compile and generate but doesen't works. res -> 1200
    You 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.

  11. #11
    Join Date
    Dec 2005
    Posts
    20

    Unhappy Re: _TCHAR Conversion

    just a moment...try a workaround. Can you show me a little part of code which use WNetAddConnection2 with _TCHAR variables?
    Thanks

  12. #12
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    Here's the workaround that I was hinting at
    Code:
    // 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...

  13. #13
    Join Date
    Dec 2005
    Posts
    20

    Talking Re: _TCHAR Conversion

    Ok. Now the program works very fine. I don't understand _TCHAR conversion ... but I'll study that.
    Thank you very much.

  14. #14
    Join Date
    May 2002
    Posts
    1,435

    Re: _TCHAR Conversion

    Quote Originally Posted by agenore
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured