www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - help on windows

reply newbie <newbie dummy.com> writes:
hi all,

can anybody tell me how to translate a callback and the dialog stuff to D? 
i searched, but could not find a sample code on how to deal with that dialog
stuff.

appreciate any help

till

snip ...

define in header as :
#pragma pack( push, 8 )
#pragma comment( lib, "PServer" )
#define PSERVERLIB extern "C" __declspec( dllimport )

typedef BOOL (CALLBACK *ONCONNECTPROC)( PCONNECTION pConnection );


//###############################################################################################################//
BOOL CALLBACK OnConnect( PCONNECTION pConnection )
{
    InterlockedIncrement( &g_lOnConnectCount );

    // Allocate connection data buffer.
    pConnection->pvPerConnectionUserData = (PCONNECTION_DATA) malloc( sizeof(
CONNECTION_DATA ) );
    if( NULL == pConnection->pvPerConnectionUserData ){
        LogError( "malloc()", 0 );
        return FALSE;
        }

    PCONNECTION_DATA pConnectionData = (PCONNECTION_DATA)
pConnection->pvPerConnectionUserData;

    pConnectionData->dwBytesTransferred = 0;
    pConnectionData->dwDataSize         = 0;
    pConnectionData->dwIndex            = 0;
    pConnectionData->hFile              = INVALID_HANDLE_VALUE;

    // Receive request.
    if( PServer.PostRecv( pConnection, 
                          pConnectionData->bBuffer,
                          BUF_SIZE,
                          NULL ) != ERROR_SUCCESS ){ return FALSE; }

    return TRUE;
}
snip ...

//###############################################################################################################//
BOOL DlgMain_OnInitDialog( HWND hWnd, HWND hWndFocus, LPARAM lParam )
{
    g_hwndMain = hWnd;

    SetDlgItemInt( hWnd, ID_EDIT_PORT, 6060, FALSE );

    return TRUE;
}

//###############################################################################################################//
void DlgMain_OnTimer( HWND hWnd, UINT id )
{
    INFORMATION info = {0};

    PServer.QueryInformation( &info );

    SetDlgItemInt( hWnd, ID_STATIC_CPUUSAGE,            info.dwCpuUsage,       
       FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_WORKER_THREADS,     
info.dwWorkerThreadsCount,     FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_WORKER_THREADS_BUSY,
info.dwWorkerThreadsBusyCount, FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_WORKER_IOLEN,        info.dwIoCompletionLen,
       FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_USER_THREADS,       
info.dwUserThreadsCount,       FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_USER_THREADS_BUSY,  
info.dwUserThreadsBusyCount,   FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_USER_IOLEN,         
info.dwUserIoCompletionLen,    FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_CONNECTIONS,        
info.dwConnectionsCount,       FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_ONCONNECT,           g_lOnConnectCount,     
       FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_ONDISCONNECT,        g_lOnDisconnectCount,  
       FALSE );
    SetDlgItemInt( hWnd, ID_STATIC_ONREMOVECONNECTION, 
g_lOnRemoveConnectionCount,    FALSE );
}

//###############################################################################################################//
void DlgMain_OnClose( HWND hWnd )
{
    if( g_fStarted ){
        SendMessage( hWnd, WM_COMMAND, ID_BUTTON_STOP, NULL );
        }

	EndDialog( hWnd, 0 );
}

//###############################################################################################################//
int WINAPI Dialog_Main( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch( message ){
        Dlg_Proc( hWnd, WM_INITDIALOG, DlgMain_OnInitDialog );
        Dlg_Proc( hWnd, WM_COMMAND,    DlgMain_OnCommand    );
        Dlg_Proc( hWnd, WM_TIMER,      DlgMain_OnTimer      );
        Dlg_Proc( hWnd, WM_CLOSE,      DlgMain_OnClose      );
        }

    return 0;
}

//##############################################################################################################//
int WINAPI _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow )
{
    DialogBox( hInstance, MAKEINTRESOURCE( IDD_DIALOG_MAIN ), NULL, Dialog_Main
);

    return 0;
}

snip ...
Jun 05 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"newbie" <newbie dummy.com> wrote in message 
news:f44e81$1pg1$1 digitalmars.com...
 hi all,

 can anybody tell me how to translate a callback and the dialog stuff to D?
 i searched, but could not find a sample code on how to deal with that 
 dialog stuff.

Any callback functions that you send to the Windows API just have to be marked as extern(Windows). Furthermore the "CALLBACK" macro a lot of times doesn't mean anything, so for example: extern(Windows) BOOL OnConnect( PCONNECTION pConnection ) { ... } That's about it. Everything else maps pretty straightforwardly into D.
Jun 05 2007