pointers

i have a couple of questions

i am trying to use the following function that was written in unmanaged code:

DWORD RasDial(
 LPRASDIALEXTENSIONS lpRasDialExtensions,
 LPCTSTR lpszPhonebook,
 LPRASDIALPARAMS lpRasDialParams,
 DWORD dwNotifierType,
 LPVOID lpvNotifier,
 LPHRASCONN lphRasConn
);
i am declared it like this

[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]

public extern static uint RasDial(IntPtr lpRasDialExtentoins,string phoneBook,ref RasDialParams lpRasDialParams,int dwNotifierType,IntPtr lpNotifier,ref IntPtr lpRasConn);

 

lpNotifier - Call back function

 

1) have i done it correctle

2) how do i cast from delecate to IntPtr

3) how do i cast from IntPtr to the object it is pointing to

 



Answer this question

pointers

  • Matt W

    using System;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Forms;
    using Microsoft.WindowsCE.Forms;

    namespace CWRS2.util
    {
    /// <summary>
    /// Summary description for RasManager.
    /// </summary>
    public class RasUtil : MessageWindow
    {
    private class RASDIALPARAMS : AdvancedMarshaler
    {
    private const int RAS_MaxEntryName = 20;
    private const int RAS_MaxPhoneNumber = 128;
    private const int RAS_MaxCallbackNumber = 48;
    private const int UNLEN = 256;
    private const int PWLEN = 256;
    private const int DNLEN = 15;
    private const int MAX_PATH = 260;
    private const int RAS_MaxDeviceType = 16;

    public int dwSize;
    [CustomMarshalAs(SizeConst = RAS_MaxEntryName + 1)]
    public string szEntryName;
    [CustomMarshalAs(SizeConst = RAS_MaxPhoneNumber + 1)]
    public string szPhoneNumber;
    [CustomMarshalAs(SizeConst = RAS_MaxCallbackNumber + 1)]
    public string szCallbackNumber;
    [CustomMarshalAs(SizeConst = UNLEN + 1)]
    public string szUserName;
    [CustomMarshalAs(SizeConst = PWLEN + 1)]
    public string szPassword;
    [CustomMarshalAs(SizeConst = DNLEN + 1)]
    public string szDomain;
    public byte Dummy1;
    public byte Dummy2;

    public RASDIALPARAMS()
    {
    dwSize = (int)GetSize();
    data = new byte[dwSize];
    Serialize();
    }
    }

    private class RASCONNSTATUS : AdvancedMarshaler
    {
    private const int RAS_MaxDeviceType = 16;
    private const int RAS_MaxDeviceName = 128;

    public uint dwSize;
    public int rasconnstate;
    public uint dwError;
    [CustomMarshalAs(SizeConst = RAS_MaxDeviceType + 1)]
    public string szDeviceType;
    [CustomMarshalAs(SizeConst = RAS_MaxDeviceName + 1)]
    public string szDeviceName;

    public RASCONNSTATUS()
    {
    dwSize = (uint)GetSize();
    data = new byte[dwSize];
    Serialize();
    }
    }

    private enum RASCONNSTATE : int
    {
    RASCS_OpenPort = 0, //0
    RASCS_PortOpened, //1
    RASCS_ConnectDevice, //2
    RASCS_DeviceConnected, //3
    RASCS_AllDevicesConnected, //4
    RASCS_Authenticate, //5
    RASCS_AuthNotify, //6
    RASCS_AuthRetry, //6
    RASCS_AuthCallback, //8
    RASCS_AuthChangePassword, //9
    RASCS_AuthProject, //10
    RASCS_AuthLinkSpeed, //11
    RASCS_AuthAck, //12
    RASCS_ReAuthenticate, //13
    RASCS_Authenticated, //14
    RASCS_PrepareForCallback, //15
    RASCS_WaitForModemReset, //16
    RASCS_WaitForCallback, //17
    RASCS_Projected, //18

    RASCS_Interactive = RASCS_PAUSED, //4096
    RASCS_RetryAuthentication, //4097
    RASCS_CallbackSetByCaller, //4098
    RASCS_PasswordExpired, //4099

    RASCS_Connected = RASCS_DONE, //8192
    RASCS_Disconnected //8193
    } ;

    [DllImport("coredll.dll", CharSet = CharSet.Auto)]
    private static extern int RasDial(IntPtr pDialExtensions,
    string pPhonebook,
    byte[] pRasDialParam,
    uint pNotifierType,
    IntPtr pHwnd,
    ref int pRasConn);

    [DllImport("coredll.dll", CharSet = CharSet.Auto)]
    private static extern Int32 RasGetEntryDialParams(string lpszPhoneBook,
    byte[] lpRasDialParams,
    ref UInt32 lpfPassword);

    [DllImport("coredll.dll", CharSet = CharSet.Auto)]
    private static extern int RasHangUp(int pSession);

    [DllImport("coredll.dll", CharSet = CharSet.Auto)]
    private static extern uint RasGetConnectStatus(int pSession, byte[] lpRasConnStatus);

    private const int RASCS_PAUSED = 0x1000;
    private const int RASCS_DONE = 0x2000;
    private const int WM_RASDIALEVENT = 0xCCCD;

    private RASDIALPARAMS RasDialParams;
    private int mRasConn;
    private IntPtr mDialExtensions = IntPtr.Zero;

    private static RasUtil mInstance;

    public delegate void NotifyEvent();
    public event NotifyEvent OnConnectedEvent;
    public event NotifyEvent OnDisconnectedEvent;

    private RasUtil()
    {
    mRasConn = 0;
    RasDialParams = new RASDIALPARAMS();
    }

    public static RasUtil getInstance()
    {
    if (mInstance == null)
    {
    mInstance = new RasUtil();
    }

    return mInstance;
    }

    public void dialUp(string pEntryName, string pUserName, string pPassword)
    {
    RasDialParams.szEntryName = pEntryName;
    RasDialParams.Serialize();

    UInt32 lpfPassword = 0;
    int lResult = RasGetEntryDialParams(null, RasDialParams.ByteArray, ref lpfPassword);

    if (lResult != 0)
    {
    throw new ApplicationException("RasGetEntryDialParams(null, RasDialParams.ByteArray, ref lpfPassword) failed with error code: " + lResult);
    }

    RasDialParams.Deserialize();

    RasDialParams.szUserName = pUserName;
    RasDialParams.szPassword = pPassword;

    RasDialParams.Serialize();

    lResult = RasDial(mDialExtensions, null, RasDialParams.ByteArray, 0xFFFFFFFF, this.Hwnd, ref mRasConn);

    if (lResult != 0)
    {
    throw new ApplicationException("RasDial(0, null, RasDialParams.ByteArray, 0xFFFFFFFF, this.Hwnd, ref " + mRasConn + ") failed with error code: " + lResult);
    }
    }

    public int getStatus()
    {
    RASCONNSTATUS lRasStatus = new RASCONNSTATUS();
    uint lReturn = RasGetConnectStatus(mRasConn, lRasStatus.ByteArray);
    lRasStatus.Deserialize();

    if (lReturn == 0)
    {
    return lRasStatus.rasconnstate;
    }
    else
    {
    throw new ApplicationException("RasGetConnectStatus(" + mRasConn + ", lRasStatus.ByteArray) failed with error code: " + lReturn);
    }
    }

    public void hangUp()
    {
    if (mRasConn != 0)
    {
    int lStatus = RasHangUp(mRasConn);

    if (lStatus != 0)
    {
    throw new ApplicationException("RasHangUp(" + mRasConn + ") failed with error code: " + lStatus);
    }

    mRasConn = 0;
    }
    else
    {
    onDisconnected();
    }
    }

    private void onConnected()
    {
    if (OnConnectedEvent != null)
    {
    OnConnectedEvent();
    }
    }

    private void onDisconnected()
    {
    if (OnDisconnectedEvent != null)
    {
    OnDisconnectedEvent();
    }
    }

    protected override void WndProc(ref Message pMsg)
    {
    if (pMsg.Msg == WM_RASDIALEVENT)
    {
    switch ((int) pMsg.WParam)
    {
    case (int) RASCONNSTATE.RASCS_OpenPort:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_PortOpened:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_ConnectDevice:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_DeviceConnected:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AllDevicesConnected:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_Authenticate:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthNotify:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthRetry:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthCallback:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthChangePassword:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthProject:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthLinkSpeed:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_AuthAck:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_ReAuthenticate:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_Authenticated:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_PrepareForCallback:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_WaitForModemReset:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_WaitForCallback:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_Projected:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_Interactive:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_RetryAuthentication:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_CallbackSetByCaller:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_PasswordExpired:
    {
    break;
    }
    case (int) RASCONNSTATE.RASCS_Connected:
    {
    onConnected();
    break;
    }
    case (int) RASCONNSTATE.RASCS_Disconnected:
    {
    onDisconnected();
    break;
    }
    default:
    {
    break;
    }
    }
    }

    base.WndProc(ref pMsg);
    }
    }
    }

  • Acts7

    i figured it out the other 2 questions what i need,is to get the object that is beeing pointed by IntPtr, meaning:

    if i have:

    IntPtr a;

    f(ref a);

    and the i know that a  is pointed to "Customer" object how do i get the information that the function f put there.

    Regards


  • Nicola Zanardi

    Avishay,
         I'm not exactly sure what you are trying to do, but here is a link that I used which shows you how to call a windows API (unmanaged) and pass it a callback function that I'd written in C#. Hopefully this will help.
    http://support.microsoft.com/default.aspx scid=kb;en-us;318804

    Regards
    Alan

  • Anthony Moore

    You can only marshal a pointer to a struct/ValueType, as that is the nature of a struct. Look at Marshal.PtrToStructure(IntPtr, Type). For example:

    public struct Customer
    {
        public int CustomerID;
    }

    // pointer to native structure
    IntPtr pCustomer ...

    // marshal the struct
    Customer c = (Customer) Marshal.PtrToStructure(pCustomer, typeof(Customer));

    But if ur function f() only takes a pointer to a Customer object then the function prototype need only be like the following.

    [DllImport(....)]
    static extern void f(ref Customer);

  • maur0_cr0mer

    For the RasDial example you have to change the AdvancedMarshaler from the OpenNETCF Smart Device Framework v1.4

    Change method:

    public static void WriteString(BinaryWriter writer, string value, int size)
    {
    if (value != null)
    {
    int lByteSize = size * 2;
    byte[] result = new byte[lByteSize]; //UNICODE => BYTE
    byte[] bstring = System.Text.Encoding.Unicode.GetBytes(value);
    int i;

    for (i = 0; i < lByteSize; i++)
    {
    resultIdea = 0;
    }

    if (bstring.Length < lByteSize)
    {
    for (i = 0; i < bstring.Length; i++)
    {
    resultIdea = bstringIdea;
    }
    }
    else
    {
    for (i = 0; i < lByteSize; i++)
    {
    resultIdea = bstringIdea;
    }
    }

    writer.Write(result);
    }
    else
    {
    int lByteSize = size * 2;
    byte[] result = new byte[lByteSize]; //UNICODE => BYTE
    writer.Write(result);
    }
    }

  • pointers