rasdial returns 632 on x64 bit Vista nd Windows XP

H

hq4000

I have a 32-bit application installed on x64 (AMD64) Vista and Windows
XP. The rasdial returns 632 - ERROR_INVALID_SIZE on x64 machine; but
the rasdial behaves properly installing the same 32-bit application on
a 32-bit machine. Any clue?
 
H

hq4000

1. Rasdial:

[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
public extern static uint RasDial(
[In]RASDIALEXTENSIONS lpRasDialExtensions,
[In]string lpszPhonebook,
[In]RASDIALPARAMS lpRasDialParams,
uint dwNotifierType,
IntPtr lpvNotifier,
ref IntPtr lphRasConn
);

2. RASDIALPARAMS:

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public class RASDIALPARAMS
{
public int dwSize=Marshal.SizeOf(typeof(RASDIALPARAMS));
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szEntryName=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szPhoneNumber=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szCallbackNumber=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szUserName=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szPassword=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=15+1)]
public string szDomain=null;
public int dwSubEntry=0;
public int dwCallbackId=0;
}

3. Usage:

RASDIALPARAMS rdp = new RASDIALPARAMS();
rdp.dwSize = Marshal.SizeOf(rdp);
rdp.szEntryName = "Entry Name;
IntPtr notifyHandle = (IntPtr)null;
IntPtr connectionHandle = (IntPtr)null;
uint dwSucc = RASWrapper.RasDial(null, null, rdp, 0xFFFFFFFF,
notifyHandle, ref connectionHandle);
 
N

Nicholas Paldino [.NET/C# MVP]

What is the definition of RASDIALEXTENSIONS? Are you using a class to
define it? I'm assuming so, so that you can pass null in.

One thing I definitely notice is the definition of the dwCallbackId
field in the RASDIALPARAMS structure. You are defining it as an int, when
it should be an IntPtr. This would probably explain why the size is wrong
when calling on a 64-bit platform.

Assuming that all the lengths to the strings are correct, this should be
the issue.
 
H

hq4000

Following are my original definition. With your suggestion, I changed
int in 2. 4. and 5. for IntPtr; and I changed uint in 4. and 5. for
UIntPtr. But still RasDial still returns 632.

4.RASDIALEXTENSIONS

[StructLayout(LayoutKind.Sequential)]
public class RASDIALEXTENSIONS
{
public readonly int
dwSize=Marshal.SizeOf(typeof(RASDIALEXTENSIONS));
public uint dwfOptions=0;
public int hwndParent=0;
public int reserved=0;
public int reserved1=0;
public RASEAPINFO RasEapInfo=new RASEAPINFO();
}

5.RASEAPINFO:

[StructLayout(LayoutKind.Sequential)]
public struct RASEAPINFO
{
public uint dwSizeofEapInfo;
public int pbEapInfo;
}
 
W

Willy Denoyette [MVP]

Following are my original definition. With your suggestion, I changed
int in 2. 4. and 5. for IntPtr; and I changed uint in 4. and 5. for
UIntPtr. But still RasDial still returns 632.

4.RASDIALEXTENSIONS

[StructLayout(LayoutKind.Sequential)]
public class RASDIALEXTENSIONS
{
public readonly int
dwSize=Marshal.SizeOf(typeof(RASDIALEXTENSIONS));
public uint dwfOptions=0;
public int hwndParent=0;
public int reserved=0;
public int reserved1=0;
public RASEAPINFO RasEapInfo=new RASEAPINFO();
}

5.RASEAPINFO:

[StructLayout(LayoutKind.Sequential)]
public struct RASEAPINFO
{
public uint dwSizeofEapInfo;
public int pbEapInfo;
}


Please post your actual definitions.

Willy.
 
H

hq4000

what you see in 1. 2. 3. 4. 5. is basically everything I have as it
is. And they worked in 32-bit machine, but not the x64 ones. Of
course, I mssed a '}' in 5. Please clarify what you need?
 
D

da5namroi

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public class RASDIALPARAMS
{
public int dwSize=Marshal.SizeOf(typeof(RASDIALPARAMS));

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName
+1)]
public string szEntryName=null;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxPhoneNumber
+1)]
public string szPhoneNumber=null;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxCallbackNumber
+1)]
public string szCallbackNumber=null;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.UNLEN
+1)]
public string szUserName=null;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.PWLEN
+1)]
public string szPassword=null;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.DNLEN
+1)]
public string szDomain=null;
public int dwSubEntry=0;
public IntPtr dwCallbackId= IntPtr.Zero ;
}

[StructLayout(LayoutKind.Sequential)]
public class RASDIALEXTENSIONS
{
public readonly int
dwSize=Marshal.SizeOf(typeof(RASDIALEXTENSIONS));
public uint dwfOptions=0;
public int hwndParent=0;
public int reserved=0;
public int reserved1=0;
public RASEAPINFO RasEapInfo=new RASEAPINFO();
}

[DllImport("coredll.dll", CharSet = CharSet.Auto)]
public extern static uint RasDial(
[In]RASDIALEXTENSIONS lpRasDialExtensions,
// pointer to function extensions data
[In]string lpszPhonebook, // pointer to full path and file
// name of phone-book file
[In]RASDIALPARAMS lpRasDialParams,
// pointer to calling parameters data
uint dwNotifierType, // specifies type of RasDial event
handler
IntPtr lpvNotifier, // specifies a handler for RasDial
events
ref IntPtr lphRasConn // pointer to variable to receive
// connection handle
);


public bool DialConnection()
{
bool bRetVal = false;
m_applicationLog.AppendLineToLog("DialConnection");
// try to reconnect
uint dwSucc = 0;
if (IsAlreadyConnected() == false)
{
m_applicationLog.AppendLineToLog("IsAlreadyConnected()
== false");
DoHangup();

RASDIALPARAMS rdp = new RASDIALPARAMS();
rdp.dwSize = Marshal.SizeOf(rdp);
rdp.szEntryName = m_connectionName;
uint dwNotifierType = 0xFFFFFFFF;
dwSucc = RASWrapper.RasDial(null, null, rdp,
dwNotifierType, m_notifyHandle, ref m_connection);
m_weDialed = true;
}
else
{
m_applicationLog.AppendLineToLog("IsAlreadyConnected()
== true");
m_weDialed = false;
}

if (dwSucc == 0)
{
bRetVal = true;
}

return bRetVal;
}
 
H

hq4000

in 3. above, out of desperate, I changed:

rdp.dwSize = Marshal.SizeOf(rdp);

to

rdp.dwSize = Marshal.SizeOf(rdp) + 4.

Then the rasdial returns successfully in x64. I don't think this is a
soloution, but do have more to say based on this hint?
 
W

Willy Denoyette [MVP]

what you see in 1. 2. 3. 4. 5. is basically everything I have as it
is. And they worked in 32-bit machine, but not the x64 ones. Of
course, I mssed a '}' in 5. Please clarify what you need?

Oh, but I don't see any IntPtr's, the HWND field (hwndParent) in
RASDIALEXTENSIONS must be an IntPtr (all Handles are IntPtr in .NET), the
same is true for the ULONG_PTR fields in RASDIALEXTENSIONS (reserved and
reserved1)and RASDIALPARAMS (dwCallbackId ).

Willy.
 
D

da5namroi

After making the changes as you suggest,

RASDIALPARAMS rdp = new RASDIALPARAMS();
rdp.dwSize = Marshal.SizeOf(rdp); returns a value of
2120.

We found that the "magic number" for it to work is to when
Marshal.SizeOf(rpd) is 2108.

Do you have any idea why?
 
W

Willy Denoyette [MVP]

After making the changes as you suggest,

RASDIALPARAMS rdp = new RASDIALPARAMS();
rdp.dwSize = Marshal.SizeOf(rdp); returns a value of
2120.

We found that the "magic number" for it to work is to when
Marshal.SizeOf(rpd) is 2108.

Do you have any idea why?


Don't use classes for interop, use structures and check the structure
packing as used in the header files.

This:
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto, Pack=4)]
public struct RASDIALPARAMS
{
public int dwSize=Marshal.SizeOf(typeof(RASDIALPARAMS));
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szEntryName=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szPhoneNumber=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szCallbackNumber=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szUserName=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szPassword=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=15+1)]
public string szDomain=null;
public int dwSubEntry=0;
public IntPtr dwCallbackId;
}

returns 2108 for dwSize.


Willy.
 
W

Willy Denoyette [MVP]

Willy Denoyette said:
After making the changes as you suggest,

RASDIALPARAMS rdp = new RASDIALPARAMS();
rdp.dwSize = Marshal.SizeOf(rdp); returns a value of
2120.

We found that the "magic number" for it to work is to when
Marshal.SizeOf(rpd) is 2108.

Do you have any idea why?


Don't use classes for interop, use structures and check the structure
packing as used in the header files.

This:
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto, Pack=4)]
public struct RASDIALPARAMS
{
public int dwSize=Marshal.SizeOf(typeof(RASDIALPARAMS));
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szEntryName=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szPhoneNumber=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szCallbackNumber=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szUserName=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szPassword=null;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=15+1)]
public string szDomain=null;
public int dwSubEntry=0;
public IntPtr dwCallbackId;
}

returns 2108 for dwSize.


Willy.



Correction, a struct cannot contain field initializers, here is the
corrected struct version:

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto, Pack=4)]
public struct RASDIALPARAMS
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szEntryName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szPhoneNumber;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128+1)]
public string szCallbackNumber;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szUserName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256+1)]
public string szPassword;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=15+1)]
public string szDomain;
public int dwSubEntry;
public IntPtr dwCallbackId;
}

// usage....

RASDIALPARAMS rasdial = new RASDIALPARAMS();
rasdial.dwSize = Marshal.SizeOf(typeof(RASDIALPARAMS));


Willy.
 
D

da5namroi

Willy,

Changing class to struct makes it work on 64x, but crash on 32x. The
return value of rasdial.dwSize is 2104 on 32x, which is correct, but
then the call
dwSucc = RASWrapper.RasDial(null, null, rdp, dwNotifierType,
m_notifyHandle, ref m_connection);

creates a crash. It appears that 32x only works well when
RASDIALPARAMS is a class, not a struct and vice versa for 64x.

Thank you for your responses
 
D

da5namroi

Willy,

Changing class to struct makes it work on 64x, but crash on 32x. The
return value of rasdial.dwSize is 2104 on 32x, which is correct, but
then the call
dwSucc = RASWrapper.RasDial(null, null, rdp, dwNotifierType,
m_notifyHandle, ref m_connection);

creates a crash. It appears that 32x only works well when
RASDIALPARAMS is a class, not a struct and vice versa for 64x.

Thank you for your responses
 
W

Willy Denoyette [MVP]

Willy,

Changing class to struct makes it work on 64x, but crash on 32x. The
return value of rasdial.dwSize is 2104 on 32x, which is correct, but
then the call
dwSucc = RASWrapper.RasDial(null, null, rdp, dwNotifierType,
m_notifyHandle, ref m_connection);

creates a crash. It appears that 32x only works well when
RASDIALPARAMS is a class, not a struct and vice versa for 64x.

Thank you for your responses

It should work with both cases, no matter what you specify (class or struct)
the correct sizes are 2104 for x86 and 2108 for X64.
I guess there is something wrong with your RASWrapper code, you should check
the packing and alignment requirements for both CS and the wrapper module,
there must be a mismatch.
I would love to see the RASWrapper code if you don't mind posting.

Willy.
 
D

da5namroi

Willy,

Could you please clarify your previous post:

1) What do you mean by "CS" in "you should check the packing and
alignment requirements for both CS and the wrapper module"
2) How do I check the alignment requirements?

If we need to send you the RASWrapper class, would you mind if we send
it directly to your email?

Thank you for your help,
 
W

Willy Denoyette [MVP]

Willy,

Could you please clarify your previous post:

1) What do you mean by "CS" in "you should check the packing and
alignment requirements for both CS and the wrapper module"

CS = csharp, sorry about the confusion :-(
The wrapper is a typo, should read "wrapped" module, that is the module you
are calling, here it is Rasapi32.dll.
2) How do I check the alignment requirements?
The wrapped module's definitions can be found in the header file ras.h, you
need to make sure your definitions in C# are correctly mapped to the
definitions in the header file, watch for explicit alignment directives, for
differences in structure alignment and packing between 32 and 64 bit and
across OS versions.
Also check your API declaration in C#, they should correctly map to the
declarations in the header file.
If we need to send you the RASWrapper class, would you mind if we send
it directly to your email?

No problem.

Willy.
 
D

da5namroi

Willy,

Could you please clarify a few things on your previous posts?

1) How do I check alignment requirement?
2) What do you mean by "CS" in "check the packing and alignment
requirements for both CS and the wrapper module "?

Also, if we really need to send you the RasWrapper file, can we send
it directly to your email address?

Thanks for your help,
 

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