How to get a message

I am using Visual C++ 6 Ent. and try to make a function handle a certain message. I want it to handle the WM_RASDIALEVENT message.
But it just wouldn't work. MSDN says I have to use the RasDial(...) function to tell it to send a message to my hWnd. That function, however, returns just "Phone book entry not found".

I included ras.h and linked rasapi32.lib, there are no errors or warnings when I compile. In the Messagemap of my window I added:
ON_REGISTERED_MESSAGE(RASDIALEVENT, OnRasDialEvent).

Help is very much appreciated!
maximulianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brain2000Commented:
In this example, RasDialFunc is my callback function, and dial() is my main function.  So you would call the dial() function like this:

dial("555-1212","logon","password");

This works hunky dory under Windows NT (and I believe Windows 98).  However, Windows 95 is another story.  You actually have to create a dial-up networking entry and put the phone number in it.  Let's say you call the Dial-up entry "testdial".  You could do the following:

dial("testdial","logon","password");

And even this sometimes acts flaky.  If you need to do dial ups with Windows 95, you're asking for trouble.

Another alternative is to download the lastest dial-up networking update from microsoft.com for Windows 95.  I believe this helps fix the problems.

Regardless though, here's the source code:

#include <windows.h>
#include <ras.h>

VOID WINAPI RasDialFunc(UINT msg, RASCONNSTATE st, DWORD err)
{
  int i;

  if(err)
  {
    dialcount++;
    printf("Redial %d\n",dialcount);
    if(!RasHangUp(hRas)) Sleep(3000);
    while(RasGetConnectStatus(hRas,&rasConnStatus)!=ERROR_INVALID_HANDLE) Sleep(0);

    if(dialcount > MAX_DIAL_ATTEMPTS)
    {
      RASdialing = 0;
      return;            
    }

    hRas=NULL;
    i = RasDial(NULL, NULL, &rdParams, 0, RasDialFunc, &hRas);
    if(i)
    {
      printf("error %d on rascon\n", i);
      return;
    }
  }
  if(st == RASCS_Connected)
  {
    printf("Setting connected flag\n");
    RASconnected = 1;
    RASdialing = 0;
  }
}

void dial(char *host, char *name, char *passwd)
{
  int i;

  if(RASconnected || RASdialing)
  return;  //already in use

  printf("Dial '%s' '%s' '%s'\n", host, name, passwd);
  dialcount = 0;      
  RASdialing = 1;

  memset(&rdParams, 0, sizeof(rdParams));
  rdParams.dwSize = sizeof(rdParams);
  if(host[0]>='0' && host[0]<='9')
    strcpy(rdParams.szPhoneNumber, host);
  else strcpy(rdParams.szEntryName, host);
  strcpy(rdParams.szUserName, name);
  strcpy(rdParams.szPassword, passwd);

  memset(&rdExtensions, 0, sizeof(rdExtensions));
  rdExtensions.dwSize = sizeof(rdExtensions);
  rdExtensions.dwfOptions = RDEOPT_IgnoreModemSpeaker |
RDEOPT_IgnoreSoftwareCompression |
RDEOPT_SetSoftwareCompression;
  rdExtensions.hwndParent = NULL;

  hRas=NULL;
  i = RasDial(&rdExtensions, NULL, &rdParams, 0, RasDialFunc, &hRas);
  if(i)
    printf("error %d on rascon\n", i);
}

void undial(void)
{
  if(RASconnected || RASdialing)
  {
    if(!RasHangUp(hRas)) Sleep(3000);
    RASconnected = 0;
    RASdialing = 0;
  }
}
0
maximulianAuthor Commented:
Thank you brain2000. My application is not supposed to dial up anything.
I want it to "know" when there is DUN Activity so I can track online time. I spyed on a few online timers developed in VB and saw that they are invoking a timer to check for activity.

I want my application to receive a windows message, I believe it is WM_RASDIALEVENT, but it wouldn't work. any suggestions?
0
Brain2000Commented:
Oh, if you want to get the WM_RASDIALEVENT message, you have to call the function like this:

RasDial(&rdExtensions, NULL, &rdParams, 0xffffffff, hWnd, &hRas);

At this point, it will poke WM_RASDIALEVENT messages into your main message queue instead of calling the RasDialFunc(...) function.

Does this help?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

maximulianAuthor Commented:
I am still having trouble. Some code nuggets (unimportant stuff cut out):

..h file:
BOOL OnRasDialEvent(WPARAM wParam, LPARAM lParam); // Function to handle RAS Event

..cpp file:
const UINT unRasMsg = RegisterWindowMessage( RASDIALEVENT );
BEGIN_MESSAGE_MAP(CDUNTimerDlg, CDialog)
//{{AFX_MSG_MAP(CDUNTimerDlg)
      ON_REGISTERED_MESSAGE(unRasMsg, OnRasDialEvent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CDUNTimerDlg::OnInitDialog()
{
RasDial(&rsDialExt, NULL, &rsDialParams, 0xffffffff, this->m_hWnd, &hrasConn);
}

BOOL CDUNTimerDlg::OnRasDialEvent(WPARAM wParam, LPARAM lParam)
{
MessageBox("Some RAS Activity occured...", "Internet Connection", MB_OK);
return TRUE;
}

When I start the program and make a connection, nothing happens (no dialog box, etc.) - info: using win98
0
Brain2000Commented:
Sorry, was out of the office yesterday so I didn't get to respond right away.

Try this instead:
const UINT unRasMsg = RegisterWindowMessage( RASDIALEVENT );
BEGIN_MESSAGE_MAP(CDUNTimerDlg, CDialog)
//{{AFX_MSG_MAP(CDUNTimerDlg)
ON_REGISTERED_MESSAGE(WM_RASDIALEVENT, OnRasDialEvent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

By calling RegisterWindowMessage(...) it creates a unique number that is most likely not WM_RASDIALEVENT.  Just to be sure though, do this:

BOOL CDUNTimerDlg::OnInitDialog()
{
  MessageBox(NULL,"OnInitDialog()","OnInitDialog()",MB_OK);
  if(unRasMsg!=WM_RASDIALEVENT)
    MessageBox(NULL,"ERROR","unRasMsg does not equal WM_RASDIALEVENT",MB_OK);
  RasDial(&rsDialExt, NULL, &rsDialParams, 0xffffffff, this->m_hWnd, &hrasConn);
}
0
chensuCommented:
ON_MESSAGE(WM_RASDIALEVENT, OnRasDialEvent)

LRESULT CDUNTimerDlg::OnRasDialEvent(WPARAM, LPARAM)
{
//...
}
0
maximulianAuthor Commented:
-- Points increased by 10 --

Thank you brain2000 & chensu

brain2000:

I tried your code but it didn't make it very far:

(a) ON_REGISTERED_MESSAGE(WM_RASDIALEVENT, OnRasDialEvent) results in Error C2101 ('&' in Constant)

(b) ON_MESSAGE(WM_RASDIALEVENT, OnRasDialEvent) compiles but your Error Message pops up, and the program does not receive the notification

(c) (a) + (b) = unRasMsg holds the unique message code (of CE2F) RegisterWindowMessage() returned. So i tried
ON_REGISTERED_MESSAGE(unRasMsg, OnRasDialEvent)
and

ON_MESSAGE(unRasMsg, OnRasDialEvent)

.... (of course the error message still pops up)...

I made the changes to the Message Handler Function as chensu suggested and placed a dumb dialog box in there just to test: nothing.

What could I be doing wrong?

0
chensuCommented:
Use Spy++ to check if the message is really sent.
0
joepaladinCommented:

All you really need to do to catch
a windows message is this:

ON_MESSAGE(WM_DISPLAYCHANGE, OnDisplayChange)

Just substitute your windows message
and declare the function

it should be within the message map,
but outside the comment :

//}}AFX_MSG_MAP
ON_MESSAGE(WM_DISPLAYCHANGE, OnDisplayChange)


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brain2000Commented:
Tell you what.  If you want to get this working, send me your .cpp file to brian@pcioh.com.  I'll spend a few minutes on it and e-mail it back.
0
chensuCommented:
joepaladin,

Please read the question history. What you suggest has been there.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.