Solved

LoadLibrary() from another DLL

Posted on 2003-11-17
7
596 Views
Last Modified: 2010-04-01
I'm writing an set of ID 'tags' in dll form for the beta testers of my Half-Life mod (so if they leak my beta i'll know who it was) but i'm running into a problem.

i'm running this code:

HMODULE idlib = LoadLibrary("mod\\dlls\\id");
if(idlib == NULL)
{
      strcpy(szRejectReason, "Unable to load id.dll");
      return FALSE;
}

the library loads correctly at this point.
i tried to get a pointer to the one and only function in the dll with this code:

typedef char* (*MYPROC)(void);

MYPROC func;
func = (MYPROC)GetProcAddress(idlib, "ID_GetTesterName");

but func is always null after this call, so I'm unable to get the Tester's ID.
So, I'm reduced to only making the id.dll be present to run the game; I had planned on only allowing one player with each ID in a multiplayer game at once (during the beta phase, anyway) so Joe can't share the mod with his friends.

Now, I may have had the typedef wrong in my original code, but would that have made func null?
I will try it once more in a moment, but i'd appreciate any help.
(I'd link the dll i'm compiling with id.lib at compile-time but then id.dll would have to be in the same folder as hl.exe, and I want it at the least in the mod folder.)
thanks
HeronBlademaster
0
Comment
Question by:heronblademaster
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 9768365
It seems that you are a victim of C++ name decoration. Be sure to declare 'ID_GetTesterName()' as

extern "C" __declspec(dllexport) char* ID_GetTesterName ( void);

If you are still experiencing troublen, use

dumpbin.exe /exports id.dll

to see what functions actually are listed in the export table.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9768371
BTW, a sure way to ensure that these problems do not occur is using .def files, e.g.

LIBRARY      ID
DESCRIPTION  'extension for my program'

EXPORTS
    ID_GetTesterName
0
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 100 total points
ID: 9768394
You are either not exporting the function, or it's name has been decorated.  You can use DEPENDS.EXE or maybe DUMPBIN to find out.  In the DLL that exports, use this in the source code:

__declspec(dllexport) void PASCAL ID_GetTesterName(void)
{
      return( 1234 );
}

Your .DEF file for that DLL, should look something like this:
=--==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; ID_DLL.def : Declares the module parameters for the DLL.

LIBRARY      "ID"
DESCRIPTION  'ID DEF Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
ID_GetTesterName     @1
=--==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

That should fix you up, unless I misunderstand the question.

-- Dan
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:heronblademaster
ID: 9768444
thanks, jkr, that extern "C" thing did it.
and to Dan (which is my name too :D) thanks too, i'll do that from now on
i gave jkr more points because he gave me a more immediate solution
thanks again all
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9768559
jkr is just a pointhound on a summit-push for his C++ 'Genius' certificate.  You should ignore anything he says. :-))))

-- Dan
0
 

Author Comment

by:heronblademaster
ID: 9768566
lol
0
 
LVL 86

Expert Comment

by:jkr
ID: 9768605
Hum, if so, I better take care of my W2k cert, that's just 7k away :o)
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question