Click here to Skip to main content
15,917,731 members
Home / Discussions / C#
   

C#

 
GeneralPrint To File Pin
Gary Thom3-Jun-04 4:16
Gary Thom3-Jun-04 4:16 
GeneralReplicateTextbox functions Pin
Aaron Eldreth3-Jun-04 3:05
Aaron Eldreth3-Jun-04 3:05 
GeneralRe: ReplicateTextbox functions Pin
Heath Stewart3-Jun-04 4:04
protectorHeath Stewart3-Jun-04 4:04 
GeneralRe: ReplicateTextbox functions Pin
Aaron Eldreth3-Jun-04 10:07
Aaron Eldreth3-Jun-04 10:07 
GeneralRe: ReplicateTextbox functions Pin
Dave Kreskowiak3-Jun-04 10:09
mveDave Kreskowiak3-Jun-04 10:09 
QuestionHow to connect variable to generated control ? Pin
vgrigor3-Jun-04 2:52
vgrigor3-Jun-04 2:52 
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 

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.