C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 28th, 2006, 02:26 AM
vermin1302 vermin1302 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 vermin1302 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 27 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old February 28th, 2006, 03:20 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 757 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 4 m 56 sec
Reputation Power: 4
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

Reply With Quote
  #3  
Old February 28th, 2006, 03:27 AM
vermin1302 vermin1302 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 vermin1302 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
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


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.

Reply With Quote
  #4  
Old February 28th, 2006, 04:35 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 757 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 4 m 56 sec
Reputation Power: 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?

Reply With Quote
  #5  
Old March 13th, 2006, 06:41 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by vermin1302
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.


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().

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Allowing user to choose DLL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT