CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2016
    Posts
    3

    WNetAddConnection2 returns error 1200

    I have shared network folder on my disk C:\folder.

    When I use WNetAddConnection2 I get error 1200. My code is:
    Code:
    DWORD dwResult;
    NETRESOURCE nr;
    nr.dwType = RESOURCETYPE_ANY;
    nr.lpLocalName = (LPWSTR)"folder";
    nr.lpRemoteName = (LPWSTR)"\\\\ASYA\\folder";
    nr.lpProvider = NULL;
    dwResult = WNetAddConnection2(&nr,NULL,(LPCWSTR) "Nastya",CONNECT_UPDATE_PROFILE); 
    
    if (dwResult == NO_ERROR)
        wprintf(L"Connection added to %s\n", nr.lpRemoteName);
    else
        wprintf(L"WNetAddConnection2 failed with error: %u\n", dwResult);
    What is my mistake? How can I solve this problem?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: WNetAddConnection2 returns error 1200

    Are you compiling as Unicode?

    1200 means that the specified device name is invalid. Try (not tried)
    Code:
    nr.lpLocalName = L"folder";
    nr.lpRemoteName = L"\\\\ASYA\\folder";
    and similar for nastya
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: WNetAddConnection2 returns error 1200

    Quote Originally Posted by NastyaG View Post

    What is my mistake? How can I solve this problem?
    Your mistake is that you think that casting ANSI string to LPWSTR would miraculously turn it to Unicode string. But the trick just won't work, because what you do is making compiler believe the string is wide string. While it's realy not.

    Quite a standard mistake for beginner who does not understand C pointers.

    And yes, about how to solve... You either provide wide string literal (this is what 2kaud advised), or explicitly tranlate string to the required encoding. E.g. using CString class:

    Code:
    CString name = "folder";
    nr.lpLocalName = name.GetBuffer();
    Oh! And you'd better get to habit of zeroing structures you're not going to initialize in every member.
    Code:
    NETRESOURCE nr = {0};
    Just to avoid passing garbage.
    Last edited by Igor Vartanov; September 11th, 2016 at 02:45 PM.
    Best regards,
    Igor

  4. #4
    Join Date
    Sep 2016
    Posts
    3

    Re: WNetAddConnection2 returns error 1200

    Quote Originally Posted by Igor Vartanov View Post
    Your mistake is that you think that casting ANSI string to LPWSTR would miraculously turn it to Unicode string. But the trick just won't work, because what you do is making compiler believe the string is wide string. While it's realy not.

    Quite a standard mistake for beginner who does not understand C pointers.

    And yes, about how to solve... You either provide wide string literal (this is what 2kaud advised), or explicitly tranlate string to the required encoding. E.g. using CString class:

    Code:
    CString name = "folder";
    nr.lpLocalName = name.GetBuffer();
    Oh! And you'd better get to habit of zeroing structures you're not going to initialize in every member.
    Code:
    NETRESOURCE nr = {0};
    Just to avoid passing garbage.
    Yes, I changed this:

    Code:
    DWORD dwResult;
        NETRESOURCE nr;
        nr.dwType = RESOURCETYPE_ANY;
        nr.lpLocalName = L"K:";
        nr.lpRemoteName = L"\\\\ASYA\\folder";
        nr.lpProvider = NULL;
        dwResult = WNetAddConnection2(&nr,L"",L"",CONNECT_UPDATE_PROFILE);
     
     if (dwResult == NO_ERROR)
            wprintf(L"Connection added to %s\n", nr.lpRemoteName);
    But now I get error 85 (Local Resource is in use) .
    I think it is mistake in
    Code:
    nr.lpLocalName
    What I must write to this string?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: WNetAddConnection2 returns error 1200

    Quote Originally Posted by NastyaG View Post
    What I must write to this string?
    First you must read the article WNetAddConnection2 top to bottom. Please read it carefully. I'm sure you'll find interesting things there about when RESOURCE_ANY is applicable, and when it's not.

    Second, you must make sure there is no K: dos device in yor system when you add the connection. You better do that programmatically right before the call.
    Best regards,
    Igor

  6. #6
    Join Date
    Sep 2016
    Posts
    3

    Re: WNetAddConnection2 returns error 1200

    Quote Originally Posted by Igor Vartanov View Post
    First you must read the article WNetAddConnection2 top to bottom. Please read it carefully. I'm sure you'll find interesting things there about when RESOURCE_ANY is applicable, and when it's not.

    Second, you must make sure there is no K: dos device in yor system when you add the connection. You better do that programmatically right before the call.
    Thank's for comment. I read this text but my code doesn't work.
    I changed it:
    DWORD dwResult;
    NETRESOURCE nr;
    nr.dwType = RESOURCETYPE_DISK;
    nr.lpLocalName = L"K:";
    nr.lpRemoteName = L"\\\\ASYA\\folder";
    nr.lpProvider = NULL;
    dwResult = WNetAddConnection2(&nr,L"",L"",CONNECT_UPDATE_PROFILE);
    But I get error 85.
    I don't know what to write in nr.lpLocalName . Can you explain me,please ?
    Before I did a shared network folder, that located in C:\folder.
    Maybe this info help to solve my problem(

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: WNetAddConnection2 returns error 1200

    Error 85 is resource in use. From command prompt, what does
    Code:
    net use
    show?

    If the local resource trying to be used (eg k: ) is shown then you'll need to delete it first (or use another local resource if this resource is being used for another purpose).
    Code:
    net use k: \delete
    From a program, if WNetAddConnection2() fails with error 85 then use WNetCancelConnection2() to delete the resource. See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    Last edited by 2kaud; September 11th, 2016 at 04:08 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: WNetAddConnection2 returns error 1200

    Quote Originally Posted by NastyaG View Post
    I don't know what to write in nr.lpLocalName . Can you explain me,please ?
    It depends on what you're trying to achieve. Local name may be dos device name, like what you specify in your snippet, in case you want the network share be mapped to local drive. Or it may be some folder name, like what you did before. Or it may be empty string or NULL, in case you just need to log in to remote system and start using its network paths.
    Best regards,
    Igor

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: WNetAddConnection2 returns error 1200

    Like Ignor asked in post #8, what exactly are you trying to achieve? Depending upon this answer depends what you provide for .lpLocalName.

    Try doing what you want to achieve using the NET USE command from a command prompt. When that works then get WNetAddConnection2() to work. It's often easier to try with the NET USE command as you can create new connections, delete existing ones, see connections etc easily.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: WNetAddConnection2 returns error 1200

    A working sample goes: HOWTO_map_network_drive.zip

    Code:
    J:\Temp\126>make
    "Making...  "
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    126.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:126.exe
    /subsystem:console
    /out:netmap.exe
    126.obj
    
    J:\Temp\126>netmap K: \\iserver\exchange igor
    Add: 0
    
    J:\Temp\126>netmap K: /del
    K: was deleted successfully.
    
    J:\Temp\126>make unicode
    "Making...  /DUNICODE /D"_UNICODE""
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    126.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:126.exe
    /subsystem:console
    /out:netmap.exe
    126.obj
    
    J:\Temp\126>netmap K: \\iserver\exchange igor
    Add: 0
    
    J:\Temp\126>netmap K: /del
    K: was deleted successfully.
    When missing username, error 487 happens.
    When remote resource is already connected with different credentials, error 1219 happens.
    When remote resource is already connected, error 85 happens.
    When remote resource is not found, error 53 happens.

    And specifically to the subject of the thread:
    Code:
    J:\Temp\126>netmap !@#$ \\iserver\exchange1 igor
    Add: 1200
    
    J:\Temp\126>net helpmsg 1200
    
    The specified device name is invalid.
    So, 1200 means local path is something not a path.
    Last edited by Igor Vartanov; September 12th, 2016 at 04:55 AM.
    Best regards,
    Igor

Tags for this Thread

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