A
aetdata
I have an application in Java accessing a VC++ dll for getting the text
in a Internet Explorer Server component. I access the dll frequently
once each second to get the latest content. It usually works fine and
does what I want but sooner or later it crashes the program completely.
I have tried to isolate the crash but can not find exactly what is
going wrong. I also use some try..catch blocks and test all return
values, but nothing seem to help. I am getting quite desperate by now
so any help would be greatly appreciated!
Code as follows, I call the
Java_poker_jni_ipoker_IPTHandler_getOutput() method once each second
from the Java side, when it crashes I get the message "Exception
thrown 1", but nothing else...
JNIEXPORT jstring JNICALL
Java_poker_jni_ipoker_IPTHandler_getOutput(JNIEnv *env, jobject obj){
try {
LPTSTR str;
//init COM/OLE
if(CoInitialize( NULL ) != S_OK)
MsgBoxA("COM NOT INITED OK!");
else{
str = getText();
}
if(str == NULL){
MsgBoxA("STR = NULL");
str = "";
}else if(str == ""){
MsgBoxA("STR = ''");
}else if(!str){
MsgBoxA("NOT STR");
}
jstring res = env->NewStringUTF(str);
CoUninitialize();
//delete str;
return res;
} catch(...) {
LPTSTR str = "";
MsgBoxA("Exception thrown 1");
jstring res = env->NewStringUTF(str);
CoUninitialize();
return res;
}
}
LPTSTR getText(){
try {
CComPtr<IHTMLDocument2> spDoc;
CComPtr<IHTMLElement> pHTMLElement;
CComBSTR bstrText;
LPTSTR str = "";
HRESULT hr;
hr = GetHTMLDocument( chatWindow, spDoc );
if ( SUCCEEDED(hr) ){
hr = spDoc->get_body(&pHTMLElement);
if ( SUCCEEDED(hr) ){
hr = pHTMLElement->get_innerText(&bstrText);
if( SUCCEEDED(hr) ){
str =(char *)_bstr_t(bstrText);
}
}
}
//clean up
pHTMLElement.Release();
spDoc.Release();
return str;
} catch(...) {
MsgBoxA("Exception thrown 2");
}
return "";
}
HRESULT GetHTMLDocument( HWND hwnd, CComPtr<IHTMLDocument2> &spDoc )
{
try{
TCHAR lpszClass[512];
HRESULT hr;
if ( ::GetClassName(hwnd,
lpszClass,(sizeof(lpszClass)/sizeof(TCHAR))-1) == 0 ){
return E_INVALIDARG;
}
if ( _tcscmp(lpszClass, _T("Internet Explorer_Server")) == 0 ){
LRESULT lRes;
::SendMessageTimeout( hwnd, nMsgGetHTML, 0L, 0L,
SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
if ( pfObjectFromLresult != NULL ) {
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2,
0, (void**)&spDoc );
CHECK_HR( hr );
}
}
else
{
hr = E_INVALIDARG;
}
//delete[] lpszClass;
return hr;
} catch(...) {
MsgBoxA("Exception thrown 3");
}
return E_INVALIDARG;
}
in a Internet Explorer Server component. I access the dll frequently
once each second to get the latest content. It usually works fine and
does what I want but sooner or later it crashes the program completely.
I have tried to isolate the crash but can not find exactly what is
going wrong. I also use some try..catch blocks and test all return
values, but nothing seem to help. I am getting quite desperate by now
so any help would be greatly appreciated!
Code as follows, I call the
Java_poker_jni_ipoker_IPTHandler_getOutput() method once each second
from the Java side, when it crashes I get the message "Exception
thrown 1", but nothing else...
JNIEXPORT jstring JNICALL
Java_poker_jni_ipoker_IPTHandler_getOutput(JNIEnv *env, jobject obj){
try {
LPTSTR str;
//init COM/OLE
if(CoInitialize( NULL ) != S_OK)
MsgBoxA("COM NOT INITED OK!");
else{
str = getText();
}
if(str == NULL){
MsgBoxA("STR = NULL");
str = "";
}else if(str == ""){
MsgBoxA("STR = ''");
}else if(!str){
MsgBoxA("NOT STR");
}
jstring res = env->NewStringUTF(str);
CoUninitialize();
//delete str;
return res;
} catch(...) {
LPTSTR str = "";
MsgBoxA("Exception thrown 1");
jstring res = env->NewStringUTF(str);
CoUninitialize();
return res;
}
}
LPTSTR getText(){
try {
CComPtr<IHTMLDocument2> spDoc;
CComPtr<IHTMLElement> pHTMLElement;
CComBSTR bstrText;
LPTSTR str = "";
HRESULT hr;
hr = GetHTMLDocument( chatWindow, spDoc );
if ( SUCCEEDED(hr) ){
hr = spDoc->get_body(&pHTMLElement);
if ( SUCCEEDED(hr) ){
hr = pHTMLElement->get_innerText(&bstrText);
if( SUCCEEDED(hr) ){
str =(char *)_bstr_t(bstrText);
}
}
}
//clean up
pHTMLElement.Release();
spDoc.Release();
return str;
} catch(...) {
MsgBoxA("Exception thrown 2");
}
return "";
}
HRESULT GetHTMLDocument( HWND hwnd, CComPtr<IHTMLDocument2> &spDoc )
{
try{
TCHAR lpszClass[512];
HRESULT hr;
if ( ::GetClassName(hwnd,
lpszClass,(sizeof(lpszClass)/sizeof(TCHAR))-1) == 0 ){
return E_INVALIDARG;
}
if ( _tcscmp(lpszClass, _T("Internet Explorer_Server")) == 0 ){
LRESULT lRes;
::SendMessageTimeout( hwnd, nMsgGetHTML, 0L, 0L,
SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
if ( pfObjectFromLresult != NULL ) {
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2,
0, (void**)&spDoc );
CHECK_HR( hr );
}
}
else
{
hr = E_INVALIDARG;
}
//delete[] lpszClass;
return hr;
} catch(...) {
MsgBoxA("Exception thrown 3");
}
return E_INVALIDARG;
}