Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling function in dll 2

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
0
0
US
Hi,
I am having a bit of trouble with this one. All I want to do is call a function that is in a dll that I created. The dll gets loaded put I cannot make the function call. What am I missing??

//Here is my dll code

Code:
#include <iostream>


void __declspec(dllexport)__stdcall doIt()
{
	std::cout << &quot;doIt function called &quot; << std::endl;
}


//And here is my calling code in the executable
Code:
#include &quot;afx.h&quot;
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    HANDLE dllHandle;
	typedef void (__stdcall *MY_PROC)(void);
	MY_PROC doIt;
	dllHandle = LoadLibrary((LPCTSTR) &quot;MyDll.dll&quot;); 
	if (dllHandle == NULL)
	{
		cout << &quot;MyDll.dll failed to load...&quot; << endl;
		exit(0);
    }
    
	doIt = (MY_PROC)GetProcAddress((HINSTANCE)dllHandle, &quot;doIt&quot;); 	
	if (doIt == NULL)
	{
		cout << &quot;doIt failed...&quot; << endl;
	}
	else doIt();
   
    return(1);
}


many thanks,

Barry
 
Is the function doIt() actually exported ?

You can check if it is using dumpbin /EXPORTS dllname.dll

Dumpbin is a console program included with VC.

/JOlesen
 
When you call the the function you should call it like this:
(*doIt)();

because doIt is a pointer to a function.
-S.Kannan
 
This is the output I get using the dumpbin program:

Code:
Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


Dump of file H:\CSTUFF\MYDLL\CallMyDll\DEBUG\MyDll.dll

File Type: DLL

  Section contains the following exports for MyDll.dll

           0 characteristics
    3F25D48F time date stamp Mon Jul 28 21:57:35 2003
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 0000108C ?doIt@@YGXXZ

  Summary

        8000 .data
        1000 .idata
        4000 .rdata
        3000 .reloc
       3C000 .text

Yes, I know doIt() is a pointer:
Code:
typedef void (__stdcall *MY_PROC)(void);
I check to see if it's NULL, and it always is. Just for kicks, I tried calling it like (*doIt)(); as S.Kannan suggested and it still doesn't work. Any ideas?



thanks,


Barry
 
I tried the following code and it worked. All I did is to add a DEF file to the dll project.
***************************************************************
// DLL part

void __declspec(dllexport)__stdcall doIt()
{
AfxMessageBox(&quot;doIt&quot;);
}


************************************************************
//// Client part
{
HINSTANCE dllHandle;
typedef void (*MY_PROC)();
MY_PROC doXIt;
dllHandle = ::LoadLibrary(&quot;dll.dll&quot;);
if (dllHandle == NULL)
{
AfxMessageBox(&quot;Dll failed to load&quot;);
return;

}

doXIt = (MY_PROC)GetProcAddress(dllHandle, &quot;doIt&quot;);
if (doXIt == NULL)
{
AfxMessageBox(&quot;Unable to get function address&quot;);
return;
}
else doXIt();

return;
}

I think when you dynamically load a library and call its function the dll project should contain a DEF file. It worked for me. It should work for you.

-S.Kannan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top