Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-22-2005, 02:54 PM   #1 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Runtime DLL selection

I have a program that compiled with A.lib which will load A.dll. Now I want to use B.dll instead. The functions are the same. I have 2 choices, 1) recompile my app with B.lib or 2) rename B.dll as A.dll and replce the old one.

Is there a way, at runtime without needing to recompile my app, to enumerate all the dll's, and then select which one I would like to use?
Say, next year we have a new C.dll, and we want the old app to be able to use it without renaming it or recompiling.

Thanks.
QUantumAnenome is offline   Reply With Quote
Old 04-22-2005, 04:35 PM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
yes there is.
You can load dll's dynamicly
Code:
HMODULE my_dll = LoadLibrary("A.dll");
After that you must load all ProcAddresses that you need, for example
Code:
typedef WINSHELLAPI void (WINAPI *pILFree)(LPCITEMIDLIST pidl);
pILFree ILFree = (pILFree)GetProcAddress(my_dll, MAKEINTRESOURCE(195));
This creates a pointer to the function that is inside the dll.

GetProcAddress() can be used in 2 ways:
#1 by position (INTRESOURCE)
#2 by name GetProcAddress(my_dll, "winampDSPGetHeader2");

the typedef part is used to identify the function as how it is inside the dll.
I think i'm not 100% here so others could addl in or correct me

Then when you don't need the dll anymore you use
Code:
FreeLibrary(my_dll);
DJMaze is offline   Reply With Quote
Old 04-24-2005, 10:54 AM   #3 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Thanks, this is what I needed, but the LoadLibrary() always fails, regardless of where I copy my DLLs.
QUantumAnenome is offline   Reply With Quote
Old 04-24-2005, 11:29 AM   #4 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Ok, LoadLibrary works fine now (silly error), but what do I need to do to get GetProcAddress() to work. I tried the (my_dll, "MyFuncame"), but I think I need a Module Definition File, but don't know what that is or what to put in it.
Thanks for your help!
QUantumAnenome is offline   Reply With Quote
Old 04-24-2005, 11:55 AM   #5 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Yeah! After wading through the microsuck docs, I figured it out. Here it is for those who need to know.

How to explicitly link to a DLL:

In the DLL, you have to have 3 things.
1) The DllMain() MUST return true.
2) The function you want to export must have __declspec(dllexport) in front of it.
3) You need a .DEF file with a list of functions you are exporting
(Note that the microsuck docs claim you do not need #3 if you do #2, but it turns out you need them both for some reason.)

Code:
#include	<windows.h>

bool WINAPI DllMain(HINSTANCE hInstance, DWORD reason, LPVOID data)
{
	return true;
}

__declspec(dllexport) int Add(int a, int b) 
{
	return (a+b);
}
Here is the .DEF file

Code:
LIBRARY	C1
EXPORTS
   Add  DATA
In the app, you do not need any .LIB files that were generated by the DLL project.

Code:
// HMODULE worked too, I don't know which is technically correct
HINSTANCE my_dll = LoadLibrary("C1.dll");

// you could also typedef it like  DJMaze did
int (*myadd)(int, int) = (int (*)(int, int))GetProcAddress(my_dll1, "Add");

// use it normally
int foo = myadd(1, 2);

// free it when finished
FreeLibrary(my_dll);
QUantumAnenome is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mono runtime supadawg MS Technologies ( ASP, VB, C#, .NET ) 6 04-05-2005 02:58 PM
Langauge selection for a newbie ConcreteClam Standard C, C++ 19 07-12-2004 11:08 PM
drawing a selection box sammy HTML, XML, Javascript, AJAX 1 06-18-2004 10:45 AM
import delphi dll in c# eandras MS Technologies ( ASP, VB, C#, .NET ) 0 05-20-2004 05:36 AM
dll authentication Eyelfixit Windows 18 07-08-2003 06:54 PM


All times are GMT -8. The time now is 06:32 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting