tags:

views:

59

answers:

4

I have DLL and application that will call some function in this dll. For example...

DLL function:

char const* func1() 
{ 
    return reinterpret_cast<char const*>(0x11223344); 
}

Application code:

func1 = reinterpret_cast<Func1Callback>(::GetProcAddress(hDll, "func1"));
blablabla
char const* ptr = func1();
cout << ptr;

That DLL is not under my control (plugin).. Same code will cause access violation in my application, so... Is there any mechanism that will allow to determine such errors?

+1  A: 

The dll is loaded into your process address space. If it accesses some invalid memory location your process will crash. I don't see any way around it other than not using this dll at all.

Naveen
i search a way which allow for example print some message when dll returns illegal pointer or doing another strange things...
ProgramWriter
A: 

If a DLL is returning junk memory addresses to your application, I'd say you shouldn't be using it, because it isn't performing its intended/desired function.

Also, don't try to guard against it by using the Win32 IsBad*Ptr class of functions, as the MSDN documentation states (see also this post by Raymond Chen for a good description of why not).

slyfox
A: 

You can enclose function call into try catch block, it'll help not to crush entire application right at function call, but nobody can garantee, that dll loaded will not alter memory somwhere in your code, in this case yo'll get hard to debug crash in other place. So you can probably raise some protection for just access violation, but anyway cannot fully protext you application for some side effects.

drlazy
+1  A: 

Since the DLL can do anything your program could do the only reliable way is to load it into a separate worker lightweight process and once anything bad happens just restart the process. You'll need some protocol to pass data into the worker process and receive results.

sharptooth