For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2006 > Re: cannot convert from <STRUCT_NAME> to ref <STRUCT_NAME>









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: cannot convert from <STRUCT_NAME> to ref <STRUCT_NAME>
Ciaran

2006-01-10, 4:09 am

The function declaration :
public static extern Int32 WNetAddConnection2(
ref NETRESOURCE
lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string lpUserName,
Int32 dwFlags);
Says the parameter must be passed by reference. You need to call this function like:

WNetAddConnection2(ref theNetResource, ........

Give that a try

Ciaran


"Gee" <info@emmanuelsolutions.co.uk> wrote in message news:1119362691.935521.221740@z14g2000cwz.googlegroups.com...
>I get the above error with this code and I can't figure out why? Any
> ideas please? See code below - the actual error is included in the
> code:
>
> public struct NETRESOURCE
> {
> public Int32 dwScope ;
> public Int32 dwType;
> public Int32 dwDisplayType;
> public Int32 dwUsage;
> public string lpLocalName;
> public string lpRemoteName;
> public string lpComment;
> public string lpProvider;
> }
>
> public const Int32 NO_ERROR = 0;
> public const Int32 CONNECT_UPDATE_PROFILE = 0x1;
> public const Int32 RESOURCETYPE_DISK = 0x1;
>
> [System.Runtime.InteropServices.DllImport(
> "mpr.dll",
> EntryPoint="WNetAddConnection2A",
> ExactSpelling=false,
> CharSet=System.Runtime.InteropServices.CharSet.Ansi,
> SetLastError=true)]
>
> public static extern Int32 WNetAddConnection2(
> ref NETRESOURCE
> lpNetResource,
> [MarshalAs(UnmanagedType.LPStr)] string lpPassword,
> [MarshalAs(UnmanagedType.LPStr)] string lpUserName,
> Int32 dwFlags);
>
> [System.Runtime.InteropServices.DllImport(
> "mpr.dll",
> EntryPoint="WNetCancelConnection2A",
> ExactSpelling=false,
> CharSet=System.Runtime.InteropServices.CharSet.Ansi,
> SetLastError=true)]
>
> public static extern long WNetCancelConnection2(
> string lpName,
> long dwFlags,
> long fForce);
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> Int32 intRes;
> long linResult;
> NETRESOURCE theNetResource;
> Enterprise01.Toolkit objToolkit;
>
> //' Set Resource Type
> theNetResource.dwType = RESOURCETYPE_DISK;
>
> //'Set Remote server & share name
> theNetResource.lpRemoteName = "\\\\server\\share";
>
> //'Set Local Path for share
> theNetResource.lpLocalName = "F:";
>
> //'Ensure the Username and Password are set correctly
> // ********************FIRST_PARAMETER_CAUS
ES_ERROR********************************
*
>
> // \\server\wwwroot$\TEST\pgeCOMtest.aspx.cs(85): The best overloaded
> method match for 'TEST.pgeCOMtest.WNetAddConnection2(ref
> TEST.pgeCOMtest.NETRESOURCE, string, string, int)' has some invalid
> arguments
> // \\server\wwwroot$\TEST\pgeCOMtest.aspx.cs(86): Argument '1': cannot
> convert from 'TEST.pgeCOMtest.NETRESOURCE' to 'ref
> TEST.pgeCOMtest.NETRESOURCE'
>
>
> linResult = WNetAddConnection2(theNetResource, "pwd", "dom_usr",
> CONNECT_UPDATE_PROFILE);
>
>
> // ****************************************
****************************************
***
>
> objToolkit = new Enterprise01.ToolkitClass();
> intRes = objToolkit.OpenToolkit();
>
> if(intRes > 0)
> {
> Response.Write("Error opening toolkit: " +
> objToolkit.LastErrorString);
> intRes = objToolkit.CloseToolkit();
> if(intRes > 0)
> {
> Response.Write("Error closing toolkit: " +
> objToolkit.LastErrorString);
> linResult = WNetCancelConnection2(theNetResource.lpLocalName, 0,
> 0);
> Response.End();
> }
> }
> }
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com