MFC calls C Dynamic library functions-----to be replenished

Source: Internet
Author: User

Interface with MFC write the background with a written C program is OK

C written by the program compiled into a DLL, the function used to make an export function, in C + + W/MFC program called

1,VC can compile C function dynamic Library;

2.simple dll writing example ( Take the simplest two-number addition function as an example ):

Create Project Win32 Dynamic-link Library.

Add header files and source files such as dll.h dll.cpp ,

In the header file:
#ifndef Dll_h
#define Dll_h
extern "C" int __declspec (dllexport) Add (int x, int y);
#endif
In the source file:
#include "dll.h"
int add (int x, int y)
{
return x + y;
}
3,DLLsimple invocation of:
#include <stdio.h>
#include <windows.h>
typedef int (*lpaddfun) (int, int); //macro definition function pointer type
int main (int argc, char *argv[])
{
HINSTANCE hDLL; Dll Handle 
Lpaddfun Addfun; //function Pointers
hDLL = LoadLibrary (".. \\Debug\\dll.dll "); //This is your last project .DLLpath, of course you can directly putDLLCopy to current project, so write directlyhDLL = LoadLibrary ("Dll.dll");
if (hdll! = NULL)
{
Addfun = (lpaddfun) GetProcAddress (hDLL, "add");
if (addfun! = NULL)
{
int result = Addfun (2, 3);
printf ("%d", result);//mfcchange it intoAfxMessageBoxto test
}
FreeLibrary (hDLL); //don't forget to release when you're done.
}
return 0;
}

MFC calls C Dynamic library functions-----to be replenished

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.