How to call a dll from a dll

I have some dll's for a frame grabber card that I need to access from
LabVIEW (a graphical programming environment).  LabVIEW can call win32 dll's and can handle many different datatypes - my problem is that I have not been successful at passing in handles to the dll.  So what I thought I would do was wrap the dll inside a dll which could handle the translation of the types that LabVIEW supports to the data types needed by the frame grabber dll's.

Problem is I am not exactly sure how to call a dll from within another dll.

after searching the Internet I found an example program which uses the LoadLibrary and GetProcAddress functions - these look like what I need but I am not clear on how you actually pass parameters into the dll and get data out of it...   The code I found was downloaded from
http://www.chami.com/tips/delphi/011097D.html

Could someone please provide a clear example of how to do this ?

Thanks For Reading

Chris
duffechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PriyeshCommented:
Suppose you have a function
int sqrt(int) in a dll sqrt.dll in c:\

use the following code to load it.

typedef int SQRTFN (int) ;

HMODULE hDll = ::LoadLibrary("c:\\sqrt.dll") ;
//check for hDll not null.
SQRTFN* sqrt = (SQRTFN*)::GetProcAddress(hDll, "sqrt") ;
//check for sqrt not null
//call sqrt()
int i = sqrt(2) ;
::FreeLibrary(hDll) ;

Hope this helps. as long as you give the path in loadlibrary correct, it doesn't matter wether u load from a dll or exe.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
duffechAuthor Commented:
not quite there yet - taking your example I coded a dll to find the square root - it tests out OK.


/********TEST CODE*******/
/* sqrt.cpp source code */

#include <windows.h>
#include <ctype.h>
#include <math.h>
#include "sqrt.h"

BOOL WINAPI DllMain (
HANDLE hModule,
DWORD dwFunction,
LPVOID lpNot)

{
return TRUE;
}

/* find square root */
_declspec (dllexport) double sqroot(double a){
return((double) pow(a, 0.5));}

***************************************
Using your example, I coded the following:

/* sqrt.dll wrapper code */

#include <windows.h>
#include <ctype.h>
#include "check_sqrt.h"

BOOL WINAPI DllMain (
HANDLE hModule,
DWORD dwFunction,
LPVOID lpNot)

{
return TRUE;
}

/* square root via another dll */
_declspec (dllexport) double get(double a){

typedef double SQRTFN (double);

HMODULE hDll = LoadLibrary("sqrt.dll");
//check for hDll not null.
SQRTFN* sqroot = (SQRTFN*) GetProcAddress(hDll, "sqroot") ;
//check for sqrt not null
//call sqrt()
double i = sqroot(a) ;
FreeLibrary(hDll) ;

return((double)(i));}

The above code compiles as written - however it will not compile with a path in front of the "sqrt.dll" - so I omitted the path and put the sqrt.dll in the same path as the wrapper dll.  Upon execution LabVIEW explodes and dies
LabVIEW has no problem calling the sqrt.dll directly.

any thoughts ?????




0
PriyeshCommented:
duffech,
  Did you try to debug or outputdebugstring to find out to what point our second dll makes it? I think you have a path problem and i can't find why it won't compile with a path in front of the dll name. If you put "c:\\dll.dll" it should compile.
  Could you paste the exact code that won't compile with the path in front?
   Also, pls put OutputDebugString after the LoadLibrary and GetProcAddress to check for the validity of recieved handles/pointers.
 
Priyesh.
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

MFCAnswerCommented:
U can try using ,lib file created from one dll, to
create another dll. THis way u may not be able to
control loading anf unloading of dll, however
u will be free of pointer problems.
0
duffechAuthor Commented:


SUCCESS !

Using what you originally gave me and digging into the MSDN CD I came up with this working solution - seems really simple now :-)

The main issue with the original code was the typedef - I found this by putting the dll call inside a simple console app and attempting to call the dll multiple times - it would always work the first time but would fail on subsequent calls with an exception.  I am not sure probably the function pointer was walking around on memory.

The form below seems to work 100 %

Thanks for the help !!!!
Chris



// This code demonstrates calling a dll function from another dll.

#include <windows.h>
#include "check_sqrt.h"

typedef double (CALLBACK* LPFNDLLFUNC1)(double);
LPFNDLLFUNC1 sqroot;          // Function pointer
HINSTANCE hDll;               // Handle to dll
double answer;                          // The answer goes here

BOOL WINAPI DllMain (
HANDLE hModule,
DWORD dwFunction,
LPVOID lpNot)

{
return TRUE;
}


_declspec (dllexport) double get(double a)

{

hDll = LoadLibrary("sqrt.dll");      // get handle to desired dll
if (hDll != NULL)                        // if handle is NULL return -1
{
sqroot = (LPFNDLLFUNC1) GetProcAddress(hDll, "sqroot");// get function pointer
answer = sqroot(a);            // call dll function
FreeLibrary(hDll);            // release the dll handle
return(answer);                  // return the function evaluation
}
else
{
answer = -1.0;                  // handle was NULL, return -1
return(answer);
}

}
0
duffechAuthor Commented:
BTW I never could get the path to the dll to work ?  The root path does work e.g.

This compiles and works>  

c:\\mydll.dll

This doesn't compile > 

c:\mydll.dll

This compiles but doesn't work >

c:\\temp\\mydll.dll

very weird, but not a show stopper for me since I will just locate the library dlls in the same directory with the app.

0
PriyeshCommented:
Hi,
   The c:\mydll.dll does not compile because, the compiler tries to build up an escape sequence like \n etc from the string.. so whenever u use \ in strings, u have to tell the compiler that u intended a \ by putting one more \ in front.. ie \\,
If your dll is there in c:\temp, then the 3rd form c:\\temp\\mydll.dll should work, it seems wierd to me also.. Copy the dll to c:\temp and try opening it as a file for reading to make sure if  you want to dig the problem to find what's wrong..

Like this:
 
 FILE* fp = fopen("c:\\temp\\mydll.dll", "r") ;
 //check for fp != NULL

 if you get a value in fp, we know that the file is seen from program and the LoadLibrary also should load it if the same dll loads from app dir..

Thanks and best wishes,
Priyesh.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.