|
|
- how can i detect if the LAN cable is unlplugged, can i check the status of the connection?
try IsNetworkAlive() API
aks
|
|
|
|
|
There are alot of ways of determining the status of a network connection! All of the functions below will tell you the status of a network connection. Microsoft has provided us with many alternatives.
You could also use the IP Helper[^] tools to query the status something like this:
#include "Iphlpapi.h"
#pragma comment(lib, "iphlpapi.lib")
VOID PrintOperationalEthernet()
{
BYTE *pBuf=NULL;
DWORD dwSize=0;
DWORD dwResult=0;
BOOL bConnected=FALSE;
PMIB_IFTABLE pMIBTable;
CString str;
GetIfTable(NULL,&dwSize,FALSE);
pBuf=new BYTE[dwSize];
pMIBTable=reinterpret_cast<PMIB_IFTABLE>(pBuf);
if(NO_ERROR == GetIfTable(pMIBTable,&dwSize,FALSE))
{
for(UINT i=0; i < pMIBTable->dwNumEntries; ++i)
{
if(MIB_IF_TYPE_ETHERNET == pMIBTable->table[i].dwType)
{
bConnected = MIB_IF_OPER_STATUS_OPERATIONAL == pMIBTable->table[i].dwOperStatus;
char szBuf[MAX_PATH];
sprintf(szBuf,"%s is %s\n",pMIBTable->table[i].bDescr,
TRUE == bConnected?"Connected":"Offline");
TRACE(szBuf);
}
}
}
delete []pBuf;
}
You could also use the lower level DeviceIOControl[^] to query the device status directly from the network card like this:
#include <winioctl.h>
typedef ULONG NDIS_OID, *PNDIS_OID;
#define _NDIS_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD, request, method, FILE_ANY_ACCESS)
#define IOCTL_NDIS_QUERY_GLOBAL_STATS _NDIS_CONTROL_CODE(0,METHOD_OUT_DIRECT)
#define OID_GEN_MEDIA_CONNECT_STATUS 0x00010114
typedef enum _NDIS_MEDIA_STATE
{
NdisMediaStateConnected,
NdisMediaStateDisconnected
} NDIS_MEDIA_STATE,*PNDIS_MEDIA_STATE;
VOID PrintOperationalEthernet()
{
int index =0;
DWORD dwType =0;
HKEY key;
HKEY cardkey;
CString str;
HANDLE hDevice;
NDIS_OID OidCode = OID_GEN_MEDIA_CONNECT_STATUS;
ULONG status=0;
ULONG dwRet=0;
TCHAR szCardVal[CHAR_MAX];
TCHAR szDescription[MAX_PATH];
TCHAR szDevice[MAX_PATH];
str.Format(_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\"));
if(ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE,str,&key))
{
while(ERROR_SUCCESS == RegEnumKey(key,index++,szCardVal,CHAR_MAX))
{
str.Format(_T("%s\\%d"),str,_ttoi(szCardVal));
if(ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE,str,&cardkey))
{
str = str.Left(str.ReverseFind('\\'));
DWORD dwLen = CHAR_MAX * sizeof(TCHAR);
if(ERROR_SUCCESS == RegQueryValueEx(cardkey,_T("ServiceName"),NULL,
&dwType,(LPBYTE)szCardVal,&dwLen))
{
RegQueryValueEx(cardkey,_T("Description"),NULL,
&dwType,(LPBYTE)szDescription,&dwLen);
_stprintf(szDevice,_T("\\\\.\\%s"),szCardVal);
hDevice = CreateFile(szDevice,GENERIC_READ,FILE_SHARE_READ|
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
if(DeviceIoControl(hDevice,IOCTL_NDIS_QUERY_GLOBAL_STATS,
&OidCode,sizeof(NDIS_OID),&status,
sizeof(ULONG),&dwRet,NULL))
{
TCHAR szBuf[MAX_PATH];
_stprintf(szBuf,_T("%s is %s\n"),szCardVal,
NdisMediaStateConnected == status?_T("Connected"):_T("Offline"));
TRACE(szBuf);
CloseHandle(hDevice);
}
RegCloseKey(cardkey);
}
}
}
RegCloseKey(key);
}
}
You could also use the Internet Connection Firewall API[^] to determine the status of the network card such as below:
#include <netcon.h>
#define _WIN32_DCOM 1
typedef void(__stdcall *LPNcFreeNetconProperties)(NETCON_PROPERTIES *pProps);
VOID PrintOperationalEthernet()
{
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IDENTIFY,
NULL, EOAC_NONE, 0
);
HMODULE hmod=LoadLibrary(_T("netshell.dll"));
if(hmod)
{
LPNcFreeNetconProperties NcFreeNetconProperties=(LPNcFreeNetconProperties)\
GetProcAddress(hmod,"NcFreeNetconProperties");
INetConnectionManager *pMan=0;
HRESULT hres=CoCreateInstance(CLSID_ConnectionManager,0,CLSCTX_ALL,\
__uuidof(INetConnectionManager),(void **) &pMan);
if(SUCCEEDED(hres))
{
IEnumNetConnection *pEnum=0;
HRESULT hres=pMan->EnumConnections(NCME_DEFAULT,&pEnum);
if(SUCCEEDED(hres))
{
INetConnection *pCon=0;
ULONG count;
bool done=FALSE;
while(pEnum->Next(1,&pCon,&count) == S_OK && !done)
{
NETCON_PROPERTIES *pProps=0;
hres=pCon->GetProperties(&pProps);
if(SUCCEEDED(hres))
{
wchar_t wszBuf[MAX_PATH];
wsprintf(wszBuf,_T("%s is %s\n"),pProps->pszwName,
NCS_CONNECTED == pProps->Status?_T("Connected"):_T("Offline"));
TRACE(wszBuf);
NcFreeNetconProperties(pProps);
}
pCon->Release();
}
pEnum->Release();
}
pMan->Release();
}
FreeLibrary(hmod);
}
CoUninitialize();
}
As was previously mentioned the WMI Win32_NetworkAdapter Class[^] can be used to determine device state by checking the NetConnectionStatus member. Sorry... no WMI sample... I absolutely hate WMI.
Best Wishes,
David Delaune
|
|
|
|
|
If you've just typed all that, I'm impressed! (And skeptical!)
Good little batches of code.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Iain Clarke wrote:
If you've just typed all that, I'm impressed!
I actually created all three functions last night specifically for the reply. I typed them in VC2008 and then pasted them here. It is easier with intellisense and guarantees that the code is correct and compiles.
Best Wishes,
-David Delaune
|
|
|
|
|
best posts as always. Good work, keep it up
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
Hi,
Can some one provide me sample example for storing/retriving object of class from storage medium(i.e file)?
Mike.
|
|
|
|
|
|
I have a CString CString str= 104032;
how can i get substrings like 4032 or 32 or 04032 in vc++
thanks & regards
|
|
|
|
|
You can use Mid()of CString.
- ns ami -
|
|
|
|
|
l_d wrote: how can i get substrings like 4032 or 32 or 04032 in vc++
By reading here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
You can use Find().It will return the postion of the first character then use Mid()to extract that
Thanks & Regards
VIJITH VIJAYAN
|
|
|
|
|
Hi,
I have a pushbutton "Cancel" in my DialogBox that for some reason the Code doesn't get activated when I click on it
Actually the appearence seems give a hint of what the problem might be
When the Dialog comes up the button seems to have a perferation around it as if it is in a depressed state
I coded the DEFPUSHBUTTON as I have others but it does respond the same way below is the code for the Dialog
IDD_DIALOG6 DIALOGEX 0, 0, 316, 180
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Storage View"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
--> DEFPUSHBUTTON "Cancel",IDCANCEL1,205,159,50,14 <-- BUTTON IN QUESTION
CONTROL "",IDC_RICHEDIT21,"RichEdit20A",ES_MULTILINE | ES_WANTRETURN,80,28,180,105
LTEXT "Storage",IDC_STATIC,30,28,30,8
END
|
|
|
|
|
Did you override OnCancel() in your dialog class.
«_Superman_»
|
|
|
|
|
I am using plain old Win32 SDK
|
|
|
|
|
I just want you to check if you have mapped IDCANCEL and not destroying the dialog.
«_Superman_»
|
|
|
|
|
This the entire Dialog Code right below WM_INITDIALOG is the IDCANCEL1 case
I think the problem is the the Cancel button appears to be disabled meaning as if it has aleady been depressed (perferation around the button) Which is why in the INITDIALOG I do a BM_SETSTATE meesage
thankx again
case WM_INITDIALOG:
{
RichWindow = GetDlgItem(hwndDlg,IDC_RICHEDIT21);
SetFocus(RichWindow);
SendDlgItemMessage(hwndDlg, IDCANCEL1, BM_SETSTATE,0,0);
strncpy(&stor_start[0],&command[2],8); // copy command buffer
stor_start[8] = 0x00; // null mark
start_stor = atoi((const char *) &stor_start[0]); // convert to decimal
strncpy(&stor_end[0],&command[11],8); // copy over buffer
stor_end[8] = 0x00; // mark ens
end_stor = atoi((const char *) &stor_end[0]);
SendDlgItemMessage(hwndDlg, IDC_RICHEDIT21, EM_SETWORDBREAKPROC, 0, (LPSTR) EditWordBreakProc);
strcpy(&stor_char[0],&stor_start); // start at the begining
strncpy(&stor_buffer[0],&stor_start[0],8);
for (stor_count = start_stor; stor_count < end_stor; stor_count +=16)
{
memset( &cf, 0, sizeof cf );
cf.cbSize = sizeof cf;
cf.dwMask = CFM_COLOR;
cf.crTextColor = RGB(255,0,0);
cr.cpMin = 1;
cr.cpMax = 7;
SendDlgItemMessage(hwndDlg, IDC_RICHEDIT21, EM_EXSETSEL,0,&cr); // Set the Seelction
SendDlgItemMessage( hwndDlg, IDC_RICHEDIT21, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
strncpy(&stor_buffer[10],&buffer[buff_count],4);
buff_count+=4;
strncpy(&stor_buffer[15],&buffer[buff_count],4);
buff_count+=4;
strncpy(&stor_buffer[21],&buffer[buff_count],4);
buff_count+=4;
strncpy(&stor_buffer[26],&buffer[buff_count],4);
buff_count+=4;
stor_buffer[32]= '/r';
SendDlgItemMessage(hwndDlg, IDC_RICHEDIT21, WM_SETTEXT,0, (LPARAM) &stor_buffer[0]);
mysettext.flags = ST_DEFAULT;
mysettext.codepage = CP_ACP;
SendMessage(Stor_edit,EM_SETTEXTEX,&mysettext,(LPSTR) &stor_buffer[0]);
}
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
switch (LOWORD (wParam))
{
case IDCANCEL1:
DestroyWindow(hwndDlg);
hwndDlg = NULL;
break;
}
}
default:
return FALSE;
}
}
|
|
|
|
|
ForNow wrote: case WM_COMMAND:
switch (LOWORD(wParam))
{
switch (LOWORD (wParam))
{
case IDCANCEL1:
DestroyWindow(hwndDlg);
hwndDlg = NULL;
break;
}
}
If this is not a cut/paste error, remove one of these switch statements so that you have:
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDCANCEL1:
DestroyWindow(hwndDlg);
hwndDlg = NULL;
break;
}
Judy
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
I am going to look at my code at home as I when I got the a Answer to my post
I was on my way to work (rushed) but I beleive I only have one switch
thankz again
|
|
|
|
|
In addition to Judy's reply...
WM_COMMAND is used for more than just button clicks - it's also used for
other notifications from a control to its parent.
You may want something more like this:
switch (LOWORD (wParam))
{
case IDCANCEL1:
if (BN_CLICKED == HIWORD(wParam))
{
DestroyWindow(hwndDlg);
hwndDlg = NULL;
return 0;
}
break;
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thankx I'll try it when I get home BTW do you think the ES_WANTRETURN could be giving me problems with DEFPUSHBUTTON as with that style the Dialog takes control of the <enter> key which otherwise would cuase the DEFPUSHBUTTON to be activated
thankx again
|
|
|
|
|
ForNow wrote: do you think the ES_WANTRETURN could be giving me problems with DEFPUSHBUTTON
Possibly...
"ES_WANTRETURN
Specifies that a carriage return be inserted when the user presses
the ENTER key while entering text into a multiline edit control in a
dialog box. If you do not specify this style, pressing the ENTER key
has the same effect as pressing the dialog box's default push button.
This style has no effect on a single-line edit control. "
If the edit control has focus, hitting the enter key won't be
translated into a default push button press.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you Judy and Mark
Code is working
|
|
|
|
|
hi all
i am writing a thread per client server, well its not really a server, but it destributes jobs and calculations to other connected PCs.
i have been having this error
Free Heap block 00DB3D28 modified at 00DB3D78 after it was freed
Windows has triggered a breakpoint
now i traced the corruption of the heap by using _heapchk() and i found that the heap is being corrupted after a call to select() with fd_set containing one socket to check it for readability, the call to _heapchk() befor the select() returns _HEAPOK and after the select() it returns _HEAPBADNODE.
the call to select() is inside the client specific thread, the first client that connects has no problem at all, when a second client connects the select() works fine untill there are nothing to read from the socket, call select() and returns zero then the heap corrupts, how to solve such a problem where the code is not under my control???? i am using VC++ 2005 and win xp.
another thing, the thread proc is part of an object, that is creating a thread by calling a static function passing pointer to an object and inside the static function call a member function of object, dose that effect the heap in any way harmful??? i used to do it all the time with no problems.
here is the thread code:
thread(void* pParam)
{
int bytesSent,bytesToBeSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[4096] = "";
char recvbuf[4096] = "";
char tempbuf[4096] = "";
short* aa = (short*) &sendbuf[0];
*aa = PGA_CMD_SENDSTATE;
bytesToBeSent = 2;
bytesSent = send( socket, sendbuf, bytesToBeSent, 0 );
aa=NULL;
NotCritical = true;
int cmd,i,j,s,ret = 0;
TIMEVAL t;
t.tv_sec=0;
t.tv_usec=5000;
fd_set fd;
while( 1 )
{
FD_ZERO(&fd);
FD_SET(socket,&fd);
int hs2 = _heapchk();
ret = select(0, &fd, NULL, NULL, &t);
hs2 = _heapchk();
if ( ret > 0 )
{
s=0;
bytesRecv = recv( socket, recvbuf, 4096, 0);
if(bytesRecv == SOCKET_ERROR)
goto ERR;
cmd = *(short*)recvbuf;
j=2;
switch(cmd)
{
}
}
else
{
if( ret < 0)
goto ERR;
}
}
ERR:
closesocket(socket);
state |= CI_STATE_ERROR;
isBad = true;
return 0;
}
any ideas??????
if i comment out the line
i can connect as many clients i want with no problem, but the thread will not read any thing from the socket since ret is always zero.
Ali Usam AlCherchefchi
modified on Monday, January 26, 2009 8:06 PM
|
|
|
|
|
|