tags:

views:

22

answers:

1

I have following function defined in one dll:

__declspec( dllexport ) int __stdcall
mjscall(char *cmd, DWORD wtime, char *stdoutfile, char *stderrfile )

I need to write one process to call the above function. I am doing it first time,I do not have much idea. I have written the following code

#include <windows.h>
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <limits.h>
extern __declspec( dllexport ) int __stdcall mjscall(char *cmd, DWORD wtime, char *stdoutfile, char *stderrfile );
typedef INT (*MJSCALL)    (char *,DWORD, char *, char *);
int main()
{

    char *a,*b,*c;
    a=NULL;
    b=NULL;
    c=NULL;
    DWORD aa =1;
    int i;
    HMODULE hLib;
    MJSCALL ADD;
    hLib=LoadLibrary("f3cucall.dll");
    if(hLib==NULL)
    {
        return 1;
    }
    ADD=(MJSCALL)GetProcAddress(hLib,"mjscall");
    if (ADD==NULL)
    {
        return 1;
    }

    (ADD)(a,aa,b,c);
     return 0;
}

The "(ADD)(a,aa,b,c);" is causing the problem. Can somebody help me out?

A: 

I think you mixed two things up:
the __declspec(dllexport) MSVC keyword exports functions from a DLL (tells the linker to do so), and the __declspec(dllimport) imports functions from a DLL. This is done at loading time, the linker will create all the necessary code to load the DLL and resolve the symbol. In fact, it adds some code to the exe to let the OS load the DLL. You can use functions declared with __declspec(dllimport) just like any normal internal function.

If you want to use this approach, you need the DLL's lib file, since it contains information for the linker to resolve the symbolic name. It doesn't actually contain code, only these information on the DLL for the linker. Additionally, you have to tell the linker that the function you want to use is located at a DLL, using the magic __declspec(dllimport) before the function declaration. That's why you provide a .lib and a header file (containing these declarations) with your DLL if you want to do it this way. You should rebuild the project that uses the DLL when you change the DLL, as the .lib file may have changed. You can use the same header file in your DLL project and the projects that import from this DLL:

#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

MYDLL_API void printMe(int);

The MyDLL_API get resolved to either __declspec(dllexport) (in the DLL project, where you define the MYDLL_EXPORTS in the project settings) or __declspec(dllimport) (in all projects that use the dll). This way, you only need one header file for the DLL.

Another method of calling DLL functions is to use LoadLibrary and GetProcAdress. These two are used to load a DLL at runtime. The main difference between loading a DLL at loading time and at runtime is that you have some control over loading the DLL at runtime, whereas the OS will do the job when the DLL is to load at loading time (e.g. show a message box if the DLL cannot be found and do not run the process).

DyP