"Attempt to access invalid address" -problem
-
Hi all. I'm trying to use WNetAddConnection2 to connect to a printerqueue on "lpt2:" or to some shared folder on a server on "u:" (or some other "free letter"). I have found several pieces of sample code on the net, and I have a working piece in managed C++, but in c# I get the error "Attempt to access invalid address". From what I understand the program does not recognise the local resource Some code snippets: Declarations: [StructLayout(LayoutKind.Sequential)] public class NETRESOURCE { public long dwScope = 0; public long dwType = 0; public long dwDisplayType = 0; public long dwUsage = 0; public string lpLocalName = null; public string lpRemoteName = null; public string lpComment = null; public string lpProvider = null; }; [DllImport("mpr.dll", EntryPoint="WNetAddConnection2")] public static extern int WNetAddConnection2A(NETRESOURCE lpNetResource, string lpPassword, string lpUserName, long dwFlags); "Working code": ntRec=new NETRESOURCE(); ntRec.lpRemoteName="\\\\SW\\cn=ADR-HP.ou=Printers.ou=HE.o=SW"; ntRec.dwType=RESOURCETYPE_PRINT; ntRec.lpLocalName="lpt2:"; ntRec.lpProvider=null; intRet=WNetAddConnection2A(ntRec, null,null, CONNECT_INTERACTIVE); The c++ code that works looks like this: String* pstrPrinter="\\\\SW\\cn=ADR-HP.ou=Printers.ou=HE.o=SW"; String* pstrPort="lpt2:"; TCHAR* pszName=(TCHAR*)(Marshal::StringToHGlobalAnsi(pstrPort)).ToPointer(); TCHAR* pszConn=(TCHAR*)(Marshal::StringToHGlobalAnsi(pstrPrinter)).ToPointer(); ntRec.lpRemoteName=pszConn; ntRec.dwType=RESOURCETYPE_PRINT; ntRec.lpLocalName=pszName; ntRec.lpProvider=NULL; dwRet=WNetAddConnection2(&ntRec, NULL,NULL, CONNECT_INTERACTIVE); WNetCancelConnection works in C# thought, just using "lpt2:" Any good ideas? Thanks /EnkelIk
-
Hi all. I'm trying to use WNetAddConnection2 to connect to a printerqueue on "lpt2:" or to some shared folder on a server on "u:" (or some other "free letter"). I have found several pieces of sample code on the net, and I have a working piece in managed C++, but in c# I get the error "Attempt to access invalid address". From what I understand the program does not recognise the local resource Some code snippets: Declarations: [StructLayout(LayoutKind.Sequential)] public class NETRESOURCE { public long dwScope = 0; public long dwType = 0; public long dwDisplayType = 0; public long dwUsage = 0; public string lpLocalName = null; public string lpRemoteName = null; public string lpComment = null; public string lpProvider = null; }; [DllImport("mpr.dll", EntryPoint="WNetAddConnection2")] public static extern int WNetAddConnection2A(NETRESOURCE lpNetResource, string lpPassword, string lpUserName, long dwFlags); "Working code": ntRec=new NETRESOURCE(); ntRec.lpRemoteName="\\\\SW\\cn=ADR-HP.ou=Printers.ou=HE.o=SW"; ntRec.dwType=RESOURCETYPE_PRINT; ntRec.lpLocalName="lpt2:"; ntRec.lpProvider=null; intRet=WNetAddConnection2A(ntRec, null,null, CONNECT_INTERACTIVE); The c++ code that works looks like this: String* pstrPrinter="\\\\SW\\cn=ADR-HP.ou=Printers.ou=HE.o=SW"; String* pstrPort="lpt2:"; TCHAR* pszName=(TCHAR*)(Marshal::StringToHGlobalAnsi(pstrPort)).ToPointer(); TCHAR* pszConn=(TCHAR*)(Marshal::StringToHGlobalAnsi(pstrPrinter)).ToPointer(); ntRec.lpRemoteName=pszConn; ntRec.dwType=RESOURCETYPE_PRINT; ntRec.lpLocalName=pszName; ntRec.lpProvider=NULL; dwRet=WNetAddConnection2(&ntRec, NULL,NULL, CONNECT_INTERACTIVE); WNetCancelConnection works in C# thought, just using "lpt2:" Any good ideas? Thanks /EnkelIk
Hm. What can I say? Going over the code again, and replacing the long with enumerators in NETRESOURCE -I had some problem with these before but I can't quite remember what. Having done this it works like a charm. Can't believe I have spent hours to solve this! Sorry for taking your time ;) Thanks /EnkelIk
-
Hi all. I'm trying to use WNetAddConnection2 to connect to a printerqueue on "lpt2:" or to some shared folder on a server on "u:" (or some other "free letter"). I have found several pieces of sample code on the net, and I have a working piece in managed C++, but in c# I get the error "Attempt to access invalid address". From what I understand the program does not recognise the local resource Some code snippets: Declarations: [StructLayout(LayoutKind.Sequential)] public class NETRESOURCE { public long dwScope = 0; public long dwType = 0; public long dwDisplayType = 0; public long dwUsage = 0; public string lpLocalName = null; public string lpRemoteName = null; public string lpComment = null; public string lpProvider = null; }; [DllImport("mpr.dll", EntryPoint="WNetAddConnection2")] public static extern int WNetAddConnection2A(NETRESOURCE lpNetResource, string lpPassword, string lpUserName, long dwFlags); "Working code": ntRec=new NETRESOURCE(); ntRec.lpRemoteName="\\\\SW\\cn=ADR-HP.ou=Printers.ou=HE.o=SW"; ntRec.dwType=RESOURCETYPE_PRINT; ntRec.lpLocalName="lpt2:"; ntRec.lpProvider=null; intRet=WNetAddConnection2A(ntRec, null,null, CONNECT_INTERACTIVE); The c++ code that works looks like this: String* pstrPrinter="\\\\SW\\cn=ADR-HP.ou=Printers.ou=HE.o=SW"; String* pstrPort="lpt2:"; TCHAR* pszName=(TCHAR*)(Marshal::StringToHGlobalAnsi(pstrPort)).ToPointer(); TCHAR* pszConn=(TCHAR*)(Marshal::StringToHGlobalAnsi(pstrPrinter)).ToPointer(); ntRec.lpRemoteName=pszConn; ntRec.dwType=RESOURCETYPE_PRINT; ntRec.lpLocalName=pszName; ntRec.lpProvider=NULL; dwRet=WNetAddConnection2(&ntRec, NULL,NULL, CONNECT_INTERACTIVE); WNetCancelConnection works in C# thought, just using "lpt2:" Any good ideas? Thanks /EnkelIk