DLL programming example of VC

Source: Internet
Author: User

Reprinted by Liu





 DLL programming instance

1. Functions for detecting flat areas of signals without DLL encapsulation.

# Include <iostream>

# Include <vector>

Using namespace STD;

 

Int signaljudge (double * signal, int nsize,

Double highlow, int flatmax );

Int main ()

{

Double signal [110] = {, 5,

5, 5, 5, 5.1, 5.3, 1,

5.1, 5,

, 5, 1,

5, 5, 5, 5, 5, 5, 5,

5, 5, 5, 5, 6, 5, 1,

5, 5, 5, 6, 7, 1, 5.6, 5, 5.4,

6.1, 6.2, 1.2,

1.3, 5, 5, 5, 6, 1 };

Double highlow = 3.0;

Int number, flatmin = 5;

Number = signaljudge (signal, 110, highlow, flatmin );

If (number> = 1)

{

Cout <"the number of flat areas is:" <number <Endl;

}

Else

Cout <"no matching results! "<Endl;

Return 0;

}

 

Int signaljudge (double * signal, int nsize,

Double highlow, int flatmin)

{

Int p, q, S = 1, flatwidth, initflatwidth = 3, temp;

Int flatnum = 0, sum = 0, k = 1;

Double Max, Min, average;

Vector <int> position, realflatwidth;

While (S <nsize)

{

If (signal [s]-signal [s-1])> highlow)

{

Flatwidth = initflatwidth;

While (1)

{

For (P = s; P <S + flatwidth; P ++)

{

For (q = S + FlatWidth-1; q> P; q --)

{

If (signal [Q] <signal [q-1])

{

Temp = signal [q-1];

Signal [q-1] = signal [Q];

Signal [Q] = temp;

}

}

}

 

Max = signal [S + FlatWidth-1];

Min = signal [s];

 

If (max-min)

Flatwidth ++;

Else

Break;

}

 

If (FlatWidth-1> = flatmin)

{

Position. push_back (s );

Realflatwidth. push_back (FlatWidth-1 );

Flatnum ++;

}

S = S + FlatWidth-1;

}

Else

S ++;

 

}

 

For (INT I = 0; I <flatnum; I ++)

Sum = sum + realflatwidth [I];

 

For (vector <int>: iterator iter1 = position. Begin (),

Iter2 = realflatwidth. Begin (); iter1! = Position. End (),

Iter2! = Realflatwidth. End (); iter1 ++, iter2 ++)

{

Start position of cout <"nth" <k <"valid flat area:" <* iter1 <Endl;

Cout <"no." <k <"length of valid flat area:" <* iter2 <Endl;

K ++;

}

 

If (flatnum> = 1)

Cout <"average length of valid flat areas:" <(double) sum/flatnum <Endl;

 

Return flatnum;

}

 

Compile and run the following code:

 

 

 

2. Next, encapsulate the red function in the code above into a DLL. (That is, the function signaljudge)

1) create a project MFC Appwizard (DLL) named dlltest

2) Add the following code segment to dlltest. h:

3) Add the following code segment to dlltest. cpp:

Int signaljudge (double * signal, int nsize, double highlow, int flatmin)

{

Int p, q, S = 1, flatwidth, initflatwidth = 3, temp;

Int flatnum = 0, sum = 0, k = 1;

Double Max, Min, average;

Vector <int> position, realflatwidth;

While (S <nsize)

{

If (signal [s]-signal [s-1])> highlow)

{

Flatwidth = initflatwidth;

While (1)

{

For (P = s; P <S + flatwidth; P ++)

{

For (q = S + FlatWidth-1; q> P; q --)

{

If (signal [Q] <signal [q-1])

{

Temp = signal [q-1];

Signal [q-1] = signal [Q];

Signal [Q] = temp;

}

}

}

 

Max = signal [S + FlatWidth-1];

Min = signal [s];

 

If (max-min)

Flatwidth ++;

Else

Break;

}

 

If (FlatWidth-1> = flatmin)

{

Position. push_back (s );

Realflatwidth. push_back (FlatWidth-1 );

Flatnum ++;

}

S = S + FlatWidth-1;

}

Else

S ++;

 

}

 

For (INT I = 0; I <flatnum; I ++)

Sum = sum + realflatwidth [I];

 

For (vector <int>: iterator iter1 = position. Begin (),

Iter2 = realflatwidth. Begin (); iter1! = Position. End (),

Iter2! = Realflatwidth. End (); iter1 ++, iter2 ++)

{

Start position of cout <"nth" <k <"valid flat area:" <* iter1 <Endl;

Cout <"no." <k <"length of valid flat area:" <* iter2 <Endl;

K ++;

}

 

If (flatnum> = 1)

Cout <"average length of valid flat areas:" <(double) sum/flatnum <Endl;

 

Return flatnum;

}

Note: add necessary header files to testdll. cpp.

# Include <vector>

# Include <iostream>

Using namespace STD;

4) Compile the DLL project and generate the testdll. dll and test. Lib files.

3. Create an application project and test the DLL call.

1) create a Win32 console application named dllcall. Add dllcall. cpp to the project.

The Code is as follows:

# Include <iostream>

# Include <vector>

Using namespace STD;

 

Extern "C" _ declspec (dllimport) signaljudge (double * signal, int nsize,

Double highlow, int flatmax );

Int main ()

{

Double signal [110] = {, 5,

5, 5, 5, 5.1, 5.3, 1,

5.1, 5,

, 5, 1,

5, 5, 5, 5, 5, 5, 5,

5, 5, 5, 5, 6, 5, 1,

5, 5, 5, 6, 7, 1, 5.6, 5, 5.4,

6.1, 6.2, 1.2,

1.3, 5, 5, 5, 6, 1 };

Double highlow = 3.0;

Int number, flatmin = 4;

Number = signaljudge (signal, 110, highlow, flatmin );

If (number> = 1)

{

Cout <"the number of flat areas is:" <number <Endl;

}

Else

Cout <"no matching results! "<Endl;

Return 0;

}

2) Copy testdll to the DEBUG directory of the dllcall project, and copy testdll. lib and testdll. H to the dllcall project directory. In testcall, project ---> Settings ---> link ---> module library is added to testdll. Lib

3) Compile and run the project dllcall. The result is as follows:

 

4) Obviously, the running results are the same before and after creating the DLL. All functions can be implemented. At this point, the signaljudge function implements DLL encapsulation.



Reprinted by Liu

DLL programming example of VC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.