Callback + unmanaged gode
От: Butia  
Дата: 17.01.07 09:13
Оценка:
Привет всем еще раз.
Извините что много кода, но иначе не объяснишь.
Есть кода на С++

    SWI_RCODE eRcode;
        //  open the API
        eRcode = SwiApiOpenEx(SWI_DEVICE_AC710, (U8*) NULL);
        
        // register the Callback function
        eRcode= SwiRegisterCallback(NotificationCallback);    
        
               //enable notification
               eRcode = SwiNotify(SWI_NOTIFY_Rssi, 3000);

        // informing modem of host startup state
        // this function will attach the modem onto the Network (in the event it's not)
        // modem needs to be on mobile network to send SMS
        eRcode = SwiSetHostStartup (TRUE, (TIMEOUT * 20));

где,

void NotificationCallback(SwiNotifyVariant *pNotify)
{
    CSMSOutboxSampleDlg* pThisObject = CSMSOutboxSampleDlg::Instance();
    pThisObject->HandleCallback(pNotify);
}


где,


void CSMSOutboxSampleDlg::HandleCallback(SwiNotifyVariant *pNotify)
{
    SWI_TYPE_Notify  eNotify = pNotify->eNotify;

    //used for debugging purposes, displays the notifications received
    TRACE(_T("Got notif %i\n"), eNotify);

    //Catch the info and post a message
    switch (eNotify) //add notifications here
    {
    
                 case SWI_NOTIFY_HeartBeat:
                   вывод messagebox
                    break;

                case SWI_NOTIFY_Rssi:
                    Console.WriteLine("SWI_NOTIFY_Rssi");
                    break;
                    default:
        break;
    }

}


где,

struct SwiNotifyVariant
{
    SWI_TYPE_Notify  eNotify;
    union
    {
    SWI_STRUCT_HeartBeat          _HeartBeat;
        SWI_STRUCT_NetworkStatus      _NetworkStatus;
        SWI_STRUCT_Rssi               _Rssi;
        .......
    }
typedef void (*SWI_CALLBACK_EX)(SwiNotifyVariant *pNotify);


struct SWI_STRUCT_NetworkStatus
{
    U32                     sizeStruct;            //size of this structure
    SWI_TYPE_ServiceStatus  eServiceStatus;        //status of service       енум
    SWI_TYPE_ServiceType    eServiceType;        //service type            енум
    SWI_TYPE_RoamingState   eRoaming;            //roaming state           енум
    SWI_TYPE_ModeSelection  eModeSelection;        //mode selection          енум
    TCHAR                   szCountry[5];        //country 
    TCHAR                   szNetwork[9];        //network
    U32                     nMCC;                //MCC
    U32                     nMNC;                //MNC
    U32                     nLAC;                //LAC
    U32                     nCellID;            //cell ID
};


В шарпе описание выглядит так
            SWI_RCODE rcode;
            //Register the Callback function
            rcode = SWI_API.SwiRegisterCallback(NotificationCallback);

            Console.WriteLine("SwiRegisterCallback return code : " + rcode.ToString("G"));


//где,

public void NotificationCallback(ref SwiNotifyVariant pNotify)
        {
            HandleCallback(ref pNotify);
        }
//где,
void HandleCallback(ref SwiNotifyVariant pNotify)
        {
            Console.WriteLine("HandleCallback");
            SWI_TYPE_Notify eNotify = pNotify.eNotify;

            // used for debugging purposes, displays the notifications received
            //TRACE(_T("Got notif %i\n"), eNotify);

            // Catch the info and post a message
            switch (eNotify) // add notifications here
            {
                case SWI_TYPE_Notify.SWI_NOTIFY_HeartBeat:
                    Console.WriteLine("SWI_NOTIFY_HeartBeat");
                    break;

                case SWI_TYPE_Notify.SWI_NOTIFY_Rssi:
                    Console.WriteLine("SWI_NOTIFY_Rssi");
                    break;

                default:
                    Console.WriteLine("default");
                    break;
            }
        }

//где,


[StructLayout(LayoutKind.Sequential)]
    public struct SwiNotifyVariant
    {
        public SWI_TYPE_Notify eNotify;
        public SwiNotifyVariantValue eNotifyValue;
    }
public delegate void SWI_CALLBACK_EX(ref SwiNotifyVariant pNotify);

[StructLayout( LayoutKind.Explicit)]
public struct SwiNotifyVariantValue
{
    [ FieldOffset( 0 )]
    public SWI_STRUCT_HeartBeat            _HeartBeat;
    [ FieldOffset( 0 )]
    public SWI_STRUCT_NetworkStatus     _NetworkStatus;
    [ FieldOffset( 0 )]
    public SWI_STRUCT_Rssi              _Rssi;
}
//где,
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
public struct SWI_STRUCT_NetworkStatus
{
    [FieldOffset(0)]
    public uint sizeStruct;            //size of this structure
    [FieldOffset(4)]
    public SWI_TYPE_ServiceStatus eServiceStatus;        //status of service
    [FieldOffset(8)]
    public SWI_TYPE_ServiceType eServiceType;        //service type
    [FieldOffset(12)]
    public SWI_TYPE_RoamingState eRoaming;            //roaming state
    [FieldOffset(16)]
    public SWI_TYPE_ModeSelection eModeSelection;        //mode selection
    [FieldOffset(20)]
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
    public string szCountry;
    [FieldOffset(25)]
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
    public string szNetwork;        //network
    [FieldOffset(34)]
    public uint nMCC;                //MCC
    [FieldOffset(38)]
    public uint nMNC;                //MNC
    [FieldOffset(42)]
    public uint nLAC;                //LAC
    [FieldOffset(46)]
    public uint nCellID;            //cell ID
}


Проблема в том что при вызове коллбак функции падает ошибка Could not load type 'Sierra.SWI_STRUCT_NetworkStatus' from assembly 'BulkSms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 25 that is incorrectly aligned or overlapped by a non-object field.

Пробовал переписывать структуру SWI_STRUCT_NetworkStatus как LayoutKind.Sequential, тогда падает ошибка в структуре SwiNotifyVariantValue.

Подскажите в чем ошибка плз.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.