Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "Attempt to access invalid address" -problem

"Attempt to access invalid address" -problem

Scheduled Pinned Locked Moved C#
helpcsharpc++sysadminquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EnkelIk
    wrote on last edited by
    #1

    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

    E D 2 Replies Last reply
    0
    • E 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

      E Offline
      E Offline
      EnkelIk
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • E 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

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        DWORD values should be passed as either Integer, Int32, or UInt32 types in VB.ET and Long type in VB6. VB6 Long = VB.NET Integer RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups