Dynamic Linking DLL problem 
Author Message
 Dynamic Linking DLL problem

I made a DLL and I load it succefully using static linked
dlls (by .lib). So the dll is ok.
Now I try to load it using dynamic linking. First I use
LoadLibrary and it returns to me a valid handle =
0x10000000
But when I get to GetProcAddress it returns NULL. The
function exists in the dll but it return NULL.
Here is the code I use to generate the dll:

print.cpp

#include <iostream.h>

__declspec(dllexport) void MyPrint()
{
cout << "Hello this is a DLL" << endl;

Quote:
}

And here is the loading code

main.cpp

#include <windows.h>

HINSTANCE handle;
typedef void (*FuncPtr)();

int main()
{
FuncPtr funcpointer;
handle=LoadLibrary("DLLBuilder.dll");

funcpointer=(FuncPtr)GetProcAddress(handle,"MyPrint");

funcpointer();
FreeLibrary(handle);
return 0;

Quote:
}

////
when it reach funcpointer() exit with an error (of course
because funcpointer == NULL). What I'm doing wrong
because handle is valid and "MyPrint"is the true name.
I heard that with some compilers I has to refer
the "MyPrint" as "_MyPrint" but it doesn't work any way.

did I miss something?

thanks in advance
   Frank



Fri, 09 Dec 2005 22:55:38 GMT  
 Dynamic Linking DLL problem

Quote:
> I made a DLL and I load it succefully using static linked
> dlls (by .lib). So the dll is ok.
> Now I try to load it using dynamic linking. First I use
> LoadLibrary and it returns to me a valid handle =
> 0x10000000
> But when I get to GetProcAddress it returns NULL. The
> function exists in the dll but it return NULL.
> Here is the code I use to generate the dll:

If you DLL is named foo.dll, enter this command at the prompt:

    dumpbin /exports foo.dll

That will list the names of all of the functions that are exported by the
DLL. It is one of these names that you need to pass to GetProcAddress().

This is problematic with member functions of C++ classes because the
compiler "decorates" names as a way to encode parameter and return value
info as a way to insure type-safety. If it is a member function that you are
trying to locate you might want to declare it as

    extern "C"
    {
        // Your declaration goes here
    }

Things are simpler if it is not a member function that you are trying to
export. All you usually need to do is specify the functions name in the
EXPORTS section of a module definition (.def) file.

Regards,
Will



Fri, 09 Dec 2005 23:22:16 GMT  
 Dynamic Linking DLL problem

Quote:
>    extern "C"
>    {
>        // Your declaration goes here
>    }

I try it and all seems to be Ok except because it doesn't
work any way.

Could any one make the smallest project to load a dll and
if possible to generate it too and email me.

Thanks in advance



Sat, 10 Dec 2005 01:07:15 GMT  
 Dynamic Linking DLL problem

Quote:
> I try it and all seems to be Ok except because it doesn't
> work any way.

I don't understand how it could seem to be OK and not work at the same time.

Quote:
> Could any one make the smallest project to load a dll and
> if possible to generate it too and email me.


Well, I don't have anything trivial lying around. Perhaps someone else does.

These groups work best when using a "self-help" model. That is, the original
poster tries something and posts about his experience. Another person reads
it and offers advice. Often the exchanges involve multiple people and
several posts. Ultimately a problem gets resolved and the original poster
profits from the experience.

I suggest that you post again showing

1) how you declared the exported function in the the executable that calls
the function
2) how you defined the exported function in the DLL being loaded
3) the results of the dumpbin command that I suggested

With that information any of the regular members around here would probably
be able to spot your error.

Regards,
Will



Sat, 10 Dec 2005 03:10:51 GMT  
 Dynamic Linking DLL problem
Hi thanks for your replies.

This is the program that I use to build the dll.

#include <iostream.h>

extern  "C" {
        __declspec(dllexport) void Print(void)

Quote:
}

void Print(void)
{
        cout << "It's working... ";

Quote:
}

And This is the program that actually try to load the dll

#include <windows.h>

HINSTANCE handle;

typedef void (*FuncPtr)();

int main()
{
        handle=LoadLibrary("MyFirstDLL.dll");
        FuncPtr funcpointer;

        funcpointer=(FuncPtr)GetProcAddress
(handle,"Print");

        funcpointer();
        FreeLibrary(handle);
        return 0;

Quote:
}

I could not call dumpbin with the dll because It said
that the vc has problems with the instalation and that a
reinstalation will solve the problem. But don't show me
any info about the dll.

Any way, both, the complete project and the dll can be
found at this link:
http://www.geocities.com/fpuig2003/Sources.zip



Sun, 11 Dec 2005 03:12:24 GMT  
 Dynamic Linking DLL problem

Quote:
> Hi thanks for your replies.

You are welcome.

Quote:
> This is the program that I use to build the dll.

> #include <iostream.h>

> extern  "C" {
> __declspec(dllexport) void Print(void)
> }

The declaration of Print() is missing a trailing semi-colon. Is that a typo
in your post or in the code?

Quote:
> I could not call dumpbin with the dll because It said
> that the vc has problems with the instalation and that a
> reinstalation will solve the problem. But don't show me
> any info about the dll.

Call me old fashioned, but that would raise alarm bells if it happened to
me. :-) I'd be wondering what all else might be suspect in the installation.
I'd address that problem first.

In any event, with the typo removed you should have a DLL that exports a
function named "Print" as I do here.

Quote:
> And This is the program that actually try to load the dll

> #include <windows.h>

> HINSTANCE handle;

> typedef void (*FuncPtr)();

> int main()
> {
> handle=LoadLibrary("MyFirstDLL.dll");
> FuncPtr funcpointer;

> funcpointer=(FuncPtr)GetProcAddress
> (handle,"Print");

> funcpointer();
> FreeLibrary(handle);
> return 0;
> }

This works here.

Regards,
Will



Sun, 11 Dec 2005 23:23:38 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. dynamic linked dll

2. Plug-in style dynamic dll linking security issues.

3. Writing Dynamic Link Libraries (.DLL's)

4. ATL.DLL - static or dynamic linking

5. Plug-in style dynamic dll linking security issues.

6. Dynamic Linking of Ole32.dll

7. A dynamic link library (DLL) initialization routine failed.

8. Dynamic Link Library(DLL)

9. Dynamic linking to MFC42.dll

10. Plug-in style dynamic dll linking security issues.

11. MFC40.dll Dynamic Linking question?

12. Allocating memory in a regular DLL (linked dynamic to MFC)

 

 
Powered by phpBB® Forum Software