|
-
September 11th, 2016, 11:12 AM
#1
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?
-
September 11th, 2016, 01:05 PM
#2
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)
-
September 11th, 2016, 02:12 PM
#3
Re: WNetAddConnection2 returns error 1200
 Originally Posted by NastyaG
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
-
September 11th, 2016, 02:48 PM
#4
Re: WNetAddConnection2 returns error 1200
 Originally Posted by Igor Vartanov
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 What I must write to this string?
-
September 11th, 2016, 03:24 PM
#5
Re: WNetAddConnection2 returns error 1200
 Originally Posted by NastyaG
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
-
September 11th, 2016, 03:39 PM
#6
Re: WNetAddConnection2 returns error 1200
 Originally Posted by Igor Vartanov
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(
-
September 11th, 2016, 04:04 PM
#7
Re: WNetAddConnection2 returns error 1200
Error 85 is resource in use. From command prompt, what does
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).
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)
-
September 12th, 2016, 02:07 AM
#8
Re: WNetAddConnection2 returns error 1200
 Originally Posted by NastyaG
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
-
September 12th, 2016, 02:53 AM
#9
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)
-
September 12th, 2016, 04:37 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|