Click to See Complete Forum and Search --> : Help Please! Save my SANITY!


Alex Peron
June 11th, 1999, 10:59 AM
Hello!

I'm new to windows programming,I writing a code using Visual C++ that seems very easy when I do it in DOS using Turboc++.
This is my goal, I like to play with my 16 L.E.D. lights which are driven by a PC card inserted inside my PC slot. The address I use is 0x300.
In DOS I can easily play with L.E.D. like this

outp(PortAddress,SomeData);//to "ON" L.E.D.
delay(500);
outp(PortAddress,AnotherData);//to "OFF"
delay(500);

Before I write this letter, I tried to crack my head for 3 days using below strategy.
The source code below is supposed to be my ladder step to play with my L.E.D..In this code I intent to beep the PC speaker for few seconds then stop it.I wrote a dll code which is Port.dll, inside the dll code are two functions, one function which
is OutPortWord is to out data to port address,the other one is Delay time.Also below is a source code I wrote to test this DLL code.But this code is having problems, can't run. I also studied,SetTimer(1,1000,NULL) and WM_TIMER but only creates lots of confusion on me.

Can anybody help me,Please!


//This is my DLL code, I named it Port.dll
//Port.cpp
#include <windows.h>
#include <conio.h>

//DllEntryPoint():The entry point of the DLL
//Declare DLL functions

int OutWordPort(unsigned short PortAddress, unsigned short Word);

int Delay(long duration);

BOOL WINAPI DllEntryPoint(HINSTANCE hDll,DWORD dwReason,
LPVOID Reserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
}
return TRUE;
}

int OutWordPort(unsigned short PortAddress, unsigned short Word)
{
_outpw(PortAddress,Word);
return 1;
}

int Delay(long duration)

{

Sleep(duration);
return 1;
}
//Port.def

//The DEF file for the Port.DLL DLL

LIBRARY port

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD SINGLE

EXPORTS
;The names of the DLL functions

OutWordPort
Delay


//Port.def

//The DEF file for the Port.DLL DLL

LIBRARY port

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD SINGLE

EXPORTS
;The names of the DLL functions

OutWordPort
Delay

//Port.def

//The DEF file for the Port.DLL DLL

LIBRARY port

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD SINGLE

EXPORTS
;The names of the DLL functions

OutWordPort
Delay
// TestPortDllDlg.cpp : implementation file
//

//THIS IS MY CODE FOR TESTING MY PORT.DLL
//I use AppWizard, Dialog-Based framework

#include "stdafx.h"
#include "TestPortDll.h"
#include "TestPortDllDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


HINSTANCE gLibMyDLL=NULL;
typedef int(*OUTWORDPORT)(unsigned short, unsigned short);
OUTWORDPORT OutWordPort;
typedef int(*DELAY)(long );
DELAY Delay;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestPortDllDlg dialog

CTestPortDllDlg::CTestPortDllDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestPortDllDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestPortDllDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestPortDllDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestPortDllDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestPortDllDlg, CDialog)
//{{AFX_MSG_MAP(CTestPortDllDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
ON_BN_CLICKED(IDC_BUTTON_SOUND, OnButtonSound)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestPortDllDlg message handlers

BOOL CTestPortDllDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//MY CODE STARTS HERE
gLibMyDLL=LoadLibrary("PORT.DLL");
if(gLibMyDLL==NULL)
{
char msg[300];
strcpy(msg,"Cannot load the Port.dll.");
MessageBox(msg);
}
Delay=(DELAY)GetProcAddress(gLibMyDLL,"Delay");
OutWordPort=(OUTWORDPORT)GetProcAddress(gLibMyDLL,"OutWordPort");

//MY CODE ENDS HERE

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CTestPortDllDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTestPortDllDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestPortDllDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTestPortDllDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
OnOK();
}

void CTestPortDllDlg::OnButtonSound()
{
// TODO: Add your control notification handler code here
/* if(gLibMyDLL!=NULL)
{
MessageBox("The Port.Dll has already been loaded.");
return;
}
*/
//BEEP THE SPEAKER
int SpeakerPort=0x0061;
OutWordPort(SpeakerPort,0x00FF);//beep the speaker
Delay(500);//allow speaker to beep for 0.5 sec
OutWordPort(SpeakerPort,0x00);//stop the speaker beep
}

Your Help will be greatly appreciated,

Alex