Click here to Skip to main content
15,889,096 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to connect variable to generated control ? Pin
Heath Stewart3-Jun-04 4:07
protectorHeath Stewart3-Jun-04 4:07 
AnswerRe: How to connect variable to generated control ? Pin
Dave Kreskowiak3-Jun-04 4:10
mveDave Kreskowiak3-Jun-04 4:10 
GeneralRe: How to connect variable to generated control ? Pin
vgrigor3-Jun-04 4:25
vgrigor3-Jun-04 4:25 
GeneralRe: How to connect variable to generated control ? Pin
Dave Kreskowiak3-Jun-04 4:52
mveDave Kreskowiak3-Jun-04 4:52 
GeneralBest class structure (visually) Pin
Roger Alsing3-Jun-04 0:33
Roger Alsing3-Jun-04 0:33 
GeneralRe: Best class structure (visually) Pin
Stefan Troschuetz3-Jun-04 1:24
Stefan Troschuetz3-Jun-04 1:24 
GeneralRe: Best class structure (visually) Pin
Colin Angus Mackay3-Jun-04 1:33
Colin Angus Mackay3-Jun-04 1:33 
General"Attempt to access invalid address" -problem Pin
EnkelIk2-Jun-04 23:48
EnkelIk2-Jun-04 23:48 
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
GeneralRe: "Attempt to access invalid address" -problem Pin
EnkelIk2-Jun-04 23:58
EnkelIk2-Jun-04 23:58 
GeneralRe: "Attempt to access invalid address" -problem Pin
Dave Kreskowiak3-Jun-04 4:04
mveDave Kreskowiak3-Jun-04 4:04 
GeneralExcel range limits Pin
the last free name2-Jun-04 23:34
the last free name2-Jun-04 23:34 
GeneralRe: Excel range limits Pin
the last free name3-Jun-04 2:34
the last free name3-Jun-04 2:34 
GeneralBurn with nero Pin
bjoerg742-Jun-04 22:45
sussbjoerg742-Jun-04 22:45 
GeneralMousewheel/Scrollbar questoin Pin
Aaron Eldreth2-Jun-04 17:01
Aaron Eldreth2-Jun-04 17:01 
GeneralRe: Mousewheel/Scrollbar questoin Pin
Stefan Troschuetz2-Jun-04 20:53
Stefan Troschuetz2-Jun-04 20:53 
GeneralRe: Mousewheel/Scrollbar questoin Pin
Aaron Eldreth3-Jun-04 2:45
Aaron Eldreth3-Jun-04 2:45 
GeneralThreading Error Pin
Wackatronic2-Jun-04 15:54
Wackatronic2-Jun-04 15:54 
GeneralRe: Threading Error Pin
flow55552-Jun-04 18:10
flow55552-Jun-04 18:10 
GeneralRe: Threading Error Pin
Stefan Troschuetz2-Jun-04 21:35
Stefan Troschuetz2-Jun-04 21:35 
GeneralRe: Threading Error Pin
Wackatronic3-Jun-04 6:12
Wackatronic3-Jun-04 6:12 
GeneralRe: Threading Error Pin
Stefan Troschuetz2-Jun-04 21:32
Stefan Troschuetz2-Jun-04 21:32 
Questionusing the Visual IDE ? Pin
triso2-Jun-04 13:31
triso2-Jun-04 13:31 
AnswerRe: using the Visual IDE ? Pin
Heath Stewart2-Jun-04 13:39
protectorHeath Stewart2-Jun-04 13:39 
GeneralExcel 2003 interop Pin
Dimitris Iliopoulos2-Jun-04 12:16
Dimitris Iliopoulos2-Jun-04 12:16 
GeneralRe: Excel 2003 interop Pin
Heath Stewart2-Jun-04 13:26
protectorHeath Stewart2-Jun-04 13:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.