Click to See Complete Forum and Search --> : DLLs
Wynd
Aug 21st, 2001, 04:59 PM
How can I call a function in C or C++ from a DLL that I write?
Vlatko
Aug 21st, 2001, 07:43 PM
Here a sample
#include <stdio.h>
#include <windows.h>
typedef VOID (*MYPROC)(LPTSTR);
VOID main(VOID)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("myputs");
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts");
// If the function address is valid, call the function.
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
(ProcAdd) ("message via DLL function\n");
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("message via alternative method\n");
}
Vlatko
Aug 21st, 2001, 07:45 PM
or
Using Load-Time Dynamic Linking
After you have created a DLL, you can use it in an application. The following file, LOADTIME.C, is the source code for a simple console application that uses the myPuts function exported from MYPUTS.DLL.
// File: LOADTIME.C.
// A simple program that uses myPuts from MYPUTS.DLL.
#include <windows.h>
VOID myPuts(LPTSTR); // a function from a DLL
VOID main(VOID)
{
myPuts("message printed using the DLL function\n");
}
Because LOADTIME.C calls the DLL function explicitly, the module for the application must be linked with the import library MYPUTS.LIB. For more information about building DLLs, see the documentation included with your development tools.
parksie
Aug 22nd, 2001, 09:23 AM
This should be useful :)
Vlatko
Aug 23rd, 2001, 05:04 AM
Yes, that is the idea. If you made the dll yourself than you can use the lib file else use the dynamic linking.
Wynd
Aug 23rd, 2001, 12:12 PM
Parksie, one question: in the libtest file, it will link with mylib.lib automatically. When I make my own workspace, I have to specify "debug/mylib.lib" where you just have "mylib.lib." What am I doing wrong here?
parksie
Aug 23rd, 2001, 12:15 PM
I added that folder to the list of library paths to search in Project Settings. However, when I normally use DLLs I copy it to different external folder (d:\lib). I also have a naming convention for the output files:
Dynamic, Release: xx.lib
Dynamic, Debug: xxD.lib
Static, Release: xxS.lib
Static, Debug: xxSD.lib
where xx is the name of the library. This is so I know which libraries are for what, in case of multiple versions.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.