0

I want to use a dll-file in my C-Code, but are very confused about the syntax.

My Story: I made a simple function in Matlab ( f(x1,x2)=x1*x2 ), with the "Matlab Coder" I translated it to C-Code and generated an exe, I could run it from the terminal with arguments.Now I generated a dll instead of an exe and want to use the dll.

Since now I could not make Code explanations, I googled, make work for me. I look up Syntax in http://en.cppreference.com/w/ but for my surprise there wasn't even an entry for e.g. GetProcAddress or LoadLirbary.

Here is the C-Code in which I would like to use the dll:

#include <stdio.h>
#include <stdlib.h>

/*
* In my dream I would load the dll function here
* with something like Load(mytimes4.dll)
*/

int main(int argc, char *argv[]) {

double x1,x2,myresult;
//Load Arguments from Terminal
sscanf(argv[1], "%lf", &x1);
sscanf(argv[2], "%lf", &x2);

// Use and print the function from mytimes4.dll
myresult = mytimes4(x1,x2);
printf("%3.2f\n",myresult);

return 0;
}

After generating the dll, Matlab gave me the following folder: "dll-folder" produced by Matlab

Can someone give me a most simple but complete Code that would work with my example? What files are needed (maybe .def or .exp)? Also for Explanations of the lines involved using the dll I would be gratefull. Or if not, you maybe have some background knowledge that makes the complex syntax reasonable.Thanks in advance!

System information: Windows 7 Pro 64, Matlab 64 2016b, gcc cygwin 64, eclipse ide.

crx
  • 153
  • 2
  • 8
  • I'm not surprised that cppreference didn't have entries for OpenLibrary and GetProcAddress -- these functions are part of Windows API (not part of the C++ standard). For a description of OpenLibrary see https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx and for example usage see https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx for an example of using OpenLibrary and GetProcAddress. – thurizas Oct 28 '16 at 13:46
  • You can check the solution I have proposed to that post [Link to a DLL in Pelles C](http://stackoverflow.com/questions/40282524/link-to-a-dll-in-pelles-c). – J. Piquard Oct 28 '16 at 14:15
  • What is the content of the `mytimes4.h` and `mytimes4.def` ? – J. Piquard Oct 28 '16 at 15:48
  • @thurizas, good links! And I wasn't aware it is windows-C-Code. @ J.Piquard, thanks for helping. I guess your question don't needs an answer now after my self-response. – crx Oct 28 '16 at 19:29
  • @crx also if you ever need to do this on a Linux (and I suspect Unix and Mac, but I don't have them to test), the corresponding functions are `dlopen` and `dlsym` and `dlclose`. Happy coding – thurizas Oct 29 '16 at 12:57

1 Answers1

0

With the link of thurizas I could solve my problem. https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx I copied the code from the side. Below you can see the code with additional comments of mine and with ,in my opinion, more clearly naming. Thus it is probably easier to use for beginners as I am.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 

/*Declaration of the function,contained in dll, as pointer with the arbitrary pointer name
 "*MYFUNCTIONPOINTER" (not sure if it has to be in big letters).
In my case the function means simply f(x1,x2) = x1*x2 and is thus as double declared*/
typedef double (*MYFUNCTIONPOINTER)(double, double);

int main() {

    HINSTANCE hinstLib;
    //"myfunction" is the arbitrary name the function will be called later
    MYFUNCTIONPOINTER myfunction;
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

    //Tell the dll file
    hinstLib = LoadLibrary(TEXT("mypersonal.dll"));

    if (hinstLib != NULL)
        {
        /* At this line "myfunction" gets its definition from "MYFUNCTIONPOINTER"
         and can be used as any other function.The relevant function in the dll has
         to be told here.*/
        myfunction = (MYFUNCTIONPOINTER) GetProcAddress(hinstLib, "mydllfunction");

            // If the function address is valid, call the function.
            if (NULL != myfunction)
            {
                fRunTimeLinkSuccess = TRUE;

             // The function can be used.
             double  myoutput;
             myoutput = myfunction(5,7);
             printf("%f\n",myoutput);
             getchar();
            }
            // Free the DLL module.

            fFreeResult = FreeLibrary(hinstLib);
        }

        // If unable to call the DLL function, use an alternative.
        if (! fRunTimeLinkSuccess)
            printf("Message printed from executable\n");

    return 0;
}
crx
  • 153
  • 2
  • 8