Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

monkeyman

Need help with GetProcAddress and dlls..

This topic is 5853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey- I''m dynamically loading a dll and need to use GetProcAddress to get a function pointer to an exported function. The process is pretty straightforward and documented well, the problem is, it doesn''t appear to work with any exported function I''ve created. I tried creating a generic dll project that exported some symbols(to make sure I wasn''t doing something stupid), but GetProcAddress won''t pickup on the example exported function either. LoadLibrary works with no problems, so I''m guessing that the library is actually loaded and ready to go. Most of the sample code I see goes along the lines of: HMODULE hmod = LoadLibrary("library.dll"); GetProcAddress(hmod, "A_Function"); So I''m not sure exactly what I''m missing..I know how to typedef the functions I need after getting the function pointer and all that(and my code has more error checking than above, etc.), but I can''t get very far if: *this is successful HMODULE hmod = LoadLibrary("library.dll"); *and this fails immediately after GetProcAddress(hmod, "A_Function"); The error code is 127, which means the function was not found.. My next guess is that I''m exporting the function incorrectly from the dll, but I only have the MSDN examples showing me how to do this, so I must assume that it is correct..I''ve made dll''s before with no problem that export functions, but they were always used with a corresponding statically linked lib for the host application, and all the GetProcAddress, etc. is wrapped inside the static lib. I''ve done a search for problems associated with GetProcAddress, unfortunately the docs all assume that (a)GetProcAddress actually worked, and (b) you are having problems calling the function from the pointer. There is no mention of what to do when the library is loaded and the exported function is simply not found. There aren''t many more ways that I know of exporting a function from a dll, and I''ve never heard of anything you need to do differently when the dll will be dynamically loaded by the host application. As far as I know it''s just like making a regular dll, except you have to manually GetProcAddress, etc. I have no problems understanding how this is supposed to work, and if the sample applications provided with MSDN actually did what they are supposed to I wouldn''t have a problem with it. Either I am repeatedly doing something incorrectly(and there are not many places in the code I can mess this up), or the documentation is incorrect or incomplete. If anyone has done this before, please enlighten me. If I can get a single GetProcAddress to work and link my function pointers, I can do everything I need to from there. "Like all good things, it starts with a monkey.."

Share this post


Link to post
Share on other sites
Advertisement
Here's the definition of the target function(I'm starting over with a fresh project for clarity):

TESTDLL_API int fnTestDll(void);

This is untouched straight from the dll-wizard-generated files when you create a basic dll.

I just tried an 'extern "C"' and it worked

Too bad they couldn't put that somewhere in the documentation, that's the first mention I've seen of 'extern "C"' for this issue and I've been doing all kinds of reading through msdn about all things related to GetProcAddress.

Damn MS. Thanks LessBread.


"Like all good things, it starts with a monkey.."

[edited by - monkeyman on September 13, 2002 5:05:10 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by monkeyman
Too bad they couldn''t put that somewhere in the documentation, that''s the first mention I''ve seen of ''extern "C"'' for this issue and I''ve been doing all kinds of reading through msdn about all things related to GetProcAddress.



For the record I did just try a search of GetProcAddress and extern "C" together, and got several matches because of "C".

I browsed through the docs that came up, and although I could not find any specific mention that this was required for dynamically loaded dlls I did see a few code snippets that showed this being used in the dll header.

The scary part for me is that even if I had a clue that ''extern "C"'' might be involved in any way, and I searched for it, I still would not have been able to find any direct reference to how it applied to my particular situation.

Since the default dll project does not generate the header file correctly for dynamic dll loading it seems like it would be a good idea to mention this somewhere, I just can''t find where you''re supposed to see it.

Just out of curiosity LessBread where did you know about this from? I can imagine all sorts of scenarios where it might come up, but I can''t possible imagine anyone extracting this from the documentation alone(without lots of serious browsing).

Is it just me? I always seem to run into a brick wall when it comes to finding stuff in msdn.

"Like all good things, it starts with a monkey.."

Share this post


Link to post
Share on other sites
Um, well, I code mostly in C - but I run into a lot of C++ "disguised" to look like C - (joking). At any rate, a C compiler chokes on ''extern "C"'' - and so after watching the compiler gag on those a few times I looked around for an explanation. When it comes to language issues the C and C++ faqs are better places to start than MSDN. Granted, "GetProcAddress" is a MS thing and they should make reference of the mangling issue in the docs for the function, but they don''t. I''ve seen the question come up here are few times before - not often - maybe once every couple months or so. I''ve also looked into name mangling before too as part of my efforts helping out with PE Explorer. And I''ve had other reasons to go digging through the MS docs looking for explanations about esoteric things like calling conventions and the like. During those searches other items turn up too. The keyword to search on is "external linkage"

One last thing - ''extern "C"'' is well suited when you only have a few functions to export. When you have dozens of functions it''s easier to wrap the whole block of prototypes like so:

#ifdef __cplusplus
extern "C" {
#endif

// protos here

#ifdef __cplusplus
}
#endif

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!