Discussion:
calling (Exported) Exe Function
(too old to reply)
Mayur
2006-09-25 13:04:39 UTC
Permalink
Hello All
I am able to call the function exported by Exe from a dll file
im getting the address of the function but whille calling that function im
getting access violation Error as bellow my code



Function Exported by EXE
void __declspec(dllexport) ExeFn()
{
MessageBox(NULL,"hi","From Exe",MB_OK);
}


Function call from Dll

HMODULE hModule;
LPTSTR modname;
hModule=GetModuleHandle("Placeholder.exe");
if (hModule == NULL)
{
hModule=(HMODULE)LoadLibrary ("Placeholder.exe");
}

FnPtrT FnPtr = (FnPtrT)::GetProcAddress((HMODULE)hModule, "ExeFn");
if(FnPtr)
{
MessageBox(NULL,(LPCTSTR)FnPtr,"Exec",MB_OK);
(*FnPtr)();
}



Followinf Error

Unhandled exception at 0xfffffff9 in Mayur.dll 0xC0000005: Access violation
reading location 0xfffffff9.
Norman Bullen
2006-09-26 14:01:15 UTC
Permalink
Post by Mayur
Hello All
I am able to call the function exported by Exe from a dll file
im getting the address of the function but whille calling that function im
getting access violation Error as bellow my code
Function Exported by EXE
void __declspec(dllexport) ExeFn()
{
MessageBox(NULL,"hi","From Exe",MB_OK);
}
Function call from Dll
HMODULE hModule;
LPTSTR modname;
hModule=GetModuleHandle("Placeholder.exe");
if (hModule == NULL)
{
hModule=(HMODULE)LoadLibrary ("Placeholder.exe");
}
FnPtrT FnPtr = (FnPtrT)::GetProcAddress((HMODULE)hModule, "ExeFn");
if(FnPtr)
{
MessageBox(NULL,(LPCTSTR)FnPtr,"Exec",MB_OK);
(*FnPtr)();
}
Followinf Error
Unhandled exception at 0xfffffff9 in Mayur.dll 0xC0000005: Access violation
reading location 0xfffffff9.
Make sure the declarations of ExeFn() and FnPtr, particularly with
respect to calling sequence (__cdecl vs. __stdcall).

In the code shown, I can see that the declarations don't match; ExeFn()
is declared void yet a value is expected to be returned from FnPtr.

Does the error occur before or after the message box is displayed by
ExeFn()?

Norm
--
--
To reply, change domain to an adult feline.
Vladimir Scherbina
2006-09-27 08:19:52 UTC
Permalink
Mayur,

You do export the function from *exe* file, correct? The problem is: loader
does not set the correct address in import table for a exe file when you
load it via LoadLibrary, it means that the code of your function in exe file
will looks like:

...
push ...
push ...
push ...
push ...
call MessageBox_impl_address
...

and the MessageBox_impl_address points to a wrong location. Use dll's to
export functions from.

-
Vladimir
Post by Mayur
Hello All
I am able to call the function exported by Exe from a dll file
im getting the address of the function but whille calling that function im
getting access violation Error as bellow my code
Function Exported by EXE
void __declspec(dllexport) ExeFn()
{
MessageBox(NULL,"hi","From Exe",MB_OK);
}
Function call from Dll
HMODULE hModule;
LPTSTR modname;
hModule=GetModuleHandle("Placeholder.exe");
if (hModule == NULL)
{
hModule=(HMODULE)LoadLibrary ("Placeholder.exe");
}
FnPtrT FnPtr = (FnPtrT)::GetProcAddress((HMODULE)hModule, "ExeFn");
if(FnPtr)
{
MessageBox(NULL,(LPCTSTR)FnPtr,"Exec",MB_OK);
(*FnPtr)();
}
Followinf Error
Unhandled exception at 0xfffffff9 in Mayur.dll 0xC0000005: Access violation
reading location 0xfffffff9.
Loading...