DLL Published matlab code release

https://blog.csdn.net/alansss/article/details/81320548

Recently matlab code needs to be written to turn on OpenCV code, and then packaged into dll publish. Start looking directly generated based on OpenCV dll, but are less successful, and therefore not to try OpenCV dll generation. 
The main reference to the following two pages: 

VS2013 generate dll and dynamic invocation: 
https://blog.csdn.net/liu_matthew/article/details/55804710  
VS not use the parameter 1 from "const char *" convert "LPCWSTR" Solution: 
HTTPS: //blog.csdn .net / u011394598 / article / details / 80536753

The first step: File -> New -> Project -> win32 console application project name: DLLGenerator 
Application Type: DLL Extras: Empty Project 
Step Two: Write your own needs to be compiled into a dll function in a dll project created cpp and header files H 
dll.h

#ifndef DLL_H
#define DLL_H
//h文件
int Add(int a, int b);
int Mul(int c, int d);
#endif
View Code

dll.cpp

#include "DLL.h"
//cpp文件
int Add(int a, int b)
{
    return a + b;
}

int Mul(int c, int d)
{
    return c * d;
}
View Code

The third step: establishing a source file dll dllmain.cpp, define the entry point for the application 
dllmain.cpp

// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include <windows.h>
BOOL APIENTRY DllMain(
    HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
    )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
View Code

The fourth step is to establish the source file Source.def

LIBRARY "DLLGenerator"
EXPORTS
    Add @1
    Mul @2LIBRARY
View Code

Step Five: Click the compiler to generate solutions, displayed generation is complete. At this .dll files and .lib files will appear in debug directory

VS2013 using the static dynamic link files (using the end) 
because a lot of times we do not lib file and h file, we can use the following ways. 
Create a new empty win32 console application project, add cpp file and added dll file path need to call in the program:

#include <the iostream> 
#include <the Windows.h> 
#include <time.h> 
typedef int (* Dllfun) ( int , int );
 the using  namespace STD;
 int main () 
{ 
    Dllfun F1, F2; 
    the HINSTANCE the HDLL; 
    the HDLL = LoadLibrary ( " E: \\ \\ C Code Debug \\ \\ \\ DLLGenerator DLLGenerator.dll " );
     // here error, then right click and choose project -> properties -> General -> character set (select multi-byte it is OK) 
    IF (the HDLL == NULL) 
    { 
        the FreeLibrary (the HDLL); 
    } 
    F1 = (Dllfun) the GetProcAddress (the HDLL, "Add");
    f2 = (Dllfun)GetProcAddress(hdll, "Mul");
    if (f1 == NULL)
    {
        FreeLibrary(hdll);
    }

    int a = f1(4, 10);//加法
    int b = f2(4, 10);//乘法

    cout <<"4+10=" << a << endl;
    cout << "4*10=" << b << endl;

    FreeLibrary(hdll);
    return 0;
}
View Code

Results are as follows: 

 

Guess you like

Origin www.cnblogs.com/wllwqdeai/p/10981354.html