cannot convert from <STRUCT_NAME> to ref <STRUCT_NAME>

G

Gee

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.InteropService­s.DllImport(
"mpr.dll",
EntryPoint="WNetAddConnection2­A",
ExactSpelling=false,

CharSet=System.Runtime.Interop­Services.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.InteropService­s.DllImport(
"mpr.dll",
EntryPoint="WNetCancelConnecti­on2A",
ExactSpelling=false,

CharSet=System.Runtime.Interop­Services.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_PA­RAMETER_CAUSES_ERROR**********­***********************



// \\server\wwwroot$\TEST\pgeCOMt­est.aspx.cs(85): The best
overloaded
method match for 'TEST.pgeCOMtest.WNetAddConnec­tion2(ref
TEST.pgeCOMtest.NETRESOURCE, string, string, int)' has some invalid
arguments
// \\server\wwwroot$\TEST\pgeCOMt­est.aspx.cs(86): Argument '1':
cannot
convert from 'TEST.pgeCOMtest.NETRESOURCE' to 'ref
TEST.pgeCOMtest.NETRESOURCE'


linResult = WNetAddConnection2(theNetResou­rce, "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(theNetRe­source.lpLocalName, 0,
0);
Response.End();
}
}
}
 
C

Christof Nordiek

Hi Gee

has to be
linResult = WNetAddConnection2( ref theNetResou­rce, "pwd", "dom_usr",
CONNECT_UPDATE_PROFILE);

If there is a ref in the declaration, there has also to be a ref in the
call.

Christof

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.InteropService­s.DllImport(
"mpr.dll",
EntryPoint="WNetAddConnection2­A",
ExactSpelling=false,

CharSet=System.Runtime.Interop­Services.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.InteropService­s.DllImport(
"mpr.dll",
EntryPoint="WNetCancelConnecti­on2A",
ExactSpelling=false,

CharSet=System.Runtime.Interop­Services.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_PA­RAMETER_CAUSES_ERROR**********­***********************



// \\server\wwwroot$\TEST\pgeCOMt­est.aspx.cs(85): The best
overloaded
method match for 'TEST.pgeCOMtest.WNetAddConnec­tion2(ref
TEST.pgeCOMtest.NETRESOURCE, string, string, int)' has some invalid
arguments
// \\server\wwwroot$\TEST\pgeCOMt­est.aspx.cs(86): Argument '1':
cannot
convert from 'TEST.pgeCOMtest.NETRESOURCE' to 'ref
TEST.pgeCOMtest.NETRESOURCE'


linResult = WNetAddConnection2(theNetResou­rce, "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(theNetRe­source.lpLocalName, 0,
0);
Response.End();
}
}
}
 
A

Andy

When calling a method that expects a parameter by ref, C# requires that
you put the ref keyword in when you write the call to the method, not
just in the method declaration.

I imagine that its to make it clear that you are passing something by
reference.

HTH
Andy
 
G

Gee

Thanks Andy and Christof!

I've also tried that but that gives me this error:
Use of unassigned local variable 'theNetResource'

Not sure what that means, but if it means that I've not assigned it a
value, in the code I've instantiated it and assigned values to it's
properties.

Any ideas?

Cheers,

-Gee.
 
A

Adam Clauss

Can you post the code where you instantiate it and then call the function?

Thanks,
Adam Clauss
 
A

Adam Clauss

Sorry, ignore previous post.

I do not see where you instantiate the NETRESOURCE. Add "= new
NETRESOURCE();"

NETRESOURCE theNetResource = new NETRESOURCE(); <---- ADD THIS
Enterprise01.Toolkit objToolkit;
 
G

Gee

Doh!

Simple as that eh! Damnit, it's these little oversights that take ages
to figure out! :)

Thanks Adam!

-Gee.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top