C language Function pointer two (forward call)

Source: Internet
Author: User

//function pointers do function arguments#include <stdio.h>#include<stdlib.h>#include<string.h>#include<Windows.h>/*function pointers do 2 functions for function parameters:1. Defines a pointer type that allocates 4 bytes of memory space of 2. The parameter list for the calling function is specified, and the return value is called forward: calling the DLL file from the window's own system function library, getting the address of the function in the DLL file, executing the function reverse call: Call another function in another function through a function pointer*/voidMain () {//Forward Call//defines the handle----hinstance header file is Windows.hhinstance hinstance; //defining function pointer typestypedefint(*socketinittype) (void**/* out*/); typedefint(*socketsendtype) (void*, unsignedChar*,int ); typedefint(*socketrevtype) (void*, unsignedChar**,int*); typedefint(*socketdestory) (void**); intRET =0; void*handle=NULL; //ready to send a delivery paper    Char*sendstr ="dddddd"; intBuflen1 = strlen (SENDSTR) +1; //Receiving Messages    Char*revstr =NULL; intBuflen2 =0; //Serious note (for the first half-hour): When using the LoadLibrary macro definition, be sure to set the CharSet is not set, or the C language compiler does not recognize the file path//settings: Project Right--Properties--Configuration Properties--general--character set--not setHINSTANCE = LoadLibrary ("E:/l001.dll"); if(hinstance==NULL) {printf ("failed to get file address! \ n"); }    //Get function AddressSocketinittype PF = (socketinittype) GetProcAddress (HINSTANCE,"Cltsocketinit"); if(pf==NULL) {printf ("failed to get function pointer! \ n"); return; } socketsendtype PF2= (Socketsendtype) GetProcAddress (HINSTANCE,"Cltsocketsend"); if(PF2 = =NULL) {printf ("failed to get function pointer! \ n"); return; } socketrevtype pf3= (Socketrevtype) GetProcAddress (HINSTANCE,"Cltsocketrev"); if(PF3 = =NULL) {printf ("failed to get function pointer! \ n"); return; } socketdestory pf4= (socketdestory) GetProcAddress (HINSTANCE,"cltsocketdestory"); if(Pf4 = =NULL) {printf ("failed to get function pointer! \ n"); return; } ret= PF (&handle); if(ret!=0) {printf ("Handle initialization failed! \ n"); GotoAAA; } ret= PF2 (handle, (unsignedChar*) Sendstr, buflen1); if(ret!=0) {printf ("message failed to send! \ n"); } ret= PF3 (handle, (unsignedChar* *) &AMP;REVSTR, &buflen2); if(ret!=0) {printf ("Message Acceptance failed! \ n"); }    //Print the Received messageprintf (REVSTR); //Releasing message Memory     Free(REVSTR); RET= Pf4 (&handle); if(Ret! =0) {printf ("release handle failed! \ n"); }aaa:system ("Pause");}

C language Function pointer two (forward call)

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.