C++/VB - Loading DLL

Asked By Denis on 31-May-07 06:20 AM
I made a dll in VC 2003 .
When I load this dll the first time using Loadlibrary API I get an error ,
but if I try to load the dll again the Loadlibrary work !
I tried also to load the dll using VB declaring the functions in a module
( and so without using the api Loadlibrary ) but I get the same problem. In
the first access to a dll function I get an error and the second time it
works.
I tried also in VC using the api LoadlibraryEx and if I disable the
entrypoint call it works , but if I compile the dll without and entrypoint
function and I try to load it using the Loadlibrary ( with the call to the
entrypoint) it does not work.
So my suspect is there is a problem in some global vars in the dll ,pheraps
a problem with some constructors, but I don't know how to identify it .
Any suggestion?
Above there is the code I use to load the dll.



int main(int argc, char* argv[])
{
void *h;
//h=LoadLibraryEx(EdtFile->Text.c_str(),0,1);
h=LoadLibrary("Test.dll");
LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

// Display the string.
MessageBox( NULL, (char *)lpMsgBuf, "GetLastError",
MB_OK|MB_ICONINFORMATION );

// Free the buffer.
LocalFree( lpMsgBuf );

ShowMessage(AnsiString((long)h));

//secondo tentativo---------------------------

h=LoadLibrary("Test.dll");
LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

// Display the string.
MessageBox( NULL, (char *)lpMsgBuf, "GetLastError",
MB_OK|MB_ICONINFORMATION );

// Free the buffer.
LocalFree( lpMsgBuf );

ShowMessage(AnsiString((long)h));

return 0;
}
//---------------------------------------------------------------------------


Thank you , Denis.


Arkady Frenkel replied on 31-May-07 07:13 AM
You use dll name without pass so it have to be in the same dir with exe ,
maybe that is your problem
Arkady
Ashot Geodakov replied on 31-May-07 06:19 PM
1. So what does GetLastError() return to you?
2. Each LoadLibrary() must be accompanied by FreeLibrary(), once you want to
release the library.
3. It's not the code that you posted that is of interest. It's what your DLL
does that's interesting. Can you post the DLL's code here?
Tim Roberts replied on 01-Jun-07 02:36 AM
This is wrong.  GetLastError() is only guaranteed to return something
meaningful if the last API failed.  You aren't checking whether LoadLibrary
actually fails, so the last error might be leftover from a long time ago.
Do this instead:

HINSTANCE h = LoadLibrary("test.dll");
if( !h )
{
LPVOID lpMsgBuf;
FormatMessage( ...
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Denis replied on 01-Jun-07 02:51 AM
Yes, I have the 2 files in the same dir and I tried also to specify the full
path.
Denis replied on 01-Jun-07 03:06 AM
Using the formatmessage I get an empty string as message.
The error number in decimal is 3221225617 and hex C0000091 ( I don't know
what it means )


Yes , but also using freelibrary I have the same behaviour


The dll code it is not so small there are about 3000 files of source code.
Another thing is that If I compile an exe instead of dll using the same
files all work fine.

Have you any suspect ?

Thank you, Denis.
Denis replied on 01-Jun-07 03:10 AM
Ok, this is a better writed code but when GetLastError() give me no error in
I obtain a message of no error .
Also using this code I have the same problem.

Thank you, Denis.
Ashot Geodakov replied on 01-Jun-07 01:42 PM
You mentioned earlier that only if you define DllMain() in your DLL, it will
misbehave.

So, perhaps it's what's in your DllMain that causes the issue? Can you post
at least the contents of DllMain?

Try loading any other good DLL using your code, for instance, ole32.dll. You
will see that your main exe code works just fine.
Ashot Geodakov replied on 01-Jun-07 02:21 PM
I forgot to mention that you can debug your DLL easily.

Just make it the main (startup) project in your solution and place a
breakpoint at your DllMain.

Run the debugger, and when it prompts you for the path to the executable
that loads that DLL, just point it to your exe.
Tim Roberts replied on 02-Jun-07 09:11 PM
STATUS_FLOAT_OVERFLOW.  Are you doing floating point arithmetic in the
DllEntryPoint routine?
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Denis replied on 04-Jun-07 09:55 AM
No, if don't define DllMain() I get the same error . I have no error is I
call LoadLibraryEx with the appropriate parameter so the LoadlibraryEx does
not call the DllMain but I think using this flag the Api also does not call
the constructors for global variables and for variables initializations.


This is the code of DllMain

BOOL APIENTRY DllMain( HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

//MessageBox(0,"test",0,0);

//NewBFInterval_();

switch (ul_reason_for_call)

{

case DLL_PROCESS_ATTACH:

case DLL_THREAD_ATTACH:

case DLL_THREAD_DETACH:

case DLL_PROCESS_DETACH:

break;

}

return TRUE;

}

Loading others dll all work fine.
Denis replied on 04-Jun-07 09:58 AM
I tried to debug the executable with the DllMain but I get the error before
the code enter inside to the DllMain function.
I tried also to debug in assembly mode and I saw what is the sub function
inside to the LoadLibrary Api that fails but it is difficult to understand
why .
Denis replied on 04-Jun-07 10:01 AM
Thank you Tim !!

I don't use floating point into the DllMain but I use it in the globals
variables .

Now I search for something bad in some declarations.
I suspect may be some 32 bit initialization with 64 bit define or something
like that .
Now I try to inspect .

Denis.
Ashot Geodakov replied on 04-Jun-07 01:57 PM
What I meant was, you do not debug the executable, but you debug the DLL
instead.

The DLL, not the executable, should be the *main* project in your solution.
huangjb replied on 05-Jun-07 09:02 PM
look at you dll's import dll, use "Exescope" or other, make sure your
app can access this dlls.