|
Hi
I have an SQL query which is to be executed from the server. Just before executing, I am checking if the internet is available or not.
I am using InternetGetConnectedState api to implement this.
But this call returns true, for 2-3 seconds after the network connection is lost. This causes the application to hang.
Pls verify my code and check if it is the appropriate code
---------------------------------------------------
[DllImport("wininet.dll",
SetLastError=true,CharSet=CharSet::Unicode,ExactSpelling=true,
CallingConvention=CallingConvention::StdCall)]
static bool InternetGetConnectedState(int &Description, int ReservedValue ) ;
bool IsConnectedToInternet()
{
int Desc ;
return InternetGetConnectedState(Desc, 0 ) ;
}
if (IsConnectedToInternet())
{
mysql_query(mysqlConnectorObject,sqlQuery);
return mysql_store_result(mysqlConnectorObject);
}
else
{
MessageBox::Show("Not connected");
}
----------------------------------------------------
Thanks in advance
Anvesh
|
|
|
|
|
Hi,
InternetGetConnectedState() is the best way I know to look at the Internet connection state.
You are saying it shows a slightly outdated state, but that should not be too bad. Anyway,
the connection could be lost right after you call the function, so the code that follows must
survive that anyway. Hence, use asynchronous code (on a separate thread) and give whatever you
are trying to do a timeout.
|
|
|
|
|
Thank you very much.....
Can you pls tell me how to set the time limit when starting a new thread.
Can you also suggest the most suited class to do this? I mean whether it is backgroundworker, threadpool or thread class?
|
|
|
|
|
anveshvm wrote: how to set the time limit
you either perform calls that support a time limit, or you should launch a (one-shot) timer, which:
- you cancel again when the operation succeeds
- and if the timer fires, you stop the timer and cancel the thread
anveshvm wrote: backgroundworker, threadpool or thread?
whatever fits your circumstances best; since you may have to abort it, threadpool could not be the right choice though.
|
|
|
|
|
Thank you Luc Pattyn.... I will try that way.
|
|
|
|
|
|
I have written a code to check the return value of a message box. But it doesn't work .
My code is
--------------------------------------
DialogResult^ res = MessageBox::Show("Network connection failed.,"Error in connection",MessageBoxButtons::RetryCancel,MessageBoxIcon::Warning,MessageBoxDefaultButton::Button1);
if (res == System::Windows::Forms::DialogResult::Cancel)
{
Application::Exit();
}
else if (res == System::Windows::Forms::DialogResult::Retry)
{
continue;
}
---------------------------------------------------------
Here both the if condition fails...
In immediate window, the value of res is {...}
Pls check it
Thanks in advance
Anvesh
|
|
|
|
|
anveshvm wrote: DialogResult^ res
do you want the ^ there? DialogResult is a value type, isn't it?
|
|
|
|
|
Yeah.... I got it....
Thanks alot
|
|
|
|
|
you're welcome.
|
|
|
|
|
From inside the overriden virtual function of a derived class, how can I call the base class function. The C# equivalent is base.SomeVirtualFunction(); I need VC++ managed equivalent.
Thank you.
|
|
|
|
|
public ref class baseclass
{
public:
virtual void virtualmethod()
{
}
};
public ref class derivedclass : public baseclass
{
public:
virtual void virtualmethod() override
{
baseclass::virtualmethod();
}
};
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
You know it's really funny. C++/CLI forum is serving every other forum. All platform related things are being asked here taking the burden off C# & VB.net forums, and also the OO questions, freeing up the C++ forum
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
This board is so dead it makes me
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi, I had this code which worked beautifully in console mode, but now I am trying to convert the program into windows forms and the compiler returns the following error:
Error 1 error C3861: '_T': identifier not found
Any advice on how to solve this problem would be highly appreciated.
int GetHDDserialNo;
TCHAR volumeName[MAX_PATH];
DWORD volumeSerialNumber;
DWORD maxNameLength;
DWORD fileSystemFlags;
TCHAR systemName[MAX_PATH];
GetHDDserialNo = GetVolumeInformation
(
_T("c:\\"),
volumeName, MAX_PATH,
&volumeSerialNumber,
&maxNameLength,
&fileSystemFlags,
systemName, MAX_PATH
);
int SerialNumber = volumeSerialNumber;
|
|
|
|
|
J_E_D_I wrote: '_T': identifier not found
#include <tchar.h>
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks, I do not get that error anymore...but I get all those ones!
Error 1 error C2065: 'MAX_PATH' : undeclared identifier
Error 2 error C2065: 'DWORD' : undeclared identifier
Error 3 error C2146: syntax error : missing ';' before identifier 'volumeSerialNumber'
Error 4 error C2065: 'DWORD' : undeclared identifier
Error 5 error C2146: syntax error : missing ';' before identifier 'maxNameLength'
Error 6 error C2065: 'maxNameLength' : undeclared identifier
Error 7 error C2065: 'DWORD' : undeclared identifier
Error 8 error C2146: syntax error : missing ';' before identifier 'fileSystemFlags'
Error 9 error C2065: 'fileSystemFlags' : undeclared identifier
Error 10 error C2065: 'MAX_PATH' : undeclared identifier
Error 11 error C2065: 'maxNameLength' : undeclared identifier
Error 12 error C2065: 'fileSystemFlags' : undeclared identifier
Error 13 error C2065: 'systemName' : undeclared identifier
Error 14 error C2065: 'MAX_PATH' : undeclared identifier
Error 15 error C2078: too many initializers
|
|
|
|
|
What other header files are you including?
Those errors indicate windows.h is not included.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Actually, windows.h is already included in the .cpp file...
These are the other header files I've got:
#include "stdafx.h"
#include "Form1.h"
#include tchar.h
#include vector
#include ctime
#include math.h
#include fstream
#include string
#include iostream
#include sstream
using std::dec;
using std::hex;
#include iomanip
using std::setprecision;
using std::setbase;
#include cstdlib
using std::atof;
#include windows.h
#pragma comment(lib, "user32.lib")
#include stdio.h
#include cstdio
#include strsafe.h
using namespace Ex22_01;
(Note: I had to remove < and > from the directives as otherwise it would not show up in this message)
|
|
|
|
|
Hi,
it may come as a surprise, but the order of the includes matters too.
i.e. if tchar.h relies on windows.h, then windows.h must precede tchar.h;
normally if you suddenly need another include, you put it at the end of the list.
BTW: the behavior of message editing regarding < and > depends on the checked state of "Ignore HTML tags in this message" checkbox.
|
|
|
|
|
Luc, you are absolutely right! I've changed the order of the include directives and it works like a breeze.
Thank you so much for this, I admit I would have never thought about it.
|
|
|
|
|
You're welcome.
|
|
|
|
|
Hi again Luc, I was wondering if you had any hint on how to solve this problem which is somehow linked to the previous one. I need to save the value obtained with the previous code (which is a DWORD: DesiredValue) in a registry key and this was the syntax I was using (successfully) in console environment.
DWORD DesiredValue;
HKEY Xtmpkey;
DWORD dwDisp = 0;
LPDWORD xlpdwDisposition = &dwDisp;
DWORD dwVal = DesiredValue;
RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\MyPath", 0L,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &Xtmpkey,xlpdwDisposition);
RegSetValueEx (Xtmpkey, L"Desired_Value_Name", 0L, REG_DWORD,(CONST BYTE*) &dwVal, sizeof(DWORD));
Now that I am porting the software into C++/CLI environment it returns the following errors:
Error 1 error LNK2001: unresolved external symbol "extern "C" long __stdcall RegCreateKeyExW(struct HKEY__ *,wchar_t const *,unsigned long,wchar_t *,unsigned long,unsigned long,struct _SECURITY_ATTRIBUTES * const,struct HKEY__ * *,unsigned long *)" (?RegCreateKeyExW@@$$J236YGJPAUHKEY__@@PB_WKPA_WKKQAU_SECURITY_ATTRIBUTES@@PAPAU1@PAK@Z)
Error 2 error LNK2001: unresolved external symbol "extern "C" long __stdcall RegSetValueExW(struct HKEY__ *,wchar_t const *,unsigned long,unsigned long,unsigned char const *,unsigned long)" (?RegSetValueExW@@$$J224YGJPAUHKEY__@@PB_WKKPBEK@Z)
Error 3 fatal error LNK1120: 2 unresolved externals
What am I doing wrong?
I thank you in advance.
|
|
|
|
|
Hi,
there are two ways to solve this:
1.
most registry operations are supported in .NET by the Registry class
things not supported include special key types (not REG_SZ)
2.
when the Registry class does not offer what you need, you have to call the native functions (such as RegCreateKeyEx).
I don't know the details for C++, but you somehow must tell it where the library functions can be found,
and how they should be called.
This article[^] probably contains all you need and more.
FYI: in C# there is only one way to access native code, that is thru P/Invoke.
Remark: you would better have started a new thread for a question that hardly is connected to an old topic... that would have improved your chances for someone else to pick it up.
|
|
|
|
|
As Luc mentioned, the order of the #includes can be a factor.
Another recent post asked about tchar.h and I tested it - it can be
used all by itself and it pulls in any additional header files it needs,
so I wouldn't think that's an issue here..
In your stdafx.h file, you may be missing the Windows versioning macros.
The macros have changed a bit in recent SDK versions, but if you create a dummy
project (MFC or Win32) you can look at the stdafx.h file and see the defaults.
For example, here's a stdafx.h file from one of my projects, using the latest SDK
on VS 2008:
#pragma once
#define WIN32_LEAN_AND_MEAN
#include "<code>targetver.h</code>"
#include <stdio.h>
#include <tchar.h>
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#endif
#include <windows.h>
#pragma once
#ifndef NTDDI_VERSION
#define NTDDI_VERSION NTDDI_WINXPSP2
#endif
#ifndef WINVER
#define WINVER _WIN32_WINNT_WINXP
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#endif
#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410
#endif
#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|