----------
URGENT: Need that sample program on calling functions in 32-bit DLL
EXPLICITLY using LoadLibrary/GetProcAddress.
I have a situation where I have to access a Fortran developed
mathematical model into it's corresponding C++ framework. Needless to
say the FORTRAN is Microsoft's, the C++ Framework developed under
Borland 4.53.
Under 16 bit, a framework was developed to handle the loading of any
given DLL and EXPLICITlty calling a routine from the DLL via a call to
'GetProcAddress'. This works fine in my 16 bit world. The trouble is
that I now have 'Borland 5.01 with Design Tools' and yes I WANT to use
it.
When I port to the 32 bit world everything seems to come over quite fine
but I'm having a wicked time with the address returned from
'GetProcAddress'. While the value seems reasonable, when it gets called,
the de{*word*81} stops the process saying that the thread has generated an
access violation. When I look at the resulting code all I get is this
block of nulls (or na-na land). It seems to have either jumped into a
place where it should not have or there is code suppose to be there that
aint.
Has anybody, anywhere in there software respositories an C++, SDK or WIN
32 API example of a LoadLibrary / GetProcAddress call demonstrating an
EXPLICIT call to a DLL function (with or without parameters) working in
a 32 bit environment.
Here's the code for a (hopefully better idea as to what I'm after).
This really shouldn't be that hard to do and is CRITICAL to my going
about this like I want to and should be about to.
===============================================
-------- StdLinkLibWin Framework code ---------
Remember, this works fine in 16 bit
-----------------------------------------------
void StdLinkLibWin::Load()
{
heap DWORD hn = 0;
// SetErrorMode(SEM_NOOPENFILEERRORBOX);
if ( !IsLoaded() ) {
iw_HInst = (word) LoadLibrary( GetLib().GetStr() );
if ( iw_HInst > (word)HINSTANCE_ERROR ) {
SetStatus( 0 );
SetLoaded( true );
}
else {
hn = GetLastError();
SetStatus( (word)hn );
throw StdLinkLib::BadLoad();
}
}
void StdLinkLibWin::Free()
{
if ( IsLoaded() )
if ( iw_HInst > (word)HINSTANCE_ERROR )
FreeLibrary( (HINSTANCE)iw_HInst );
iw_HInst = 0;
SetLoaded( false );
----- Pay paticular attention to the line defining 'lpfn_LibRtn' and the
----- '(FARPROC)' cast. It's my best guess as to why this isn't working
----- but there again, your the expert.
void StdLinkLibWin::Call( const char* poc )
{
heap int (FAR *lpfn_LibRtn) ();
heap HMODULE hw = (HMODULE) iw_HInst;
(FARPROC) lpfn_LibRtn = GetProcAddress( hw, poc );
if ( !lpfn_LibRtn )
throw BadCall();
else {
DirEntry dt_Old, dt_New( GetDir() );
dt_Old.ToAbs();
if ( IsUseDir() )
dt_New.SetCWD();
SetStatus( (*lpfn_LibRtn)() );
if ( IsUseDir() )
dt_Old.SetCWD();
}
==============================================================================
--- The following is the test code I use to set up and test the
Framework ----
void TestLoadLib()
{
heap StdLinkLibWin ht( "models\\LdrFrmWk\\noname.dll", false ), ht2(
ht );
if ( ht == ht2 )
;
ht = ht2;
if ( ht == ht2 )
;
try {
StdLinkLib *pdt = new StdLinkLibWin(
"MODELS\\YASSIM11\\yassim32.dll" );
// pdt->Free();
delete pdt;
StdLinkLibWin ht3( "MODELS\\YASSIM11\\yassim32.dll" );
// ht3.Load();
ht3.Call( "EXECUTE" );
if ( ht3.GetStatus() == 7 ) {
StdLinkLibWin dt( "MODELS\\YASSIM11\\yassim32.dll" );
dt.Call( "EXECUTE" );
dt.Call( "EXECUTE" );
dt.Call( "EXECUTE" );
dt.Call( "EXECUTE" );
for ( int di=0; di<20; di++)
dt.Call( "EXECUTE" );
if ( dt.GetStatus() == 0 ) {
dt.Free();
dt.Load();
dt.Call( "MUL" );
}
}
}
catch ( StdLinkLib::Error ) {
cout << "StdLinkLib::Error" << endl;
}
catch ( ... ) {
cout << "Something else went wrong" << endl;
}
=======================================================================================
--- The following is the DEF file used by Developer Studio to generate
the 32 bit DLL
--- Maybe I'm suppose to specify STACKSIZE or something, I just don't
have an reasonable
--- example to go by.
;
; The LIBRARY statement below 'MUST' be the the name of the resulting
DLL.
;
; Use the DESCRIPTION field to provide information concerning
; identification and version control.
;
; Comment out the EXECUTE line and uncomment the RUN line if you wish to
use
; the SUBROUTINE approach as mentined in LDRFRMWK.FOR.
;
; Dont change anything else unless you know what your at.
;
LIBRARY YASSIM32
DESCRIPTION 'SPS YasSim 1.1 (32-Bit) - CORETEC Adaptive Model - 0x0110'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE SINGLE
HEAPSIZE 1024
EXPORTS
EXECUTE
==============================================================================================
Like I said, I think the best way you might be able to help me is to
send me a short example
a DLL function being EXPLICITLY called (as opposed to IMPLICITly via the
_export/_import/.lib
approach.
Could really use your help. This seems so simple and therefore is
driving me nuts becouse I
can't get it to work.
Thanks in advance.
------------------------------------------------------------------------------------
Chris,
The best work around I've come up with to date is to either try to do a
IMPLICIT link or consider the purchase of Visual C++ (which will
probably become a reality in any regard). However, I don't like work
arounds. If there is anything you can do, it would much appreciated.
Thanks in advance.
------------------------------------------------------------------------
Perry S. Anderson
Senior Systems Analyst
CORETEC Incorporated
mailto:core...@voyager.newcomm.net
http://www.newcomm.net/coretec
=========================================