app calling dll crashes

attached zip file is part of an online code. it is used to create a dll for
another exe app. copied its .dll and .lib to another project (mfc exe). crashes toward the
end. can't figure why this crashes.

code to call the dll:
typedef int (CALLBACK* FirstFunction)();
typedef int (CALLBACK* SecondFunction)(int&, int&);

void Cdyntest1Dlg::OnBnClickedButton1()
{
HINSTANCE hClcltr=LoadLibrary("dlltest.dll");
FirstFunction LPFF;
LPFF = (FirstFunction)GetProcAddress(hClcltr,"firstgo");

SecondFunction LPSF;
LPSF = (SecondFunction)GetProcAddress(hClcltr,"go");

int i;
int j;
LPFF();
LPSF(i,j);
}
[694 byte] By [teen1] at [2007-11-19 22:46:50]
# 1 Re: app calling dll crashes
Well:
Is 'hClcltr' valid after the call to 'LoadLibrary'?
Is 'LPFF' valid after the call to 'GetProcAddress'?
Is 'LPSF' valid after the call to 'GetProcAddress'?

Viggy
MrViggy at 2007-11-10 23:32:58 >
# 2 Re: app calling dll crashes
attached zip file is part of an online code. it is used to create a dll for another exe app. copied its .dll and .lib to another project (mfc exe). crashes toward the end. can't figure why this crashes.

code to call the dll:
typedef int (CALLBACK* FirstFunction)();
typedef int (CALLBACK* SecondFunction)(int&, int&);

void CMyDlg::OnBnClickedButton1()
{
HINSTANCE hClcltr=LoadLibrary("dlltest.dll");
FirstFunction LPFF;
LPFF = (FirstFunction)GetProcAddress(hClcltr,"firstgo");

SecondFunction LPSF;
LPSF = (SecondFunction)GetProcAddress(hClcltr,"go");

int i;
int j;
LPFF();
LPSF(i,j);
}
teen1 at 2007-11-10 23:33:58 >
# 3 Re: app calling dll crashes
Why create a new thread??

http://www.dev-archive.com/forum/showthread.php?t=390907

Viggy
MrViggy at 2007-11-10 23:35:01 >
# 4 Re: app calling dll crashes
Have you run it in the debugger?
GCDEF at 2007-11-10 23:36:07 >
# 5 Re: app calling dll crashes
As MrViggy already said, use debugger to check what going on and what api call is failing.

In addition, it is recommended to check ::LoadLibray() before calling ::GetProcAddress() and also check the return of ::GetProcAddress() before calling the function.

Cheers
golanshahar at 2007-11-10 23:37:01 >
# 6 Re: app calling dll crashes
Sorry. My browser was too slow that i did not think the first try went through. i am in remote village. anyway, this is error that i get when i run it in debugger:
Unhandled exception at 0x00000000 in callYdll.exe: 0xC0000005:
Access violation reading location 0x00000000.
teen1 at 2007-11-10 23:38:00 >
# 7 Re: app calling dll crashes
Sorry. My browser was too slow that i did not think the first try went through. i am in remote village. anyway, this is error that i get when i run it in debugger:
Unhandled exception at 0x00000000 in callYdll.exe: 0xC0000005:
Access violation reading location 0x00000000.

Read the other thread, you already been given an advice to check it with debugger.

Cheers
golanshahar at 2007-11-10 23:39:11 >
# 8 Re: app calling dll crashes
that was what i did. i started it with debugging option. i got Unhandled exception at 0x00000000 ,,, . not sure what causes it.
teen1 at 2007-11-10 23:40:09 >
# 9 Re: app calling dll crashes
Sorry. My browser was too slow that i did not think the first try went through. i am in remote village. anyway, this is error that i get when i run it in debugger:
Unhandled exception at 0x00000000 in callYdll.exe: 0xC0000005:
Access violation reading location 0x00000000.

You need to run the DLL in the debugger.
GCDEF at 2007-11-10 23:41:06 >
# 10 Re: app calling dll crashes
it crashes at the first exported function. can someone take a look at the attached code?. not fully familiar with debugging the dll process.
teen1 at 2007-11-10 23:42:12 >
# 11 Re: app calling dll crashes
it crashes at the first exported function. can someone take a look at the attached code?. not fully familiar with debugging the dll process.

You should learn then. It's no different from debugging an app. Load the DLL code and press debug. It'll ask for the name of the exe that calls the DLL.

Second option is run the calling app and put a breakpoint where it calls the DLL. Use F11 to step into the DLL code.
GCDEF at 2007-11-10 23:43:10 >
# 12 Re: app calling dll crashes
thanks. yes i knew it this way. i repeated the same steps. it crashes at the
second function. not sure why?.
dll code:
void CGClass::firstgo()
{
s = new Cderivedclass (2);
}
void CGClass::go(int& i, int& j)
{
s->push (1); <<-- based on debugger it crashes here.
s->push (2);

i = s->pop();
j = s->pop();
}
Unhandled exception at 0x00316dbb in callYdll.exe: 0xC0000005:
Access violation writing location 0x77fbb286.
teen1 at 2007-11-10 23:44:14 >
# 13 Re: app calling dll crashes
[ merged threads ]
cilu at 2007-11-10 23:45:10 >
# 14 Re: app calling dll crashes
not sure what type of memory access viloation is taking place.
teen1 at 2007-11-10 23:46:11 >
# 15 Re: app calling dll crashes
Well, keep going. What does s look like? Most likely it's null.
GCDEF at 2007-11-10 23:47:15 >
# 16 Re: app calling dll crashes
this works but could someone check for any error?.

CIIFoo is inherited from CIFoo.
dll returns a class:
void* CGClass::ReturnCIIFooInstance()
{
return static_cast< void* > (new CIIFoo);
}
---
//CIFoo is base class (abstract class).
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hdll = NULL;
CIFoo* FooPtr = NULL;
typedef void* (CALLBACK *pFuncP)();
pFuncP MyFoo;

hdll = LoadLibrary("dllt.dll");
MyFoo = (pFuncP)GetProcAddress( hdll, "ReturnCIIFooInstance" );

FooPtr = static_cast<CIFoo*>(MyFoo());

int i = 8;
FooPtr->SetNumber(i);
std::cout << "Foo::number is : " << FooPtr-> GetNumber() << std::endl;

delete FooPtr;
FooPtr = NULL;
FreeLibrary(hdll);
return 0;
}
couldn't fix the attached.
teen1 at 2007-11-10 23:48:14 >