| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Allowing user to choose DLL
I'm working on a game called The Prisoner's Dilemma. The outcome of the game is dictated generally by which strategy the computer uses to play. In the past few days, I wrote the program based around one hard-coded strategy, the Tit-for-Tat (TFT) strategy. Today, I decided to make the strategies modular. I removed the TFT code and compiled it into a DLL. Still, since I haven't written other strategies yet, I just left TFT hard-coded into the program with this line:
Code:
HINSTANCE hinstLib = LoadLibrary("strategies/TFT.dll");
That worked just fine, the DLL is doing just what I want it to do. But now I want to change the program so that the user selects which Strategy file to use. My problem is, however, that the program won't load the specified DLL. Here's my code: Code:
case 'P': // player wants to play
{
char dllFile[36];
importFunction Play;
bool win;
clrscr();
printf("Please enter the name of the strategy file to use.\n");
printf("This file must be in the strategies/ directory.\n");
printf("example: TFT.dll\n");
printf(">");
scanf("%s",dllFile);
HINSTANCE hinstLib = LoadLibrary(strcat("strategies\\",dllFile));
if(hinstLib == NULL) {
printf("\nERROR: unable to load %s\n",dllFile);
printf("Press any key to continue.\n");
tempvar = getch();
break;
}
Play = (importFunction)GetProcAddress(hinstLib, "Play");
if(Play == NULL) {
printf("\nERROR: %s is not a valid strategy file.\n",dllFile);
printf("Press any key to continue.\n");
tempvar = getch();
break;
}
FreeLibrary(hinstLib); // unload the DLL
win = Play(turn, maxturn, pscore, cscore, pavg, cavg);
if(win) {
printf("\nYou won the game!\n");
printf("Press any key to continue.\n");
tempvar = getch();
break;
}
else {
printf("\nYou didn't win the game.\n");
printf("Press any key to continue.\n");
tempvar = getch();
break;
}
break;
}
When running the program this code is in, I get a segmentation fault after typing in the file name TFT.dll and pressing enter. Any suggestions are appreciated, but I'd really like to find out what the most direct solution is (ie, why isn't that particular reference to LoadLibrary() working)...y'know, learning experience. Thanks a lot for taking a look! Oh, and, this is a console program. |
|
#2
|
||||
|
||||
|
Ok, I am not fluent with dll stuff but it doesn't seem right to unload the dll before you are calling its Play routine. Maybe that's not your problem but I would guess you have to unload the library after you've used it. GetProcAddress just gets the handle to the exported function. Hope this helps! Good luck
|
|
#3
|
|||
|
|||
|
Quote:
actually, I noticed that after I posted the code. Just one of those silly mistakes...but it didn't turn out to be the source of the problem. Just to make things a little easier, I moved the dll to the same directory as the exe and got rid of all that "strategies\\" stuff. Now the line is simply Code:
HINSTANCE hinstLib = LoadLibrary(dllFile); That solved my problem. ![]() |
|
#4
|
||||
|
||||
ok, so it was a path thing.. Good luck with your game, is it based on the 'original' prisoner's dilemma? I mean a sort of simulation of that concept or a whole game with the dilemma in it somehwere? |
|
#5
|
|||
|
|||
|
Quote:
There is a super version of LoadLibrary that is not dependent on the location of your DLL file in your disk.LoadLibrary has the restriction of DLL residing in same directory as your other files are. The super verion is called LoadLibraryEX(). |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Allowing user to choose DLL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|